malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int Max = 2e5 + 10;   vector<int> A;   int ok[Max];   int len;   int seg[Max * 4];   int lazy[Max * 4];   void build(int tl = 0, int tr = len + 1, int idx = 1) {     if (tr - tl == 1) {       seg[idx] = len - tl;       return;     }     int mid = (tl + tr) / 2;     build(tl, mid, idx * 2);     build(mid, tr, idx * 2 + 1);     seg[idx] = min(seg[idx * 2], seg[idx * 2 + 1]);   }   void shift(int idx) {     if (!lazy[idx]) return;     lazy[idx * 2] += lazy[idx];     lazy[idx * 2 + 1] += lazy[idx];     seg[idx * 2] += lazy[idx];     seg[idx * 2 + 1] += lazy[idx];     lazy[idx] = 0;   }   void upd(int i, int val, int tl = 0, int tr = len + 1, int idx = 1) {     if (tr - 1 <= i) {       lazy[idx] += val;       seg[idx] += val;       return;     }     if (tl > i) return;     shift(idx);     int mid = (tl + tr) / 2;     upd(i, val, tl, mid, idx * 2);     upd(i, val, mid, tr, idx * 2 + 1);     seg[idx] = min(seg[idx * 2], seg[idx * 2 + 1]);   }   int check() { return seg[1]; }   int main() {     int n, h;     cin >> n >> len >> h;     for (int i = 0; i < len; i++) {       int a;       cin >> a;       A.push_back(a);     }     sort(A.begin(), A.end());     for (int i = 0; i < n; i++) {       long long a;       cin >> a;       int l = -1, r = len;       while (r - l > 1) {         int idx = (l + r) / 2;         if (a + A[idx] >= h)           r = idx;         else           l = idx;       }       ok[i] = r;     }     int ans = 0;     build();     for (int i = 0; i < len; i++) upd(ok[i], -1);     if (check() >= 0) ans++;     for (int i = len; i < n; i++) {       upd(ok[i], -1);       upd(ok[i - len], 1);       if (check() >= 0) ans++;     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int arr[n];     for (int i = 0; i <= n - 1; i++) cin >> arr[i];     vector<pair<int, int> > G[n];     int cnt = 0;     for (int i = 0; i <= n - 1; i++) {       for (int j = i + 1; j <= n - 1; j++) {         if (arr[i] > arr[j]) {           G[i].push_back(make_pair(arr[j], j));           cnt++;         }       }     }     cout << cnt <<   n ;     for (int i = 0; i <= n - 1; i++) {       sort(G[i].begin(), G[i].end());       int s = G[i].size();       for (int j = s - 1; j >= 0; j--) {         cout << i + 1 <<     << G[i][j].second + 1 <<   n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int arr[300022];   int main() {     int q;     scanf( %d , &q);     while (q--) {       int n, i;       scanf( %d , &n);       for (i = 0; i < n; i++) scanf( %d , &arr[i]);       map<int, int> mf, ml;       map<int, int>::iterator it;       for (i = 0; i < n; i++) {         if (mf.find(arr[i]) == mf.end()) mf[arr[i]] = i;         ml[arr[i]] = i;       }       int ans = 0, in = -1, len = 0;       int cou = 0;       for (it = mf.begin(); it != mf.end(); it++) {         cou++;         int ele = it->first;         int focc = it->second, locc = ml[ele];         if (focc > in)           len++;         else           len = 1;         in = locc;         ans = max(ans, len);       }       cout << (cou - ans) << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   namespace mainns {   template <typename T>   void relaxMax(T& relaxWhat, const T& relaxBy) {     relaxWhat = max(relaxWhat, relaxBy);   }   template <typename T>   void relaxMin(T& relaxWhat, const T& relaxBy) {     relaxWhat = min(relaxWhat, relaxBy);   }   namespace {   const vector<pair<int, int>> offsets = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};   const unordered_map<char, char> nextLetter = {       { D ,  I }, { I ,  M }, { M ,  A }, { A ,  D }};   }  // namespace   class Solver374C {    public:     void run();     int height, width;     vector<string> board;     vector<vector<int>> maxValues;     int getMaxValue(int x, int y) {       if (maxValues[x][y] > 0) return maxValues[x][y];       if (maxValues[x][y] == -2) return numeric_limits<int>::max() / 10;       maxValues[x][y] = -2;       int maxNeighbourValue = 0;       for (auto& offs : offsets) {         auto neigbour = make_pair(x + offs.first, y + offs.second);         if (neigbour.first >= 0 && neigbour.first < width &&             neigbour.second >= 0 && neigbour.second < height)           if (nextLetter.at(board[y][x]) ==               board[neigbour.second][neigbour.first])             relaxMax(maxNeighbourValue,                      getMaxValue(neigbour.first, neigbour.second));       }       return maxValues[x][y] = maxNeighbourValue + 1;     }   };   void Solver374C::run() {     cin >> height >> width;     board = vector<string>(height);     for (auto& s : board) cin >> s;     maxValues = vector<vector<int>>(width, vector<int>(height, -1));     int maxValue = 0;     for (int x = 0; x < width; ++x)       for (int y = 0; y < height; ++y) {         auto v = getMaxValue(x, y);         if (board[y][x] ==  D ) relaxMax(maxValue, v);       }     if (maxValue < 4)       cout <<  Poor Dima! ;     else if (maxValue >= numeric_limits<int>::max() / 10)       cout <<  Poor Inna! ;     else       cout << (maxValue / 4);   }   }  // namespace mainns   using CurrentSolver = mainns::Solver374C;   int main() {     ios::sync_with_stdio(false);     CurrentSolver().run();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool allCharactersSame(string s) {     int n = s.length();     for (int i = 1; i < n; i++)       if (s[i] != s[0]) return false;     return true;   }   signed main() {     int n;     const int letters = 26;     cin >> n;     for (int i = 0; i < n; i++) {       string str;       cin >> str;       int n = str.size();       vector<int> cnt(letters);       for (int i = 0; i < n; ++i) cnt[str[i] -  a ]++;       int ans = 0, k = 0;       for (int i = 0; i < letters; ++i) {         if (cnt[i] > 1)           ans++;         else if (cnt[i] == 1)           k++;       }       ans += k / 2;       cout << ans <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200010;   int hp[N], dmg[N], p[N];   bool cmp(int a, int b) { return hp[a] - dmg[a] > hp[b] - dmg[b]; }   bool vis[N];   int main() {     int n, a, b;     scanf( %d%d%d , &n, &a, &b);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &hp[i], &dmg[i]);       p[i] = i;     }     sort(p + 1, p + 1 + n, cmp);     long long sum = 0, ans, la = 0, cb = 0;     bool flag = false;     for (int i = 1; i <= n; i++) {       if (i > b || hp[p[i]] <= dmg[p[i]]) {         if (!flag) flag = true, la = hp[p[i - 1]] - dmg[p[i - 1]], cb = i - 1;       }       if (!flag)         sum += hp[p[i]], vis[p[i]] = true;       else         sum += dmg[p[i]];     }     ans = sum;     if (a && b) {       for (int i = 1; i <= n; i++) {         long long tmp = hp[i];         for (int j = 1; j <= a; j++) tmp = tmp * 2;         tmp += sum;         if (vis[i]) {           tmp -= hp[i];         } else {           tmp -= dmg[i];           if (cb == b) tmp -= la;         }         ans = max(ans, tmp);       }     }     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long Maxn = 10000;   const long double pi = acos(-1);   const long long mod = 1000000007;   template <class T>   ostream &operator<<(ostream &out, vector<T> &A) {     for (auto x : A) out << x <<    ;     return out;   }   template <class T>   ostream &operator<<(ostream &out, set<T> &A) {     for (auto x : A) out << x <<    ;     return out;   }   template <class T1, class T2>   T1 powr(T1 a, T2 b) {     T1 res = 1;     for (long long i = 1; i < b + 1; i++) res = res * a;     return res;   }   int32_t main() {     std::ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n, l, r, x;     while (cin >> n >> l >> r >> x) {       vector<long long> k(n);       for (long long i = 0; i < n; i++) {         cin >> k[i];       }       long long cnt = 0;       for (long long i = 0; i < (1 << n); i++) {         long long mi = 1000000;         long long ma = -1;         long long sum = 0;         for (long long j = 0; j < n; j++) {           if (i & (1 << j)) {             sum = sum + k[j];             ma = max(ma, k[j]);             mi = min(mi, k[j]);           }         }         if (l <= sum && sum <= r && (ma - mi) >= x) {           cnt++;         }       }       cout << cnt << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1000 + 5;   int N, P[MAXN];   long long D[MAXN];   vector<pair<long long, int>> v, f[2];   int main() {     scanf( %d , &N);     P[1] = 1, P[2] = 2;     for (int i = 3, x; i <= N; ++i) {       printf( 2 1 %d %d n , P[2], i);       fflush(stdout);       scanf( %d , &x);       if (x == -1) P[2] = i;     }     for (int i = 2; i <= N; ++i) {       if (i == P[2]) continue;       printf( 1 1 %d %d n , P[2], i);       fflush(stdout);       scanf( %lld , &D[i]);       v.push_back(make_pair(D[i], i));     }     sort(v.begin(), v.end());     int p = v.back().second;     for (int i = 2, x; i <= N; ++i) {       if (i == p || i == P[2]) continue;       printf( 2 1 %d %d n , p, i);       fflush(stdout);       scanf( %d , &x);       f[x == -1].push_back(make_pair(D[i], i));     }     sort(f[0].begin(), f[0].end());     sort(f[1].begin(), f[1].end());     printf( 0 %d %d  , P[1], P[2]);     for (int i = 0; i < f[1].size(); ++i) printf( %d  , f[1][i].second);     printf( %d  , p);     for (int i = f[0].size() - 1; i >= 0; --i) printf( %d  , f[0][i].second);     fflush(stdout);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int i, j, m, n, u, v;   long long a[15], mod, lose[14][16389], f[16389], g[16389][15];   long long sz[16389], inv[16389];   long long getinv(long long a, long long p) {     if (a == 1) return 1;     long long k = a - getinv(p % a, a);     return (k * p + 1) / a;   }   long long group_lose(int v, int u) {     int i, j;     long long ret;     ret = f[v] * f[u - v] % mod * g[u - v][sz[v]] % mod * inv[u] % mod;     return ret;   }   int main() {     mod = 1000000007;     scanf( %d , &n);     for (i = 0; i <= n - 1; i++) {       scanf( %lld , &a[i]);     }     for (u = 0; u <= (1 << n) - 1; u++) {       sz[u] = 0;       for (i = 0; i <= n - 1; i++)         if (u & (1 << i)) sz[u]++;     }     for (u = 0; u <= (1 << n) - 1; u++) {       f[u] = 1;       for (i = 0; i <= n - 1; i++)         if (u & (1 << i)) {           for (j = i + 1; j <= n - 1; j++)             if (u & (1 << j)) {               f[u] = f[u] * (a[i] + a[j]) % mod;             }         }       inv[u] = getinv(f[u], mod);     }     for (u = 0; u <= (1 << n) - 1; u++) {       g[u][0] = 1;       g[u][1] = 1;       for (i = 0; i <= n - 1; i++)         if (u & (1 << i)) {           g[u][1] = g[u][1] * a[i] % mod;         }       for (i = 2; i <= n; i++) {         g[u][i] = g[u][i - 1] * g[u][1] % mod;       }     }     for (u = 0; u <= (1 << n) - 1; u++) {       for (i = 0; i <= n - 1; i++) {         if (u & (1 << i)) {           lose[i][u] = 0;           for (v = u; v; v = (v - 1) & u)             if ((v != u) && (v & (1 << i))) {               long long temp = group_lose(v, u);               lose[i][u] += (1 - lose[i][v] + mod) * temp % mod;             }           lose[i][u] %= mod;         }       }     }     long long ans = 0;     for (i = 0; i <= n - 1; i++) {       ans += (1 - lose[i][(1 << n) - 1] + mod);     }     ans %= mod;     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   const long double eps = 1e-9;   const long long mod = 1e9 + 7, ll_max = (long long)1e18;   const int MX = 2e5 + 10, int_max = 0x3f3f3f3f;   using namespace std;   mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());   long long n, k;   long long arr[MX];   int solve() {     cin >> n >> k;     for (int i = 0; i < k; i++) {       arr[i] += i + 1;       n -= i + 1;     }     if (n < 0) return 0;     for (int i = 0; i < k; i++) {       arr[i] += (n / k);     }     n %= k;     for (int i = k - 1; i > 0; i--) {       int del = min(2 * arr[i - 1] - arr[i], n);       n -= del;       arr[i] += del;     }     return (n == 0);   }   int main() {     cin.tie(0)->sync_with_stdio(0);     if (solve()) {       cout <<  YES n ;       for (int i = 0; i < k; i++) cout << arr[i] <<    ;       cout <<   n ;     } else {       cout <<  NO n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   long long x[300001];   long long p[300001] = {1};   int main() {     std::ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> x[i];       p[i + 1] = p[i] * 2 % MOD;     }     sort(x, x + n);     long long ret = 0;     for (int i = 1; i <= n; ++i) {       ret = (ret + x[i - 1] * (p[i - 1] - p[n - i])) % MOD;     }     cout << (ret + MOD) % MOD;   }
#include <bits/stdc++.h>   using namespace std;   vector<long long int> v, v1, v2;   multiset<long long int> mss;   multiset<long long int>::iterator it;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int a, b, c, d, i, j, z = 0, k, w = 1, q = 0, cnt = 0, x = 0, p = 0;     cin >> a >> c;     for (i = 0; i < a; i++) {       cin >> b;       v.push_back(b);     }     sort(v.begin(), v.end());     for (i = 0; i < a / 2; i++) {       v1.push_back(v[i]);     }     for (j = i; j < a; j++) {       mss.insert(v[j]);     }     for (i = 0; i < v1.size(); i++) {       d = v1[i] + c;       it = mss.lower_bound(d);       if (it == mss.end()) {         break;       }       cnt++;       mss.erase(it);     }     cout << cnt;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool cmp(int x, int y) { return x > y; }   const int maxn = 3e5 + 100;   vector<int> v[maxn];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     for (int i = 2; i < maxn; i++)       for (int j = i; j < maxn; j += i) v[j].push_back(i);     v[1].push_back(1);     int t;     cin >> t;     while (t--) {       int n;       char c;       cin >> n >> c;       string s;       cin >> s;       int check[n + 1];       memset(check, 0, sizeof(check));       int cnt = 0;       for (int i = 0; i < n; i++)         if (s[i] != c) {           cnt++;           for (auto j : v[i + 1]) check[j] = 1;         }       if (!cnt) {         cout << 0 << endl;         continue;       }       int cc = 0;       for (int i = 2; i <= n; i++)         if (!check[i] && s[i - 1] == c) cc = i;       if (!cc)         cout << 2 << endl << n <<     << n - 1 << endl;       else         cout << 1 << endl << cc << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class c>   struct rge {     c b, e;   };   template <class c>   rge<c> range(c i, c j) {     return rge<c>{i, j};   }   template <class c>   auto dud(c* x) -> decltype(cerr << *x, 0);   template <class c>   char dud(...);   struct debug {     template <class c>     debug& operator<<(const c&) {       return *this;     }   };   void test_case() {     int n, k;     cin >> n >> k;     vector<pair<int, int>> a(n);     for (int i = 0; i < n; i++) {       cin >> a[i].first;       a[i].second = i;     }     sort(a.begin(), a.end(), less<pair<int, int>>());     int start = 0, tot = 0;     vector<int> res;     while (start < n && k - a[start].first >= 0) {       debug() <<   [                <<  k                    :                 << (k) <<  ]  ;       tot++;       k = k - a[start].first;       res.push_back(a[start].second + 1);       start++;     }     cout << tot << endl;     for (int x : res) {       cout << x <<    ;     }     cout << endl;   }   int main() {     int t = 1;     for (int i = 0; i < t; i++) test_case();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, c;     cin >> a >> c;     vector<long long> aa;     while (a) {       aa.push_back(a % 3);       a /= 3;     }     vector<long long> cc;     while (c) {       cc.push_back(c % 3);       c /= 3;     }     long long i = 0, j = 0;     vector<long long> res;     while (i < aa.size() && j < cc.size()) {       res.push_back((cc[j] - aa[i] + 3) % 3);       i++;       j++;     }     while (i < aa.size()) {       res.push_back((3 - aa[i]) % 3);       i++;     }     while (j < cc.size()) {       res.push_back(cc[j] % 3);       j++;     }     long long p = 1;     long long ans = 0;     for (int i = 0; i < res.size(); i++) {       ans += (res[i] * (p));       p *= 3;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int sett(int n, int pos) { return n = n | (1 << pos); }   int reset(int n, int pos) { return n = n & ~(1 << pos); }   bool check(int n, int pos) { return (bool)(n & (1 << pos)); }   template <typename T>   T bigmod(T x, T p) {     if (p == 0) return 1;     if (p & 1) {       return x * bigmod(x, p - 1) % 1000000007;     } else {       T tmp = bigmod(x, p >> 1) % 1000000007;       return tmp * tmp % 1000000007;     }   }   template <typename T>   void print(const T& v) {     cerr << v <<    ;   }   template <typename T1, typename... T2>   void print(const T1& first, const T2&... rest) {     print(first);     print(rest...);   }   const int inf = (1 << 30);   int a[2000123 + 4];   struct dat {     long long val, id;     dat(long long a, long long b) {       val = a;       id = b;     };     bool operator<(dat x) const { return val == x.val ? id < x.id : val < x.val; }   };   set<dat> st;   vector<dat> v;   bool compare(dat x, dat y) { return x.id < y.id; }   void func(dat x) { cout << x.val <<     << x.id <<   n ; }   void solve() {     set<dat>::iterator it, it2, it3;     while (1) {       if (st.size() < 2) break;       it = st.begin();       it2 = st.begin();       it2++;       if (it->val != it2->val) {         v.push_back(*it);         st.erase(it);       } else {         dat x = dat(it->val + it2->val, it2->id);         st.erase(it);         it = st.begin();         st.erase(it);         st.insert(x);       }     }     if (st.size() == 1) {       v.push_back(*(st.begin()));     }     sort(v.begin(), v.end(), compare);     cout << v.size() <<   n ;     for (int i = 0; i < v.size(); i++) cout << v[i].val <<    ;   }   int main() {     long long cnt = 1, n, qr, k;     scanf( %I64d , &n);     for (int i = 1; i <= (int)n; i++) {       scanf( %I64d , &k);       st.insert(dat(k, (long long)i));     }     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   map<int, vector<int> > mx;   map<int, vector<int> > my;   int pow(int x) {     long long i = 2;     long long r = 1;     while (x) {       if (x & 1) r = (r * i) % 1000000007;       i = (i * i) % 1000000007;       x = x >> 1;     }     return r;   }   int main() {     ios::sync_with_stdio(false);     int i, j, n, t;     cin >> n;     int x[n];     int y[n];     long long an = 1;     for (i = 0; i < n; i++) {       cin >> x[i] >> y[i];       mx[x[i]].push_back(i);       my[y[i]].push_back(i);     }     bool visit[i];     memset(visit, 0, i);     set<int> sx;     set<int> sy;     int c;     queue<int> q;     for (i = 0; i < n; i++)       if (!visit[i]) {         c = 0;         sx.clear();         sy.clear();         for (j = 0; j < mx[x[i]].size(); j++) {           c++;           q.push(mx[x[i]][j]);           visit[mx[x[i]][j]] = 1;         }         sx.insert(x[i]);         for (j = 0; j < my[y[i]].size(); j++) {           c++;           q.push(my[y[i]][j]);           visit[my[y[i]][j]] = 1;         }         sy.insert(y[i]);         t = i;         while (!q.empty()) {           i = q.front();           q.pop();           if (sx.find(x[i]) == sx.end())             for (j = 0; j < mx[x[i]].size(); j++) {               c++;               q.push(mx[x[i]][j]);               visit[mx[x[i]][j]] = 1;             }           sx.insert(x[i]);           if (sy.find(y[i]) == sy.end())             for (j = 0; j < my[y[i]].size(); j++) {               c++;               q.push(my[y[i]][j]);               visit[my[y[i]][j]] = 1;             }           sy.insert(y[i]);         }         i = t;         if (c / 2 < sx.size() + sy.size())           an = (an * (pow(sx.size() + sy.size()) + 1000000007 - 1)) % 1000000007;         else           an = (an * pow(sx.size() + sy.size())) % 1000000007;       }     cout << an;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long Pow(long long a, long long b) {     long long ans = 1LL, base = a % 1000000007;     while (b != 0) {       if (b & 1LL != 0) ans = ans * base % 1000000007;       base = base * base % 1000000007;       b >>= 1LL;     }     return ans;   }   long long A, B, n, x;   long long ans;   int main() {     scanf( %lld %lld %lld %lld , &A, &B, &n, &x);     long long ppow = Pow(A, n) % 1000000007;     if (A == 1LL)       ans = (x % 1000000007 + n % 1000000007 * B % 1000000007) % 1000000007;     else       ans = (ppow * x % 1000000007 + B % 1000000007 * (ppow - 1LL) % 1000000007 *                                          Pow(A - 1LL, 1000000007 - 2) %                                          1000000007) %             1000000007;     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     int n, d, m;     cin >> n >> m >> d;     int c[m], sum = 0;     for (int i = 0; i < m; i++) {       cin >> c[i];       sum += c[i];     }     d--;     if (sum + (m + 1) * d >= n) {       cout <<  YES  << endl;       int k = (d > 0 ? (n - sum) / d : 0), res = (d > 0 ? (n - sum) % d : 0),           cnt = 0, id = 0;       bool flag = 1;       for (int i = 1; i <= n; i++) {         int j;         if (cnt == k && res) {           k++, d = res;           res = 0;         }         if (cnt < k && flag) {           for (j = i; j < i + d && j <= n; j++) {             cout << 0 <<    ;           }           cnt++;         } else {           for (j = i; j < i + c[id]; j++) {             cout << id + 1 <<    ;           }           id++;         }         flag = !flag;         i = j - 1;       }       cout << endl;     } else       cout <<  NO  << endl;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e6 + 7;   const int ALPHA = 40;   const long long INF = 1e16 + 7;   const int MOD = 1e9 + 7;   const int LOG = 22;   const int BASE[2] = {313, 239};   long long n, d;   bool ns[MAXN], seen[MAXN];   vector<long long> group[MAXN], plast[MAXN];   void solve() {     cin >> n >> d;     for (int i = 0; i < n; i++) {       group[i].clear();       plast[i].clear();     }     for (int i = 0; i < n; i++) cin >> ns[i];     fill(seen, seen + n, 0);     long long ptr = 0, cnt = 0;     for (int i = 0; i < n; i++) {       ptr = i;       if (seen[ptr]) continue;       while (!seen[ptr]) {         seen[ptr] = 1;         group[cnt].push_back(ns[ptr]);         ptr = (ptr + d) % n;       }       cnt++;     }     for (int i = 0; i < cnt; i++) {       long long k = group[i].size();       for (int j = 0; j < k; j++) group[i].push_back(group[i][j]);       long long last = -INF;       for (int j = 0; j < group[i].size(); j++) {         if (!group[i][j]) last = j;         plast[i].push_back(j - last);       }     }     long long maxim = 0;     for (int i = 0; i < cnt; i++) {       for (int j = 0; j < group[i].size() / 2; j++) {         maxim = max(maxim, min(plast[i][j], plast[i][j + (group[i].size() / 2)]));       }     }     if (maxim >= INF)       cout << -1 <<   n ;     else       cout << maxim <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     ;     int t;     cin >> t;     while (t--) solve();   }