malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e12;   const int INF1 = 1e7;   const int maxn = 100000 + 10;   int n, m, k;   int fd, u, v, w;   struct node {     int to, cost, fd;   } a[maxn], b[maxn];   bool cmp(node f, node g) { return f.fd < g.fd; }   bool cmp1(node f, node g) { return f.fd > g.fd; }   int ecnt = 0, ecnt1 = 0;   long long dp[maxn * 10], dp1[maxn * 10];   int d[maxn], d1[maxn];   int mx = 0;   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= m; i++) {       scanf( %d%d%d%d , &fd, &u, &v, &w);       if (v == 0) {         a[ecnt].to = u, a[ecnt].cost = w, a[ecnt].fd = fd;         ecnt++;       }       if (u == 0) {         b[ecnt1].to = v, b[ecnt1].cost = w, b[ecnt1].fd = fd;         ecnt1++;       }       mx = max(mx, fd);     }     sort(a, a + ecnt, cmp);     sort(b, b + ecnt1, cmp1);     for (int i = 1; i <= n; i++) d[i] = INF1, d1[i] = INF1;     int cnt = 0;     int tmp = -1;     for (int i = 0; i < ecnt; i++) {       v = a[i].to;       if (d[v] == INF1) {         cnt++;       }       d[v] = min(d[v], a[i].cost);       if (cnt == n) {         tmp = i;         break;       }     }     if (tmp == -1) {       printf( -1 n );       return 0;     }     for (int i = 1; i <= n; i++) dp[a[tmp].fd] += d[i];     for (int i = tmp + 1; i < ecnt; i++) {       v = a[i].to;       dp[a[i].fd] = dp[a[i - 1].fd];       dp[a[i].fd] = dp[a[i].fd] - d[v];       d[v] = min(d[v], a[i].cost);       dp[a[i].fd] += d[v];     }     cnt = 0;     tmp = -1;     for (int i = 0; i < ecnt1; i++) {       v = b[i].to;       if (d1[v] == INF1) {         cnt++;       }       d1[v] = min(d1[v], b[i].cost);       if (cnt == n) {         tmp = i;         break;       }     }     if (tmp == -1) {       printf( -1 n );       return 0;     }     for (int i = 1; i <= n; i++) dp1[b[tmp].fd] += d1[i];     for (int i = tmp + 1; i < ecnt1; i++) {       v = b[i].to;       dp1[b[i].fd] = dp1[b[i - 1].fd];       dp1[b[i].fd] = dp1[b[i].fd] - d1[v];       d1[v] = min(d1[v], b[i].cost);       dp1[b[i].fd] += d1[v];     }     for (int i = 1; i <= mx; i++) {       if (dp[i] == 0) dp[i] = dp[i - 1];     }     for (int i = mx; i >= 1; i--) {       if (dp1[i] == 0) dp1[i] = dp1[i + 1];     }     long long ans = INF;     for (int i = 0; i < ecnt; i++) {       if (dp[a[i].fd] != 0) {         if (a[i].fd + k + 1 <= mx && dp1[a[i].fd + k + 1] != 0)           ans = min(ans, dp[a[i].fd] + dp1[a[i].fd + k + 1]);       }     }     if (ans == INF)       printf( -1 n );     else       printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[100005], s[100005], f[100005];   int main() {     int n, ans = 0;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     s[1] = 0, f[1] = 0;     for (int i = 2; i <= n; i++) {       int step = 0, t = i - 1;       while (a[t] < a[i] && t) {         step = max(step, s[t]);         t = f[t];       }       if (a[t] > a[i]) {         f[i] = t;         s[i] = step + 1;       } else {         f[i] = 0;         s[i] = 0;       }       ans = max(ans, s[i]);     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( O3 ,  unroll-loops ,  omit-frame-pointer ,  inline )   #pragma GCC option( arch=native ,  tune=native ,  no-zeroupper )   #pragma GCC target( avx ,  popcnt )   using namespace std;   const long long int INF = (long long int)1e17 + 5;   const unsigned long long int MOD = 1e8 + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     std::cout << std::fixed;     std::cout.precision(6);     int n;     cin >> n;     vector<pair<long long int, long long int>> v;     for (int i = 0; i < n; i++) {       long long int a;       cin >> a;       v.emplace_back(make_pair(a, 0));     }     for (int i = 0; i < n; i++) {       long long int a;       cin >> a;       v[i].second = a;     }     long long int mini = INF;     int pos = 0;     for (int j = 1; j < n - 1; j++) {       long long int minl = INF;       long long int minr = INF;       for (int i = j - 1; i >= 0; i--) {         if (v[i].first < v[j].first) minl = min(minl, v[i].second);       }       for (int i = j + 1; i < n; i++) {         if (v[i].first > v[j].first) minr = min(minr, v[i].second);       }       if (minl != INF && minr != INF) {         long long int l = v[j].second + minr + minl;         mini = min(mini, l);       }     }     if (mini == INF)       cout <<  -1 n ;     else       cout << mini <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 2;   vector<int> g[maxn];   vector<long long> c[maxn], acum[maxn];   int n, m, q, idx, comp[maxn];   long long d[maxn], dist[maxn];   map<pair<int, int>, long long> memo;   vector<int> cur;   void dfs(int node, int par) {     comp[node] = idx;     cur.push_back(node);     for (auto &adj : g[node])       if (adj != par) dfs(adj, node);   }   pair<int, long long> bfs(int node) {     queue<int> Q;     for (auto &v : cur) dist[v] = -1;     dist[node] = 0;     Q.push(node);     int last = -1;     while (!Q.empty()) {       node = Q.front();       Q.pop();       last = node;       for (auto &adj : g[node]) {         if (dist[adj] == -1) {           dist[adj] = dist[node] + 1;           Q.push(adj);         }       }     }     return {last, dist[last]};   }   void diameter(int node) {     auto a = bfs(node);     auto b = bfs(a.first);     d[idx] = b.second;     for (int i = 0; i < (int)cur.size(); i++) c[idx][i] = dist[cur[i]];     bfs(b.first);     for (int i = 0; i < (int)cur.size(); i++)       c[idx][i] = max(c[idx][i], dist[cur[i]]);     sort(c[idx].begin(), c[idx].end());     acum[idx][0] = c[idx][0];     for (int i = 1; i < (int)cur.size(); i++)       acum[idx][i] = acum[idx][i - 1] + c[idx][i];   }   void process(int node) {     idx++;     cur.clear();     dfs(node, 0);     c[idx].resize(cur.size());     acum[idx].resize(cur.size());     diameter(cur[0]);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout << setprecision(10) << fixed;     cin >> n >> m >> q;     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       g[x].push_back(y);       g[y].push_back(x);     }     for (int i = 1; i <= n; i++)       if (comp[i] == 0) process(i);     while (q--) {       int x, y;       cin >> x >> y;       x = comp[x];       y = comp[y];       if (x == y) {         cout << -1 <<   n ;         continue;       }       if ((long long)c[x].size() > (long long)c[y].size()) swap(x, y);       long long N = (long long)c[x].size(), M = (long long)c[y].size();       if (memo.find({x, y}) != memo.end()) {         cout << (1.0 * memo[pair<int, int>(x, y)]) / (1.0 * N * M) <<   n ;         continue;       }       long long ans = 0;       long long mx = max(d[x], d[y]);       for (auto &v : c[x]) {         long long lo = 0, hi = M - 1LL;         long long pos = -1;         while (lo <= hi) {           long long mid = (lo + hi) >> 1LL;           if (v + c[y][mid] + 1 <= mx) {             pos = mid;             lo = mid + 1;           } else             hi = mid - 1;         }         if (pos == -1) {           ans += acum[y][M - 1] + M * (v + 1);         } else {           ans += (pos + 1) * mx;           ans += acum[y][M - 1] - acum[y][pos] + (v + 1) * (M - pos - 1);         }       }       memo[pair<int, int>(x, y)] = ans;       memo[pair<int, int>(y, x)] = ans;       cout << (1.0 * ans) / (1.0 * N * M) <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > V;   int in[50];   int main() {     vector<string> S;     V.resize(30);     int n, ile;     cin >> n;     ile = n;     while (ile--) {       string a;       cin >> a;       S.push_back(a);     }     for (int i = 0; i < S.size(); i++) {       for (int j = i + 1; j < S.size(); j++) {         for (int k = 0; k < min(S[i].size(), S[j].size()); k++) {           if (S[i][k] == S[j][k]) {             if (k + 1 == S[j].size()) {               cout <<  Impossible ;               return 0;             }             continue;           }           V[S[i][k] -  a ].push_back(S[j][k] -  a );           in[S[j][k] -  a ]++;           break;         }       }     }     vector<int> kol;     queue<int> Q;     for (int i = 0; i < 26; i++)       if (in[i] == 0) Q.push(i);     while (!Q.empty()) {       int v = Q.front();       Q.pop();       kol.push_back(v);       for (auto sas : V[v]) {         in[sas]--;         if (in[sas] == 0) Q.push(sas);       }     }     if (kol.size() < 26) {       cout <<  Impossible ;       return 0;     }     for (auto c : kol) cout << (char(c +  a ));   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   inline int GetIdx(int l, int r) { return l + r | l != r; }   int seg[maxn << 2];   void update(int l, int r, int x, int d) {     if (l == r) {       seg[GetIdx(l, r)] = d;       return;     }     int mid = (l + r) >> 1;     if (x <= mid) {       update(l, mid, x, d);     } else {       update(mid + 1, r, x, d);     }     seg[GetIdx(l, r)] = min(seg[GetIdx(l, mid)], seg[GetIdx(mid + 1, r)]);   }   int query(int l, int r, int ua, int ub) {     if (ua <= l && ub >= r) {       return seg[GetIdx(l, r)];     }     int mid = (l + r) >> 1;     int res = 1 << 30;     if (ua <= mid) {       res = min(res, query(l, mid, ua, ub));     }     if (ub > mid) {       res = min(res, query(mid + 1, r, ua, ub));     }     return res;   }   vector<int> line[maxn];   pair<int, int> rook[maxn << 1], rec1[maxn << 1], rec2[maxn << 1];   bool ans[maxn << 1];   int main() {     int n, m, k, q;     while (~scanf( %d%d%d%d , &n, &m, &k, &q)) {       for (int i = 0; i < k; i++) {         scanf( %d%d , &rook[i].first, &rook[i].second);       }       for (int i = 0; i < q; i++) {         scanf( %d%d%d%d , &rec1[i].first, &rec1[i].second, &rec2[i].first,               &rec2[i].second);       }       for (int cas = 1; cas <= 2; cas++) {         memset(seg, 0, sizeof seg);         for (int i = 1; i <= n; i++) {           line[i].clear();         }         for (int i = 0; i < k; i++) {           line[rook[i].first].push_back(rook[i].second);         }         for (int i = 0; i < q; i++) {           line[rec2[i].first].push_back(~i);         }         for (int i = 1; i <= n; i++) {           for (int j = 0; j < line[i].size(); j++) {             int tmp = line[i][j];             if (tmp < 0) {               tmp = ~tmp;               if (query(1, m, rec1[tmp].second, rec2[tmp].second) >=                   rec1[tmp].first) {                 ans[tmp] = true;               }             } else {               update(1, m, tmp, i);             }           }         }         swap(n, m);         for (int i = 0; i < k; i++) {           swap(rook[i].first, rook[i].second);         }         for (int i = 0; i < q; i++) {           swap(rec1[i].first, rec1[i].second);           swap(rec2[i].first, rec2[i].second);         }       }       for (int i = 0; i < q; i++) {         puts(ans[i] ?  YES  :  NO );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int mod = 1e9 + 7;   int mat[2 * 100001][26];   int n, k;   int count3 = 0;   void find_most(int x, int y) {     int total = 0, mx = 0;     for (int i = 0; i < 26; i++) {       total += mat[x][i] + mat[y][i];       mx = max(mx, mat[x][i] + mat[y][i]);     }     count3 += total - mx;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       cin >> n >> k;       string s;       cin >> s;       for (int i = 0; i < k; i++) {         for (int j = 0; j < 26; j++) mat[i][j] = 0;       }       for (int i = 0; i < n; i++) {         mat[i % k][s[i] -  a ]++;       }       for (int i = 0; i < k; i++) {         find_most(i, k - i - 1);       }       cout << (count3) / 2;       count3 = 0;       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool vis[500009];   int siz[500009];   int rr[1005][1005];   char arr[1005][1005];   vector<int> d;   int n;   void dfs(int x, int y, int cn) {     if (x > n or y > n or x < 1 or y < 1 or rr[x][y] != 0) return;     rr[x][y] = cn;     siz[cn]++;     if (arr[x + 1][y] ==  . ) dfs(x + 1, y, cn);     if (arr[x][y + 1] ==  . ) dfs(x, y + 1, cn);     if (arr[x - 1][y] ==  . ) dfs(x - 1, y, cn);     if (arr[x][y - 1] ==  . ) dfs(x, y - 1, cn);   }   void rem(int i, int j, int k) {     for (int t = i; t < i + k; t++) {       if (arr[t][j] ==  . ) siz[rr[t][j]]--;     }   }   int count(int i, int j, int k) {     vector<int> v;     int e, ans = 0;     for (int y = i; y < i + k && j - 1 >= 1; y++) {       e = rr[y][j - 1];       if (!vis[e] && e) {         v.push_back(e);         vis[e] = 1;         ans += siz[e];       }     }     for (int y = j; y < j + k && i - 1 >= 1; y++) {       e = rr[i - 1][y];       if (!vis[e] && e) {         v.push_back(e);         vis[e] = 1;         ans += siz[e];       }     }     for (int y = i; y < i + k && j + k <= n; y++) {       e = rr[y][j + k];       if (!vis[e] && e) {         v.push_back(e);         vis[e] = 1;         ans += siz[e];       }     }     for (int y = j; y < j + k && i + k <= n; y++) {       e = rr[i + k][y];       if (!vis[e] && e) {         v.push_back(e);         vis[e] = 1;         ans += siz[e];       }     }     for (int i = 0; i < v.size(); i++) {       vis[v[i]] = 0;     }     return ans;   }   void add(int i, int j, int k) {     for (int t = i; t < i + k; t++) {       if (arr[t][j] ==  . ) siz[rr[t][j]]++;     }   }   int main() {     int k, e = 0, a = 0;     cin >> n >> k;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         cin >> arr[i][j];       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (arr[i][j] ==  .  && rr[i][j] == 0) dfs(i, j, ++e);       }     }     for (int i = 1; i <= n - k + 1; i++) {       for (int y = 1; y <= k; y++) {         rem(i, y, k);       }       a = count(i, 1, k);       d.push_back(a);       int last = 1;       for (int j = 2; j <= n - k + 1; j++) {         rem(i, j + k - 1, k);         add(i, j - 1, k);         last = j;         a = count(i, j, k);         d.push_back(a);       }       for (int y = last; y <= n; y++) {         add(i, y, k);       }     }     sort(d.begin(), d.end());     cout << d[d.size() - 1] + (k * k) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double pi =       3.14159265358979323846264338327950288419716939937510582097494459230;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, i, x;     cin >> n;     int vis[n + 4];     int cnt[n + 4];     memset(cnt, 0, sizeof(cnt));     memset(vis, 0, sizeof(vis));     for (i = 0; i < n; i++) {       cin >> x;       cnt[x]++;     }     for (i = 1; i <= n; i++) {       if (cnt[i] >= 3) {         vis[i - 1] = 1;         vis[i] = 1;         vis[i + 1] = 1;       } else {         int d = cnt[i], j;         for (j = i - 1; j <= i + 1; j++) {           if (d == 0) break;           if (vis[j] == 1)             continue;           else {             vis[j] = 1;             d--;           }         }       }     }     int ans = 0;     for (i = 1; i <= n; i++) {       if (cnt[i] == 0) continue;       ans++;       i = i + 2;     }     cout << ans <<     << count(vis, vis + n + 2, 1);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int x[100005], res[100005];   map<int, pair<int, int>> xhMap;   int main() {     memset(x, 0, sizeof(x));     memset(res, 0, sizeof(res));     int n = 0, a = 0, b = 0;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d%d , &a, &b);       x[i] = a;       xhMap[a] = make_pair(b, i);     }     sort(x, x + n);     for (int i = n - 1; i >= 0; --i) {       int tmp = x[i] + xhMap[x[i]].first - 1, idx = xhMap[x[i]].second;       int r = i + 1, num = 1;       while (r < n && x[r] <= tmp) {         num += res[xhMap[x[r]].second];         r += res[xhMap[x[r]].second];       }       res[idx] = num;     }     for (int i = 0; i < n; ++i) {       printf( %d  , res[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 4e5 + 10;   vector<int> tree[N];   int cnt[N];   int n, mx1, mx2;   bool centr[N], ans[N];   int dfs(int v, int p = -1) {     int u;     cnt[v] = 1;     for (int i = 0; i < tree[v].size(); i++) {       u = tree[v][i];       if (u == p) continue;       cnt[v] += dfs(u, v);     }     return cnt[v];   }   void find_centr(int v, int p = -1) {     int u;     bool ok = (n - cnt[v]) <= n / 2;     for (int i = 0; i < tree[v].size(); i++) {       u = tree[v][i];       if (u == p) continue;       find_centr(u, v);       if (cnt[u] > n / 2) ok = false;     }     if (ok) centr[v] = 1;     return;   }   void calc(int v, int p = -1, int mxC = -1) {     int u;     if (n - cnt[v] - mxC <= n / 2)       ans[v] = true;     else       return;     for (int i = 0; i < tree[v].size(); i++) {       u = tree[v][i];       if (u == p) continue;       calc(u, v, mxC);     }     return;   }   void solveC(int aim) {     for (int i = 1; i <= n; i++) cnt[i] = 0;     dfs(aim);     mx1 = -1, mx2 = -1;     int u = 0;     for (int i = 0; i < tree[aim].size(); i++) {       u = tree[aim][i];       if (cnt[u] > mx1) {         mx2 = mx1;         mx1 = cnt[u];       } else if (cnt[u] > mx2)         mx2 = cnt[u];     }     for (int i = 0; i < tree[aim].size(); i++) {       u = tree[aim][i];       if (cnt[u] == mx1 && (cnt[u] != n / 2 || n / 2 * 2 != n))         calc(u, aim, mx2);       else         calc(u, aim, mx1);     }   }   void solve() {     cin >> n;     int b, e;     for (int i = 0; i < n - 1; i++) {       scanf( %d %d n , &b, &e);       tree[b].push_back(e);       tree[e].push_back(b);     }     dfs(1);     find_centr(1);     for (int i = 1; i <= n; i++) {       if (centr[i]) solveC(i);     }     for (int i = 1; i <= n; i++)       if (centr[i] || ans[i])         printf( 1  );       else         printf( 0  );   }   int main() {     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long c;   vector<int> a;   int N;   long long C[65][65];   long long nCk(long long n, long long k) {     if (k > n) return 0;     return C[n][k];   }   long long f(int index, int onesNeeded, int tight) {     if (index == N) {       if (onesNeeded == 0)         return 1;       else         return 0;     }     if (a[index] == 0) {       return f(index + 1, onesNeeded, tight);     } else {       long long ans = nCk(N - index - 1, onesNeeded);       if (onesNeeded >= 1) ans += f(index + 1, onesNeeded - 1, tight);       return ans;     }   }   int brute(int n) {     int ans = 0;     for (int i = 1; i <= n; i++) {       int t = i, cnt = 0;       while (t) {         if (t % 2 == 1) cnt++;         t /= 2;       }       if (cnt == 5) ans++;     }     return ans;   }   int main() {     for (int i = 0; i <= 60; i++) C[i][0] = 1, C[i][i] = 1;     for (int n = 1; n <= 60; n++) {       for (int r = 1; r <= n; r++) {         C[n][r] = C[n - 1][r] + C[n - 1][r - 1];       }     }     long long n, t;     cin >> n >> t;     n++;     int ok = 0;     if ((t & (t - 1)) == 0) ok = 1;     while (t) {       t /= 2;       c++;     };     if (!ok) {       cout << 0 << endl;       return 0;     }     while (n) {       a.push_back(n % 2);       n /= 2;     }     reverse(a.begin(), a.end());     N = a.size();     long long ans1 = f(0, c, 1);     if (c == 1) ans1--;     cout << ans1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, x, y, ans, f[200005];   char s[11];   int find(int k) {     if (k == f[k]) return k;     return f[k] = find(f[k]);   }   void add(int x, int y) {     if (find(x) != find(y)) f[find(x)] = find(y);   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= 2 * n + 3; i++) f[i] = i;     for (int i = 1; i <= m; i++) {       scanf( %d%d%s , &x, &y, s);       x -= y;       y = min(x + 2 * y, 2 * n - x - 2 * y + 2);       x = max(x, -x);       if (s[0] ==  x ) {         add(2 * y, 2 * x);         add(2 * y + 1, 2 * x + 1);       } else {         add(2 * y + 1, 2 * x);         add(2 * x + 1, 2 * y);       }     }     for (int i = 0; i <= n + 1; i++)       if (find(2 * i) == find(2 * i + 1)) {         printf( 0 );         return 0;       }     int s = 0;     for (int i = 0; i <= 2 * n + 3; i++) s += (find(i) == i);     s = s / 2 - 2;     ans = 1;     for (int i = 1; i <= s; i++) ans = ans * 2 % 1000000007;     printf( %d , ans);   }
#include <bits/stdc++.h>   using namespace std;   int pri[26] = {2,  3,  5,  7,  11, 13, 17, 19, 23, 29, 31, 37, 41,                  43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, -1};   long long mo[5] = {223092870ll, 2756205443ll, 907383479ll, 42600829ll, 97ll};   int N, M, d, t, len, i, j, u;   int b[100];   long long a[20000], c, ans;   char ss[100];   vector<long long> f[17][5];   int num(int d, char c) {     return c ==  ?  ? d : (c <=  9  ? c -  0  : c -  A  + 10);   }   void prework() {     int i, j, k, l;     for (i = 2; i <= 16; i++) {       for (j = 0, k = l = 1; k <= N; k *= i, l *= i + 1)         ;       for (j = 0; j < 5; j++) f[i][j].resize(l, -1);     }   }   int calc(int d, int p) {     int i, j, x = 0, y = 0;     for (i = 0; i < t; i++) x = x * (d + 1) + b[i];     if (f[d][p][x] != -1) return x;     f[d][p][x] = 1;     for (i = 0; i < t; i++)       if (b[i] == d) {         for (j = 0; j < d; j++)           b[i] = j, f[d][p][x] = (f[d][p][x] * f[d][p][calc(d, p)]) % mo[p];         b[i] = d;         return x;       }     for (i = 0; i < t; i++) y = y * d + b[i];     if (y < N) f[d][p][x] = a[y] % mo[p];     return x;   }   int main() {     scanf( %d , &N);     for (i = 0; i < N; i++) scanf( %I64d , &a[i]);     scanf( %d , &M);     prework();     while (M--) {       scanf( %d%s%I64d , &d, ss, &c);       for (j = 1, t = 0; j <= N; t++, j *= d)         ;       len = strlen(ss);       for (j = 0; j < t; j++)         if (len - t + j >= 0)           b[j] = num(d, ss[len - t + j]);         else           b[j] = 0;       for (i = 0; i < 5; i++) {         j = calc(d, i);         ans = (f[d][i][j] + c) % mo[i];         for (j = 0; j < 25; j++)           if (mo[i] % pri[j] == 0 && ans % pri[j] == 0) break;         if (j < 25) break;       }       printf( %d n , pri[j]);     }   }
#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-6;   const int INF = 2e9;   const long long LNF = 9e18;   const int mod = 1e9 + 7;   const int maxn = 2e3 + 10;   int n, m, k;   bool val[maxn], pos[maxn];   long long C[maxn][maxn], A[maxn];   void init() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       int x;       scanf( %d , &x);       if (x != -1) val[x] = pos[i] = 1;     }     k = m = 0;     for (int i = 1; i <= n; i++) {       if (!pos[i]) m++;       if (!pos[i] && !val[i]) k++;     }     A[0] = 1;     C[0][0] = 1;     for (int i = 1; i <= m; i++) {       A[i] = (1LL * i * A[i - 1]) % mod;       C[i][0] = 1;       for (int j = 1; j <= i; j++)         C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;     }   }   void solve() {     long long ans = A[m];     for (int i = 1; i <= k; i++) {       long long tmp = (1LL * C[k][i] * A[m - i]) % mod;       ans -= (i & 1) ? tmp : -tmp;       ans = (ans + mod) % mod;     }     cout << ans << endl;   }   int main() {     init();     solve();   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   char ara[100][100];   int fr[100], fc[100];   vector<int> r, c;   int pr;   void row(int j);   void col(int j) {     fc[j] = 1;     c.push_back(j);     for (int i = 0; i < n; i++)       if (ara[i][j] ==  # )         if (fr[i] == 0) row(i);   }   void row(int i) {     fr[i] = 1;     r.push_back(i);     for (int j = 0; j < m; j++)       if (ara[i][j] ==  # )         if (fc[j] == 0) col(j);   }   int check() {     for (int i = 0; i < r.size(); i++) {       for (int j = 0; j < c.size(); j++) {         if (ara[r[i]][c[j]] ==  . ) return 1;       }     }     return 0;   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> ara[i][j];       }     }     for (int j = 0; j < m; j++) {       if (fc[j] == 0) {         pr++;         c.clear();         r.clear();         col(j);         if (check()) {           cout <<  No n ;           return 0;         }       }     }     cout <<  Yes n ;     return 0;   }