malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int nmax = 52, inf = int(1e9), mod = int(1e9) + 7;   int n, K;   int dp[2][nmax][nmax], ways[2][nmax][nmax];   int comb[nmax][nmax];   int t[2];   int main() {     cin >> n >> K;     int w;     for (int i = 0; i < (int)n; ++i) {       cin >> w;       t[w != 50]++;     }     for (int k = 0; k < (int)2; ++k) {       for (int i = 0; i < (int)nmax; ++i) {         for (int j = 0; j < (int)nmax; ++j) {           dp[k][i][j] = inf;         }       }     }     comb[0][0] = 1;     for (int i = 1; i < nmax; i++) {       comb[i][0] = comb[i][i] = 1;       for (int j = 1; j < i; ++j) {         comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;       }     }     dp[0][0][0] = 0;     ways[0][0][0] = 1;     int side = 0;     for (int moves = 1;; moves++, side ^= 1) {       int cSide = side ^ 1;       if (moves == 1) {         for (int i = 0; i <= t[0]; ++i) {           for (int j = 0; j <= t[1]; ++j) {             if ((i > 0 || j > 0) && i * 50 + j * 100 <= K) {               dp[cSide][i][j] = 1;               ways[cSide][i][j] = 1ll * comb[t[0]][i] * comb[t[1]][j] % mod;             }           }         }         continue;       }       bool ok = false;       for (int i = 0; i <= t[0]; ++i) {         for (int j = 0; j <= t[1]; ++j) {           if ((!i && !j) || (i == t[0] && j == t[1]) || dp[side][i][j] == inf)             continue;           int oi = t[0] - i;           int oj = t[1] - j;           for (int k = 0; k <= i; ++k) {             for (int w = 0; w <= j; ++w) {               if (k + w > 0 && k * 50 + w * 100 <= K) {                 if (dp[cSide][oi + k][oj + w] > moves) {                   dp[cSide][oi + k][oj + w] = moves;                   ways[cSide][oi + k][oj + w] =                       (1ll * ways[side][i][j] * comb[i][k] % mod) * 1ll *                       comb[j][w] % mod;                   ok = true;                 } else if (dp[cSide][oi + k][oj + w] == moves) {                   ways[cSide][oi + k][oj + w] +=                       (1ll * ways[side][i][j] * comb[i][k] % mod) * 1ll *                       comb[j][w] % mod;                   ways[cSide][oi + k][oj + w] %= mod;                 }               }             }           }         }       }       if (!ok) break;     }     if (dp[1][t[0]][t[1]] == inf) {       cout <<  -1 n0 ;     } else {       cout << dp[1][t[0]][t[1]] <<   n  << ways[1][t[0]][t[1]];     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, x, y;     map<long long, long long> mx, my;     map<tuple<long long, long long>, long long> mt;     scanf( %lld , &n);     for (int i = 0; i < n; i++) {       scanf( %lld%lld , &x, &y);       tuple<long long, long long> a = make_tuple(x, y);       mx[x]++;       my[y]++;       mt[a]++;     }     long long ans = 0;     for (auto a : mx) {       long long b = a.second;       ans += b * (b - 1) / 2;     }     for (auto a : my) {       long long b = a.second;       ans += b * (b - 1) / 2;     }     for (auto a : mt) {       long long b = a.second;       ans -= b * (b - 1) / 2;     }     printf( %lld n , ans);   }
#include <bits/stdc++.h>   using namespace std;   string str;   void push(stack<pair<bool, int>>& stk, int offset) {     if (offset == 0) return;     pair<bool, int> cur;     if (stk.size() && stk.top().first)       cur = stk.top(), stk.pop(), stk.push({1, cur.second + offset});     else       stk.push({1, offset});   }   bool fix_stk(stack<pair<bool, int>>& stk) {     int offset = 0;     while (stk.size() && (stk.top().first || stk.top().second + offset < 0))       offset += stk.top().second * stk.top().first, stk.pop();     if (stk.size() == 0) return false;     pair<bool, int> cur = {0, stk.top().second + offset};     stk.pop();     push(stk, offset);     stk.push(cur);     return true;   }   bool operation(stack<pair<bool, int>>& stk, int idx) {     pair<bool, int> cur = {0, -1};     if (str[idx] ==  ? ) {       if (fix_stk(stk) == false) return false;       cur = stk.top();       stk.pop();       push(stk, 1);       stk.push({0, cur.second + 1});       if (cur.second - 1 >= 0) stk.push({0, cur.second - 1});     } else       push(stk, (str[idx] ==  ( ) - (str[idx] ==  ) ));     return true;   }   int solve(int idx) {     int ret = 0;     stack<pair<bool, int>> stk;     stk.push({0, 0});     while ((idx < str.size()) && stk.size()) {       if (operation(stk, idx) == false) return ret;       if (fix_stk(stk) == false) return ret;       ret += stk.top().second == 0;       idx++;     }     return ret;   }   int mine() {     int ret = 0;     for (int i = 0; i < str.size(); i++) ret += solve(i);     return ret;   }   int main() {     cin >> str;     cout << mine() << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int cp[1000010], n;   int dfs(int first) {     int second = 0;     for (int i = 1;; i++)       if ((1 << i) - 1 >= first) {         return (1 << i) - 1 ^ first;       }   }   int main() {     scanf( %d , &n);     for (int i = n; i >= 1; i--)       if (!cp[i]) {         cp[i] = dfs(i);         cp[cp[i]] = i;       }     long long ans = 0;     for (int i = n; i >= 0; i--) {       ans += i ^ cp[i];     }     cout << ans << endl;     for (int i = 0; i <= n; i++) {       printf( %d  , cp[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, c, b;       cin >> n >> c >> b;       int a[n];       for (int i = 0; i < n; i++) {         a[i] = i + 1;       }       if (n - 2 < c + b || abs(c - b) > 1) {         cout <<  -1 ;       } else {         if (c == b) {           for (int i = 1; i <= 2 * c; i += 2) {             swap(a[i], a[i + 1]);           }         } else if (c < b) {           int j = b;           for (int i = 0; j > 0; i += 2) {             swap(a[i], a[i + 1]);             j--;           }         } else if (c > b) {           int j = c;           for (int i = n - 1; j > 0; i = i - 2) {             swap(a[i], a[i - 1]);             j--;           }         }         for (int i = 0; i < n; i++) {           cout << a[i] <<    ;         }       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int ar[50][50];   int dp[51][51];   bool leaf[50];   vector<int> adj[50];   vector<int> leafs;   int bad;   vector<int> pos;   int go(int rem, int now) {     if (rem == 0) {       return 0;     }     if (dp[rem][now] != -1) {       return dp[rem][now];     }     int low = 0;     int high = 125000;     while (low < high) {       int mid = (low + high + 1) / 2;       int temp = rem;       for (int i = 0; i < n; i++) {         if (!leaf[i] || i == now) {           continue;         }         int cur = 0;         while (temp > 0 && ar[now][i] + go(rem - cur - 1, i) >= mid) {           cur++;           temp--;         }       }       if (temp == 0) {         low = mid;       } else {         high = mid - 1;       }     }     dp[rem][now] = low;     return low;   }   void dfs(int now, int from) {     if (leaf[now]) {       leafs.push_back(now);     }     for (int i = 0; i < pos.size(); i++) {       if (now == pos[i]) {         bad++;       }     }     for (int i = 0; i < adj[now].size(); i++) {       int to = adj[now][i];       if (to != from) {         dfs(to, now);       }     }   }   int main() {     std::ios::sync_with_stdio(false);     cin.tie(0);     cin >> n;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (i == j) {           ar[i][j] = 0;         } else {           ar[i][j] = 25500000;         }       }     }     int maxw = 0;     for (int i = 1; i < n; i++) {       int a, b, w;       cin >> a >> b >> w;       a--;       b--;       adj[a].push_back(b);       adj[b].push_back(a);       ar[a][b] = w;       ar[b][a] = w;       maxw = max(maxw, w);     }     assert(maxw <= 50);     for (int k = 0; k < n; k++) {       for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) {           ar[i][j] = min(ar[i][j], ar[i][k] + ar[k][j]);         }       }     }     for (int i = 0; i <= 50; i++) {       for (int j = 0; j <= 50; j++) {         dp[i][j] = -1;       }     }     for (int i = 0; i < n; i++) {       leaf[i] = adj[i].size() == 1;     }     int start, m;     cin >> start >> m;     start--;     for (int i = 0; i < m; i++) {       int x;       cin >> x;       x--;       pos.push_back(x);     }     int ans = 125000;     if (n == 1) {       assert(false);       ans = 0;     }     for (int i = 0; i < adj[start].size(); i++) {       leafs.clear();       leafs.resize(0);       bad = 0;       dfs(adj[start][i], start);       if (bad == 0) {         continue;       }       int low = 0;       int high = ans + 2;       while (low < high) {         int mid = (low + high + 1) / 2;         int temp = bad;         for (int z = 0; z < leafs.size(); z++) {           int cl = leafs[z];           if (cl == start) {             continue;           }           int cur = 0;           while (temp > 0 && ar[start][cl] + go(m - cur - 1, cl) >= mid) {             cur++;             temp--;           }         }         if (temp == 0) {           low = mid;         } else {           high = mid - 1;         }       }       ans = min(ans, low);     }     assert(ans != 125000 && ans != 0);     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, k, a[100001][6], ans[6];   multiset<int> st[6];   long long sum() {     long long ret = 0;     for (int i = 1; i <= m; i++) ret += *st[i].rbegin();     return ret;   }   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= n; i++)       for (int t = 1; t <= m; t++) scanf( %d , &a[i][t]);     long long curr = 0;     int res = 0;     for (int i = 1, j = 1; i <= n; i++) {       for (int t = 1; t <= m; t++) st[t].insert(a[i][t]);       while (j <= i and sum() > k) {         for (int t = 1; t <= m; t++) st[t].erase(st[t].find(a[j][t]));         j++;       }       if (i - j + 1 > res) {         for (int t = 1; t <= m; t++) ans[t] = *st[t].rbegin();         res = i - j + 1;       }     }     for (int t = 1; t <= m; t++) printf( %d  , ans[t]);   }
#include <bits/stdc++.h>   using namespace std;   void think() {     long long n;     cin >> n;     vector<long long> a(n);     for (long long i = 0; i < n; i++) {       cin >> a[i];     }     long long ans = 0;     unordered_map<long long, long long> mp;     mp[a[0]]++;     for (long long i = 1; i < n; i++) {       long long x = a[i];       a[i] += a[i - 1];       if (mp.find(a[i]) != mp.end() || a[i] == 0) {         ans++;         mp.clear();         a[i] = x;         mp[a[i]]++;       } else {         mp[a[i]]++;       }     }     cout << ans <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long testcase = 1;     while (testcase--) {       think();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int M = 11, L = 11, N = 1002;   const int Mod = 1e9 + 9;   char a[L], to[99];   int n;   int dp[2][M * L][N], ch[M * L][4], fail[M * L];   int len[N];   int ans = 0;   inline void mo(int& x) { x = (x >= Mod ? x - Mod : x); }   struct AC_automaton {     int cnt;     inline void init() {       cnt = 0;       to[ A ] = 0, to[ C ] = 1, to[ G ] = 2, to[ T ] = 3;     }     inline void insert() {       int l = strlen(a + 1), now = 0;       for (int i = 1; i <= l; i++) {         if (!ch[now][to[a[i]]]) ch[now][to[a[i]]] = ++cnt;         now = ch[now][to[a[i]]];       }       len[now] = max(len[now], l);     }     inline void build() {       queue<int> Q;       for (int i = 0; i < 4; i++)         if (ch[0][i]) Q.push(ch[0][i]);       while (!Q.empty()) {         int x = Q.front();         Q.pop();         len[x] = max(len[x], len[fail[x]]);         for (int i = 0; i < 4; i++) {           if (ch[x][i])             fail[ch[x][i]] = ch[fail[x]][i], Q.push(ch[x][i]);           else             ch[x][i] = ch[fail[x]][i];         }       }     }     inline void work() {       dp[0][0][1] = 1;       for (int i = 1; i <= n; i++) {         memset(dp[i & 1], 0, sizeof(dp[i & 1]));         for (int j = 0; j <= cnt; j++)           for (int k = 0; k <= i; k++) {             if (dp[!(i & 1)][j][k]) {               for (int t = 0; t < 4; t++) {                 if (len[ch[j][t]] + k > i)                   mo(dp[i & 1][ch[j][t]][i + 1] += dp[!(i & 1)][j][k]);                 else                   mo(dp[i & 1][ch[j][t]][k] += dp[!(i & 1)][j][k]);               }             }           }       }       for (int j = 0; j <= cnt; j++) mo(ans += dp[n & 1][j][n + 1]);     }   } T;   int main() {     int m;     scanf( %d%d , &n, &m);     T.init();     for (int i = 1; i <= m; i++) {       scanf( %s , a + 1);       T.insert();     }     T.build();     T.work();     printf( %d , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, y = 0;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) y = 1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 )       x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();     return y ? -x : x;   }   template <typename T>   inline T read() {     T x = 0;     int y = 0;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) y = 1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 )       x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();     return y ? -x : x;   }   char s[40];   long long pw[40] = {-0x3f3f3f3f, 1}, f[40][40];   bool g[40][40];   signed main() {     int n = read();     scanf( %s , s + 1);     for (register int i = 2; i <= n; ++i) pw[i] = pw[i - 1] * 10;     memset(f, ~0x3f, sizeof f);     f[(n << 1) + 1][0] = 0;     for (register int i = n << 1; i; --i)       for (register int j = 0; j <= min((n << 1) - i + 1, n); ++j) {         f[i][j] = max(f[i + 1][j - 1] + pw[j] * (s[i] -  0 ),                       f[i + 1][j] + pw[(n << 1) - i + 1 - j] * (s[i] -  0 ));         g[i][j] = f[i + 1][j - 1] + pw[j] * (s[i] -  0 ) >                   f[i + 1][j] + pw[(n << 1) - i + 1 - j] * (s[i] -  0 );       }     for (register int i = 1, j = n; i <= n << 1; ++i)       putchar(g[i][j] ?  H  :  M ), j -= g[i][j];     putchar(  n );   }
#include <bits/stdc++.h>   using namespace std;   int dp[30006][205][4];   int a[30006];   int n, k;   int main() {     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 0; i <= n; i++)       for (int j = 0; j <= k; j++)         for (int t = 0; t < 4; t++) dp[i][j][t] = -1e9 - 1e7;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= k; j++) {         int mul = 2 - (j == 1 || j == k);         dp[i][j][0] =             max(dp[i - 1][j][0], max(dp[i - 1][j - 1][1], dp[i - 1][j - 1][3])) +             a[i] * mul;         if (j == 1) dp[i][j][0] = max(dp[i][j][0], a[i] * mul);         dp[i][j][1] =             max(dp[i - 1][j][1], max(dp[i - 1][j - 1][0], dp[i - 1][j - 1][2])) -             a[i] * mul;         if (j == 1) dp[i][j][1] = max(dp[i][j][1], -a[i] * mul);         dp[i][j][2] =             max(dp[i - 1][j][2], max(dp[i - 1][j - 1][2], dp[i - 1][j - 1][0]));         dp[i][j][3] =             max(dp[i - 1][j][3], max(dp[i - 1][j - 1][3], dp[i - 1][j - 1][1]));       }     int res = -1e9 - 1e7;     for (int i = k; i <= n; i++) res = max(res, max(dp[i][k][0], dp[i][k][1]));     cout << res;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int n, m, q, i, s, j, k, u, dis[N][350], lazy[350], W[N];   long long sum[N], arr[N], x, Y[350];   char c;   vector<int> G[N], F[N];   bool H[N], is[N][350];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m >> q;     for (i = 1; i <= n; i++) cin >> arr[i];     for (i = 1; i <= m; i++) {       cin >> s;       G[i].resize(s);       if (s >= 350) H[i] = 1, u++, W[i] = u - 1;       for (j = 0; j < s; j++) {         cin >> G[i][j];         if (H[i]) is[G[i][j]][u - 1] = 1, sum[u - 1] += arr[G[i][j]];       }     }     for (i = 1; i <= m; i++) {       for (int j : G[i]) {         for (int k = 0; k <= 340; k++)           if (is[j][k]) dis[i][k]++;       }     }     while (q--) {       cin >> c >> k;       if (c ==  ? ) {         if (H[k] == 0) {           long long r = 0;           for (int j : G[k]) r += 1ll * arr[j];           for (i = 0; i < 340; i++) r += Y[i] * dis[k][i];           cout << r <<   n ;         } else           cout << sum[W[k]] <<   n ;       } else {         cin >> x;         if (!H[k]) {           for (int j : G[k]) arr[j] += x;           for (i = 0; i < 340; i++) sum[i] += x * dis[k][i];         } else {           for (i = 0; i < 340; i++) sum[i] += 1ll * x * dis[k][i];           Y[W[k]] += x;         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   long long odd[200005], even[200005];   int main() {     long long n, t, x, y;     cin >> n;     long long ara[n + 3];     long long sum = 0, cnt = 0;     for (int i = 1; i <= n; i++) {       cin >> ara[i];       sum += ara[i];       if (i % 2 == 0) {         even[i] = even[i - 1] + ara[i];         odd[i] = odd[i - 1];       } else {         odd[i] = odd[i - 1] + ara[i];         even[i] = even[i - 1];       }     }     for (int i = 1; i <= n; i++) {       long long one = odd[i - 1];       one += (even[n] - even[i]);       long long two = sum - (one + ara[i]);       if (one == two) cnt++;     }     cout << cnt << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cout.tie(0);     cin.tie(0);     string s;     cin >> s;     long long dp[s.length() + 1], mod = 1e9 + 7, f = 0;     dp[0] = 1;     dp[1] = 1;     if (s[0] ==  m  || s[0] ==  w ) f = 1;     for (long long i = 2; i <= s.length(); i++) {       if (s[i - 1] ==  m  || s[i - 1] ==  w )         f = 1;       else if ((s[i - 1] ==  n  || s[i - 1] ==  u ) && (s[i - 1] == s[i - 2]))         dp[i] = (dp[i - 2] + dp[i - 1]) % mod;       else         dp[i] = dp[i - 1];     }     if (f)       cout << 0 << endl;     else       cout << dp[s.length()] << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int r1, c1, r2, c2;     cin >> r1 >> c1 >> r2 >> c2;     if (r1 == r2 || c1 == c2)       cout << 1;     else       cout << 2;     cout <<    ;     if ((r1 + c1) % 2 != (r2 + c2) % 2)       cout << 0;     else {       if (r1 + c1 == r2 + c2 || r1 - c1 == r2 - c2)         cout << 1;       else {         cout << 2;       }     }     cout <<    ;     cout << max(abs(r1 - r2), abs(c1 - c2)) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s1, s2, s3;     cin >> s1 >> s2 >> s3;     int i;     s1 = s1.append(s2);     sort(s1.begin(), s1.end());     sort(s3.begin(), s3.end());     if (s1.length() != s3.length()) {       cout <<  NO ;       return 0;     } else {       for (i = 0; i < s1.length(); i++) {         if (s1[i] != s3[i]) {           cout <<  NO ;           return 0;         }       }       cout <<  YES ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 55;   char str[N][N];   int vis[N];   int n, m;   long long msk[N];   int main() {     scanf( %d %d , &n, &m);     for (int i = 0; i < int(n); i++) scanf( %s , str[i]);     for (int i = 0; i < int(n); i++)       for (int j = 0; j < int(m); j++)         if (str[i][j] ==  # ) msk[i] |= (1LL << j);     for (int i = 0; i < int(n); i++) {       if (vis[i]) continue;       vector<int> v;       for (int j = int(i); j <= int(n - 1); j++) {         if (msk[i] == msk[j])           v.push_back(j);         else if (msk[i] & msk[j]) {           puts( No );           return 0;         }       }       for (int nxt : v) vis[nxt] = true;     }     puts( YES );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int i, n;     cin >> n;     char a[n];     for (i = 0; i <= n - 1; i++) {       cin >> a[i];     }     if (a[0] ==  S  && a[n - 1] ==  F )       cout <<  YES ;     else       cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct ac {     int x, y, z;   } p[3000];   int mo[3000];   vector<pair<int, int> > pr;   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d%d%d , &p[i].x, &p[i].y, &p[i].z);     }     for (int i = 1; i <= n; ++i) {       int d = 1e9, f = 0;       if (mo[i] == 1) continue;       for (int j = 1; j <= n; ++j) {         if (mo[j] == 1 || i == j) continue;         int td =             abs(p[i].x - p[j].x) + abs(p[j].y - p[i].y) + abs(p[j].z - p[i].z);         if (td < d) {           f = j;           d = td;         }       }       mo[i] = mo[f] = 1;       cout << i <<     << f << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long K = 1000000007;   long long gcd(long long a, long long b, long long& x, long long& y) {     if (a == 0) {       x = 0;       y = 1;       return b;     }     long long x1, y1;     long long d = gcd(b % a, a, x1, y1);     x = y1 - (b / a) * x1;     y = x1;     return d;   }   int32_t main() {     long long n, m;     cin >> n >> m;     long long ans = n + 1 - m;     for (long long i = 0; i < m; i++) {       ans *= (2 * n + 2);       ans %= K;     }     long long x, y;     gcd(n + 1, K, x, y);     x *= ans;     x %= K;     if (x < 0) x += K;     cout << x << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 30;   int a[MAX_N], s[MAX_N];   int main() {     ios::sync_with_stdio(false);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       s[i] = a[i];     }     sort(s, s + n);     map<int, int> spos;     for (int i = 0; i < n; i++) {       spos[s[i]] = i;     }     for (int i = 0; i < n; i++) {       cout << s[(spos[a[i]] + 1) % n] <<    ;     }     cout << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t, n, l, r;     cin >> t;     while (t--) {       cin >> n;       int a[n];       for (int i = 0; i < n; i++) {         cin >> a[i];       }       for (int i = 0; i < n; i++) {         if (a[i] == 1) {           l = i;           break;         }       }       for (int i = n - 1; i >= 0; i--) {         if (a[i] == 1) {           r = i;           break;         }       }       int ans = 0;       for (int i = l; i < r + 1; i++) {         if (a[i] == 0) ans++;       }       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int SIZE = 640;   int h, w;   int matr[SIZE][SIZE];   int main() {     int q;     scanf( %d , &q);     for (int tt = 0; tt < q; tt++) {       scanf( %d%d , &h, &w);       for (int i = 0; i < h; i++)         for (int j = 0; j < w; j++) scanf( %d , &matr[i][j]);       int cut = h / 2;       int diffZero = 0;       int diffSwap = 0;       for (int j = 0; j < w; j++) {         diffZero += abs(matr[cut - 1][j] - matr[cut][j]);         diffSwap += abs(matr[0][j] - matr[h - 1][j]);       }       ((void)0);       printf(diffZero < diffSwap ?  NO n  :  YES n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const unsigned long long P = 239017, MaxN = 2100000, INF = 1000000000;   int a, arr[1000000], sum[1000000], have[1000000];   char s[1000000];   int main() {     scanf( %d%s , &a, s);     int len = strlen(s);     for (int i = 0; i < len; ++i) arr[i] = s[i] -  0 ;     sum[0] = 0;     for (int i = 1; i <= len; ++i) sum[i] = sum[i - 1] + arr[i - 1];     int S = 0;     for (int i = 1; i <= len; ++i)       for (int j = 1; j <= i; ++j) ++have[sum[i] - sum[j - 1]], ++S;     long long ans = 0;     for (int i = 1; i <= len; ++i)       for (int j = 1; j <= i; ++j) {         if ((sum[i] - sum[j - 1]) && a % (sum[i] - sum[j - 1]) == 0) {           if ((a / (sum[i] - sum[j - 1])) < 1000000)             ans += have[a / (sum[i] - sum[j - 1])];         }         if (a == 0 && (sum[i] - sum[j - 1]) == 0) ans += S;       }     printf( %I64d , ans);     return 0;   }