malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   namespace sat {   const int MAX = 200010;   bool vis[MAX];   vector<int> ed[MAX], rev[MAX];   int n, m;   vector<int> order;   int comp[MAX];   inline int inv(int x) { return ((x) <= n ? (x + n) : (x - n)); }   void init(int vars) {     n = vars, m = vars << 1;     for (int i = 1; i <= m; i++) {       ed[i].clear();       rev[i].clear();     }   }   inline void add(int a, int b) {     ed[a].push_back(b);     rev[b].push_back(a);   }   inline void OR(int a, int b) {     add(inv(a), b);     add(inv(b), a);   }   inline void AND(int a, int b) {     add(a, b);     add(b, a);   }   void XOR(int a, int b) {     add(inv(b), a);     add(a, inv(b));     add(inv(a), b);     add(b, inv(a));   }   inline void XNOR(int a, int b) {     add(a, b);     add(b, a);     add(inv(a), inv(b));     add(inv(b), inv(a));   }   inline void force_true(int x) { add(inv(x), x); }   void dfs(int s) {     vis[s] = true;     for (int x : ed[s]) {       if (vis[x]) continue;       dfs(x);     }     order.push_back(s);   }   void mark(int s, int c) {     comp[s] = c;     vis[s] = true;     for (int x : rev[s]) {       if (vis[x]) continue;       mark(x, c);     }   }   bool satisfy(vector<int> &res) {     memset(vis, 0, sizeof(vis));     for (int i = 1; i <= m; i++) {       if (vis[i]) continue;       dfs(i);     }     memset(vis, 0, sizeof(vis));     reverse(order.begin(), order.end());     int it = 0;     for (int s : order) {       if (vis[s]) continue;       mark(s, ++it);     }     res.clear();     for (int i = 1; i <= n; i++) {       if (comp[i] == comp[inv(i)]) return false;       if (comp[i] > comp[inv(i)]) res.push_back(i);     }     return true;   }   }  // namespace sat   int main() {     int n, m, a, b, c;     scanf( %d %d , &n, &m);     sat::init(n);     for (int i = 1; i <= m; i++) {       scanf( %d %d %d , &a, &b, &c);       if (c == 0)         sat::XOR(a, b);       else         sat::XNOR(a, b);     }     vector<int> V;     if (sat::satisfy(V)) {       cout << V.size() << endl;       for (int i = 0; i < V.size(); i++) cout << V[i] <<    ;       puts(  );     } else       puts( Impossible );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long modpow(int a, int b, int m) {     long long res = 1;     long long base = a;     base %= m;     while (b > 0) {       if (b % 2 == 1) {         res = (res * base) % m;       }       b >>= 1;       base = (base * base) % m;     }     return res;   }   long long modInverse(int a, int m) { return modpow(a, m - 2, m); }   long long modify(long long num) { return (num + 1000000007) % 1000000007; }   long long mypow(int base, int exp) {     long long ret = 1;     for (int i = 0; i < (exp); ++i) {       ret *= base;     }     return ret;   }   int main() {     int n, k;     cin >> n >> k;     vector<int> a;     vector<int> b;     for (int i = 0; i < (n / k); ++i) {       int nxt;       cin >> nxt;       a.push_back(nxt);     }     for (int i = 0; i < (n / k); ++i) {       int nxt;       cin >> nxt;       b.push_back(nxt);     }     long long ans = 1;     for (int i = 0; i < (n / k); ++i) {       long long minians = 0;       for (int dig = 0; dig <= 9; dig++) {         if (dig == b[i]) continue;         long long top = (long long)(dig + 1) * mypow(10, k - 1) - 1l;         long long bot = (long long)(dig)*mypow(10, k - 1) - 1l;         long long topcnt = (long long)(top / a[i]);         topcnt++;         if (dig == 0) {           minians += topcnt;           continue;         }         long long botcnt = (long long)(bot / a[i]);         botcnt++;         minians += topcnt - botcnt;         minians = (minians + 1000000007) % 1000000007;       }       ans = (ans * minians) % 1000000007;     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int sum, limit, start = 0, end = 0;     cin >> sum >> limit;     vector<pair<long long int, long long int>> v;     vector<long long int> ans;     for (long long int i = 1; i <= limit; i++) {       long long int t = i, cn = 0;       while (t > 0) {         if (t % 2 == 0)           t /= 2, cn++;         else           break;       }       long long int val = pow(2, cn);       v.push_back(make_pair(val, i));     }     sort(v.begin(), v.end());     for (long long int i = v.size() - 1; i >= 0; i--) {       if (sum < v[i].first)         continue;       else         sum -= v[i].first, ans.push_back(v[i].second);       if (sum == 0) break;     }     if (sum) {       cout <<  -1 ;       return 0;     }     cout << ans.size() << endl;     for (long long int i = 0; i < ans.size(); i++) cout << ans[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       char arr[n][n];       for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) {           cin >> arr[i][j];         }       }       vector<pair<int, int>> vec;       if (arr[0][1] == arr[1][0]) {         if (arr[0][1] ==  1 ) {           if (arr[n - 2][n - 1] !=  0 ) {             vec.push_back({n - 1, n});           }           if (arr[n - 1][n - 2] !=  0 ) {             vec.push_back({n, n - 1});           }         } else {           if (arr[0][1] ==  0 ) {             if (arr[n - 2][n - 1] !=  1 ) {               vec.push_back({n - 1, n});             }             if (arr[n - 1][n - 2] !=  1 ) {               vec.push_back({n, n - 1});             }           }         }       } else if (arr[n - 1][n - 2] == arr[n - 2][n - 1]) {         if (arr[n - 1][n - 2] ==  1 ) {           if (arr[0][1] !=  0 ) {             vec.push_back({1, 2});           }           if (arr[1][0] !=  0 ) {             vec.push_back({2, 1});           }         } else {           if (arr[n - 1][n - 2] ==  0 ) {             if (arr[1][0] !=  1 ) {               vec.push_back({2, 1});             }             if (arr[0][1] !=  1 ) {               vec.push_back({1, 2});             }           }         }       } else {         if (arr[n - 1][n - 2] !=  1 ) {           vec.push_back({n, n - 1});         }         if (arr[n - 2][n - 1] !=  1 ) {           vec.push_back({n - 1, n});         }         if (arr[0][1] !=  0 ) {           vec.push_back({1, 2});         }         if (arr[1][0] !=  0 ) {           vec.push_back({2, 1});         }       }       cout << vec.size() << endl;       for (auto x : vec) {         cout << x.first <<     << x.second << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   long long n, m, k;   long long x[200001], y[200001];   set<long long> sx, sy;   long long indx[200001], indy[200001];   long long find_indx(long long a) {     long long l = 0, r = n - 1;     long long ind = -1;     while (l <= r) {       long long mid = (l + r) / 2;       if (x[mid] >= a) {         ind = mid;         r = mid - 1;       } else         l = mid + 1;     }     assert(ind != -1);     return ind;   }   long long find_indy(long long b) {     long long l = 0, r = m - 1;     long long ind = -1;     while (l <= r) {       long long mid = (l + r) / 2;       if (y[mid] >= b) {         ind = mid;         r = mid - 1;       } else         l = mid + 1;     }     assert(ind != -1);     return ind;   }   void solv() {     long long res = 0;     cin >> n >> m >> k;     sx.clear();     sy.clear();     for (long long i = 0; i < n; i++) {       indx[i] = 0;       cin >> x[i];       sx.insert(x[i]);     }     for (long long i = 0; i < m; i++) {       indy[i] = 0;       cin >> y[i];       sy.insert(y[i]);     }     vector<pair<long long, long long>> onx, ony;     for (long long i = 0; i < k; i++) {       long long a, b;       cin >> a >> b;       if (sx.count(a) && sy.count(b)) continue;       if (sx.count(a))         onx.push_back({a, b});       else         ony.push_back({b, a});     }     pair<long long, long long> las = {-1, -1};     long long cnt = 0;     sort(onx.begin(), onx.end());     sort(ony.begin(), ony.end());     for (long long i = 0; i < onx.size(); i++) {       long long xx = find_indx(onx[i].first), yy = find_indy(onx[i].second);       if (make_pair(xx, yy) != las)         cnt = 0, las = {xx, yy};       else         cnt++;       res += indy[yy] - cnt;       indy[yy]++;     }     las = {-1, -1}, cnt = 0;     for (long long i = 0; i < ony.size(); i++) {       long long xx = find_indx(ony[i].second), yy = find_indy(ony[i].first);       if (make_pair(xx, yy) != las)         cnt = 0, las = {xx, yy};       else         cnt++;       res += indx[xx] - cnt;       indx[xx]++;     }     cout << res <<   n ;   }   signed main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(false);     long long t;     cin >> t;     while (t--) solv();   }
#include <bits/stdc++.h>   using namespace std;   const int L = 2e5 + 7;   long long out[L], a[L], counter[1000001], szs = 900;   struct node {     long long l, r, q;   };   bool comp(node x, node y) {     long long xx = x.l / szs, yy = y.l / szs;     if (xx == yy) return x.r < y.r;     return x.l < y.l;   }   long long ans = 0;   void add(int idx) {     counter[a[idx]]++;     ans += ((counter[a[idx]]) * (counter[a[idx]]) * a[idx] -             (counter[a[idx]] - 1) * (counter[a[idx]] - 1) * a[idx]);   }   void remove(int idx) {     counter[a[idx]]--;     ans += ((counter[a[idx]]) * (counter[a[idx]]) * a[idx] -             (counter[a[idx]] + 1) * (counter[a[idx]] + 1) * a[idx]);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int curl, curr, n, m, cur = 0, l, r, b, k;     cin >> n >> m;     for (int i = 0; i < n; ++i) cin >> a[i];     std::vector<node> v(m);     node temp;     for (int i = 0; i < m; ++i) {       cin >> l >> r;       l--, r--;       v[i].l = l, v[i].r = r, v[i].q = i;     }     sort(v.begin(), v.end(), comp);     l = 0, r = -1;     for (int i = 0; i < m; ++i) {       curl = v[i].l, curr = v[i].r;       while (r < curr) {         ++r;         add(r);       }       while (r > curr) {         remove(r);         --r;       }       while (l < curl) {         remove(l);         l++;       }       while (l > curl) {         --l;         add(l);       }       out[v[i].q] = ans;     }     for (int i = 0; i < m; ++i) cout << out[i] <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int i = 0; i < t; i++) {       int a, b, c, d, k;       cin >> a >> b >> c >> d >> k;       int x = (a + c - 1) / c;       int y = (b + d - 1) / d;       if (x + y <= k)         cout << x <<     << y << endl;       else         cout << -1 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long M = 1e9 + 7;   const double pi = acos(-1);   long long dp[1000006];   int main() {     long long n;     cin >> n;     long long a[n + 1];     memset(dp, 0, sizeof(dp));     dp[0] = 1;     for (long long i = 1; i <= n; i++) {       cin >> a[i];       vector<long long> v;       for (long long j = 1; j * j <= a[i]; j++) {         if (a[i] % j == 0) {           if (a[i] / j == j) {             v.push_back(j);           } else {             v.push_back(j);             v.push_back(a[i] / j);           }         }       }       sort(v.begin(), v.end(), greater<long long>());       for (long long j = 0; j < v.size(); j++) {         dp[v[j]] += dp[v[j] - 1];         dp[v[j]] %= M;       }     }     long long ans = 0;     for (long long i = 1; i <= n; i++) {       ans += dp[i];       ans %= M;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   const int N = 50;   const int M = 10000000;   const int MOD = 1e9 + 7;   string st[N];   int fa[N], lst[N], tot, cnt[N], ss[N];   int V[N], col[N], res;   int get_f(int t) {     if (fa[t] == t)       return t;     else       return get_f(fa[t]);   }   int ID[M], F[M], G[M];   int tmpA[M], tmpB[M], tmpC[M];   void FWT(int A[], int n) {     for (int i = 2; i <= n; i <<= 1)       for (int j = 0; j < n; j += i)         for (int k = j; k < (j + (i >> 1)); ++k)           A[k + (i >> 1)] = (A[k + (i >> 1)] + A[k]) % MOD;   }   void IFWT(int A[], int n) {     for (int i = 2; i <= n; i <<= 1)       for (int j = 0; j < n; j += i)         for (int k = j; k < (j + (i >> 1)); ++k)           A[k + (i >> 1)] = (A[k + (i >> 1)] - A[k] + MOD) % MOD;   }   int main() {     cin >> n;     for (int i = 0; i < n; ++i) cin >> st[i];     for (int i = 0; i < n; ++i) fa[i] = i;     for (int i = 0; i < n; ++i)       for (int j = 0; j < i; ++j)         if (st[i][j] ==  A ) fa[get_f(i)] = get_f(j);     for (int i = 0; i < n; ++i) cnt[get_f(i)]++;     tot = 0;     for (int i = 0; i < n; ++i)       if (fa[i] == i) {         if (cnt[i] == 1)           res++;         else           lst[i] = tot++;       }     for (int i = 0; i < n; ++i)       if (cnt[i] != 1)         for (int j = 0; j < i; ++j)           if (cnt[j] != 1)             if (st[i][j] ==  X ) {               int u = lst[get_f(i)], v = lst[get_f(j)];               if (u == v) {                 puts( -1 );                 exit(0);               }               if (u > v) swap(u, v);               V[u] |= 1 << v;               V[v] |= 1 << u;             }     for (int i = 0; i < tot; ++i) ID[1 << i] = i;     F[0] = 1;     for (int i = 1; i < (1 << tot); ++i) {       int x = i & (-i);       if (F[i ^ x] && !(V[ID[x]] & (i ^ x))) F[i] = 1;     }     G[0] = 1;     FWT(F, 1 << tot);     FWT(G, 1 << tot);     for (int i = 0;; ++i) {       int v = 0;       for (int j = 0; j < (1 << tot); ++j)         if (__builtin_popcount(((1 << tot) - 1) ^ j) & 1)           v = (v + MOD - G[j]) % MOD;         else           v = (v + G[j]) % MOD;       if (v) {         cout << i - 1 + n;         return 0;       }       for (int j = 0; j < (1 << tot); ++j) G[j] = 1ll * F[j] * G[j] % MOD;     }   }
#include <bits/stdc++.h>   using namespace std;   long long sol(long long a, long long b, long long h, long long w, int n,                 vector<long long> v) {     int p = 0;     long long th = h, tw = w;     for (; p < n; ++p) {       if (th < a)         th *= v[p];       else if (tw < b)         tw *= v[p];       else         break;     }     n = p;     v.resize(p);     auto solve = [&]() {       if (h >= a && w >= b) return true;       if (n < 20) {         for (int i = 0; i < 1 << n; ++i) {           th = h, tw = w;           for (int j = 0; j < n; ++j) {             if (i >> j & 1) {               if (th < a) th *= v[j];             } else {               if (tw < b) tw *= v[j];             }           }           if (th >= a && tw >= b) return true;         }         return false;       }       int m1 = n / 2, m2 = n - m1;       vector<pair<long long, long long>> x;       for (int i = 0; i < 1 << m1; ++i) {         th = 1, tw = 1;         for (int j = 0; j < m1; ++j) {           if (i >> j & 1) {             if (th * h < a) th *= v[j];           } else {             if (tw * w < b) tw *= v[j];           }         }         x.push_back({min(th, a), min(tw, b)});       }       sort(x.begin(), x.end());       vector<long long> mx(x.size());       for (int i = x.size() - 1; i >= 0; --i) {         mx[i] = x[i].second;         if (i + 1 < x.size()) mx[i] = max(mx[i], mx[i + 1]);       }       for (int i = 0; i < 1 << m2; ++i) {         th = 1, tw = 1;         for (int j = 0; j < m2; ++j) {           if (i >> j & 1) {             if (th * h < a) th *= v[m1 + j];           } else {             if (tw * w < b) tw *= v[m1 + j];           }         }         long long val = (a + h * th - 1) / (h * th);         auto it = lower_bound(x.begin(), x.end(), make_pair(val, 0LL));         if (it != x.end()) {           if (mx[it - x.begin()] * min(tw, b) * w >= b) return true;         }       }       return false;     };     int l = 0, r = p;     while (l < r) {       n = (l + r) >> 1;       if (solve())         r = n;       else         l = n + 1;     }     n = l;     if (solve()) return n;     return 1 << 30;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0);     long long a, b, h, w, n;     cin >> a >> b >> h >> w >> n;     vector<long long> v(n);     for (auto &i : v) cin >> i;     sort(v.begin(), v.end());     reverse(v.begin(), v.end());     int ans = min(sol(a, b, h, w, n, v), sol(b, a, h, w, n, v));     if (ans == 1 << 30) ans = -1;     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 10;   class event {    public:     long long t;     int id, type;     event() {}     event(long long a, int b, int c) {       t = a;       id = b;       type = c;     }     bool operator<(const event& other) const {       if (t != other.t) return other.t < t;       if (type != other.type) return other.type < type;       return other.id < id;     }   };   int n, p;   long long A[MAXN];   priority_queue<event> pq;   set<int> hold, qu;   long long last;   int main() {     scanf( %d %d , &n, &p);     for (int i = 1; i <= n; i++) {       scanf( %lld , &A[i]);       pq.push(event(A[i], i, 2));     }     while (!pq.empty()) {       event top = pq.top();       pq.pop();       if (top.type == 2) {         if (qu.empty()) {           qu.insert(top.id);           pq.push(event(top.t + p, top.id, 1));           last = top.t + p;         } else if (*qu.begin() > top.id) {           qu.insert(top.id);           pq.push(event(last + p, top.id, 1));           last = last + p;         } else {           hold.insert(top.id);         }       } else {         A[top.id] = top.t;         qu.erase(top.id);         set<int>::iterator lb = hold.lower_bound(top.id);         if (lb != hold.end()) {           pq.push(event(top.t, *lb, 2));           hold.erase(lb);         }       }     }     for (int i = 1; i <= n; i++) printf( %lld  , A[i]);     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   static const int MAXN = 1e5 + 10;   int n, k;   int ans;   int data[MAXN];   int has[MAXN];   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; ++i) {       scanf( %d , &data[i]);       ++has[data[i] % 10];       ans += data[i] / 10;     }     for (int i = 1; i < 10; ++i) {       int s = min(has[10 - i], k / i);       k -= i * s;       ans += s;       if (s != has[10 - i]) break;     }     ans += min(10 * n - ans, k / 10);     printf( %d , ans);   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   long long sum1[maxn];   long long sum2[maxn];   long long a[maxn];   long long pos[maxn];   int n;   long long lowbit(long long x) { return x & -x; }   void add(long long *sum, long long x, long long v) {     while (x <= n) {       sum[x] += v;       x += lowbit(x);     }   }   long long query(long long *sum, long long x) {     long long res = 0;     while (x > 0) {       res += sum[x];       x -= lowbit(x);     }     return res;   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       pos[a[i]] = i;     }     long long ans1 = 0;     for (int i = 1; i <= n; i++) {       ans1 += i - 1 - query(sum1, pos[i]);       add(sum1, pos[i], 1);       add(sum2, pos[i], pos[i]);       int mid, l = 1, r = n;       while (l <= r) {         mid = (l + r) >> 1;         if (query(sum1, mid) * 2 <= i)           l = mid + 1;         else           r = mid - 1;       }       long long ans2 = 0;       long long cnt = query(sum1, mid);       long long sum = query(sum2, mid);       ans2 += mid * cnt - sum - cnt * (cnt - 1) / 2;       cnt = i - cnt;       sum = query(sum2, n) - sum;       ans2 += sum - cnt * (mid + 1) - cnt * (cnt - 1) / 2;       cout << ans1 + ans2 <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n;     int h[100000], max[100001];     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> h[i];     }     max[n] = h[n - 1] - 1;     for (int i = n - 1; i >= 0; --i) {       if (h[i] > max[i + 1])         max[i] = h[i];       else         max[i] = max[i + 1];     }     for (int i = 0; i < n; ++i) {       if (max[i + 1] >= h[i])         cout << max[i + 1] - h[i] + 1 <<    ;       else         cout << 0 <<    ;     }     cout << endl;     return 0;   }
#include <bits/stdc++.h>   int const N = 1100;   int n, m, ans, ans1;   int map[N][N];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) map[i][j] = -1;     for (int i = 1; i <= n; ++i) {       int x;       scanf( %d , &x);       for (int j = 1; j <= x; ++j)         if (map[i][j] != 0)           map[i][j] = 1;         else {           printf( %d n , 0);           return 0;         }       if (map[i][x + 1] == 1) {         printf( %d n , 0);         return 0;       }       map[i][x + 1] = 0;     }     for (int i = 1; i <= m; ++i) {       int x;       scanf( %d , &x);       for (int j = 1; j <= x; ++j)         if (map[j][i] != 0)           map[j][i] = 1;         else {           printf( %d n , 0);           return 0;         }       if (map[x + 1][i] == 1) {         printf( %d n , 0);         return 0;       }       map[x + 1][i] = 0;     }     ans = 0;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j)         if (map[i][j] == -1) ans++;     ans1 = 1;     for (int i = 1; i <= ans; ++i) ans1 = (ans1 * 2) % 1000000007;     printf( %d n , ans1);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int x, y;   int ans = 10000000;   int bar[2001];   int dp[1002][1002][2];   string str[2001];   int dfs(int si) {     int res = 10000000;     for (int i = 0; i < m - si; i++) {       int rr = 0;       for (int j = 0; j < m; j++) {         if (j >= i && j < i + si)           rr += n - bar[j];         else           rr += bar[j];       }       res = min(rr, res);     }     return res;   }   int main(void) {     for (int i = 0; i <= 1001; i++) {       for (int j = 0; j <= 1001; j++) {         dp[i][j][0] = dp[i][j][1] = 10000000;       }     }     scanf( %d%d%d%d , &n, &m, &x, &y);     for (int i = 0; i < n; i++) {       cin >> str[i];       for (int j = 0; j < m; j++) {         if (str[i][j] ==  # ) bar[j]++;       }     }     dp[0][0][0] = 0;     dp[0][0][1] = 0;     for (int i = 0; i < m; i++) {       for (int j = 1; j <= y; j++) {         if (j >= x) dp[i + 1][1][0] = min(dp[i + 1][1][0], dp[i][j][1] + bar[i]);         if (j >= x)           dp[i + 1][1][1] = min(dp[i + 1][1][1], dp[i][j][0] + n - bar[i]);         dp[i + 1][j][0] = min(dp[i + 1][j][0], dp[i][j - 1][0] + bar[i]);         dp[i + 1][j][1] = min(dp[i + 1][j][1], dp[i][j - 1][1] + n - bar[i]);       }     }     for (int i = x; i <= y; i++) ans = min(ans, min(dp[m][i][1], dp[m][i][0]));     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:60000000 )   using namespace std;   const long double eps = 1e-9;   const int inf = (1 << 30) - 1;   const long long inf64 = ((long long)1 << 62) - 1;   const long double pi = 3.1415926535897932384626433832795;   template <class T>   T sqr(T x) {     return x * x;   }   int n, m;   long long w[300300];   long long t[300300];   vector<pair<pair<int, int>, int> > Q;   long long ans[300300];   int main() {     memset(w, 0, sizeof(w));     scanf( %d , &n);     for (int i = 0; i < (int)(n); i++) {       int x;       scanf( %d , &x);       w[i] = x;     }     scanf( %d , &m);     Q.resize(m);     for (int i = 0; i < (int)(m); i++) {       int a, b;       scanf( %d%d , &a, &b);       a--;       Q[i] = make_pair(make_pair(b, a), i);     }     sort(Q.begin(), Q.end());     int cur = 0;     for (int i = 0; i < (int)(Q.size()); i++) {       int a = Q[i].first.second;       int b = Q[i].first.first;       int num = Q[i].second;       if (b > 1000) {         ans[num] = 0;         while (a < n) {           ans[num] += w[a];           a += b;         }       } else {         if (b > cur) {           cur = b;           memset(t, 0, sizeof(t));           for (int i = n - 1; i >= 0; i--) {             t[i] = w[i];             if (i + cur < n) t[i] += t[i + cur];           }         }         if (a < n)           ans[num] = t[a];         else           ans[num] = 0;       }     }     for (int i = 0; i < (int)(m); i++) printf( %I64d n , ans[i]);     return 0;   }