CF1579F Array Stabilization (AND version)
好咕。
循环右移的数组肯定不陌生,这一题定死了循环的步宽
那我们知道了每次操作的结果影响,就考虑序列循环右移影响到的范围。整个序列长为
现在得出思路,只要有一个位置为
感觉表达的还是有点糟糕,哎看不懂的我只能说声抱歉,第一次写数论题解实在不知道怎么表达清楚。
代码复杂度
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int a[N];
bool vis[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t;
cin >> t;
while (t --) {
int n, d;
cin >> n >> d;
fill(vis+1, vis+n+1, 0);
for (int i=1;i<=n;i++)
cin >> a[i];
int ans=0;
for (int i=1;i<=n;i++) {
if (!a[i] && !vis[i]) {
int cnt=0, st=i;
while (!vis[st]) {
vis[st] = 1;
if (a[st]) ans = max(ans, cnt);
else cnt = 0;
st = (st-d+n-1) % n + 1;
++cnt;
}
}
}
bool fg = 0;
for (int i=1;i<=n;i++)
if (!vis[i]) {
fg = 1;
break;
}
if (fg) cout << "-1\n";
else cout << ans << '\n';
}
return 0;
}