malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> a(n), pmx(n, -1), smx(n, -1);     cin >> a[0];     pmx[0] = 0;     int c = 1;     for (int j = 1; j < n; j++) {       cin >> a[j];       pmx[j] = pmx[j - 1];       smx[j] = (j == 1) + (j != 1) * smx[j - 1];       if (a[j] > a[pmx[j]]) {         smx[j] = pmx[j];         pmx[j] = j;         c++;       } else if (a[j] > a[smx[j]]) {         smx[j] = j;       }     }     vector<int> rec(n, c);     int mx = -1, i;     for (int j = 0; j < n; j++) {       rec[j] -= (pmx[j] == j);     }     for (int j = 1; j < n; j++) {       if (smx[j] != j) continue;       rec[pmx[j]]++;     }     for (int j = 0; j < n; j++) {       if (mx < rec[j]) {         mx = rec[j];         i = j;       } else if (mx == rec[j] && a[j] < i) {         i = j;       }     }     int mn = n;     for (int j = 0; j < n; j++) {       if (rec[i] == rec[j] && mn > a[j]) {         mn = a[j];       }     }     cout << mn;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e3 + 5;   int n, m;   vector<int> g[maxn];   long long dis[maxn][maxn];   vector<pair<long long, long long> > from[maxn], to[maxn];   void bfs(int x) {     queue<int> q;     q.push(x);     dis[x][x] = 0;     while (!q.empty()) {       int now = q.front();       q.pop();       for (int i : g[now]) {         if (dis[x][i] == -1) {           dis[x][i] = dis[x][now] + 1;           q.push(i);         }       }     }   }   int main() {     memset(dis, -1, sizeof(dis));     scanf( %d%d , &n, &m);     for (int u, v, i = 1; i <= m; i++) {       scanf( %d%d , &u, &v);       g[u].push_back(v);     }     for (int i = 1; i <= n; i++) bfs(i);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         from[i].push_back({dis[i][j], j});         to[j].push_back({dis[i][j], i});       }     }     for (int i = 1; i <= n; i++) {       sort(from[i].begin(), from[i].end(),            greater<pair<long long, long long> >());       sort(to[i].begin(), to[i].end(), greater<pair<long long, long long> >());     }     int a, b, c, d, cnt = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (i == j || dis[i][j] == -1) continue;         for (int k = 0; k < 4; k++) {           if (to[i][k].second == j || to[i][k].second == i) continue;           for (int l = 0; l < 4; l++) {             if (from[j][l].second == i || from[j][l].second == to[i][k].second ||                 from[j][l].second == j)               continue;             if (to[i][k].first == -1 || from[j][l].first == -1) continue;             long long now = dis[i][j] + to[i][k].first + from[j][l].first;             if (now > cnt) {               cnt = now;               a = to[i][k].second, b = i, c = j, d = from[j][l].second;             }           }         }       }     }     printf( %d %d %d %d n , a, b, c, d);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       string p, h;       int w = 0;       cin >> p >> h;       sort(p.begin(), p.end());       if (h.size() >= p.size()) {         for (int i = 0; i < (h.size() - p.size() + 1); i++) {           string sub = h.substr(i, p.size());           sort(sub.begin(), sub.end());           if (sub == p) {             w = 1;             break;           }         }         if (w == 1)           cout <<  YES  << endl;         else           cout <<  NO  << endl;       } else         cout <<  NO  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int k;     cin >> k;     int n, m;     n = 2;     m = 3;     int z = (1 << 17);     cout << n <<     << m <<   n ;     cout << z + k <<     << z <<     << k <<   n ;     cout << k <<     << z + k <<     << k <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   set<int> s;   int ans[1000000];   int main() {     int n, m;     scanf( %d %d , &n, &m);     int i, j, l, r, w;     for (i = 1; i <= n; i++) s.insert(i);     for (i = 0; i < m; i++) {       scanf( %d %d %d , &l, &r, &w);       auto idx1 = s.lower_bound(l);       auto idx2 = s.upper_bound(r);       vector<int> v;       for (auto it = idx1; it != idx2; it++) {         ans[*it] = w;         if (*it != w) v.push_back(*it);       }       for (j = 0; j < v.size(); j++) s.erase(v[j]);     }     ans[w] = 0;     for (i = 1; i <= n; i++) printf( %d  , ans[i]);     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[25][25], n, m, dp[1 << 22];   char s[25][222];   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; ++i) scanf( %s , s[i]);     for (int i = 0; i < n; ++i)       for (int j = 0; j < m; ++j) scanf( %d , &a[i][j]);     memset(dp, 63, sizeof(dp)), dp[0] = 0;     for (int l, msk = 0; msk < (1 << n); ++msk) {       for (int b = 0; b < n; ++b)         if (!(msk & (1 << b))) {           l = b;           break;         }       for (int c = 0; c < m; ++c) {         dp[msk + (1 << l)] = min(dp[msk + (1 << l)], dp[msk] + a[l][c]);         int t = 0, h = 0, tmsk = msk;         for (int i = 0; i < n; ++i)           if (s[i][c] == s[l][c])             tmsk |= (1 << i), t += a[i][c], h = max(h, a[i][c]);         dp[tmsk] = min(dp[tmsk], dp[msk] + t - h);       }     }     printf( %d n , dp[(1 << n) - 1]);   }
#include <bits/stdc++.h>   using namespace std;   long long ans[200005];   vector<vector<pair<long long, long long> > > G(200005);   int main() {     long long i, j, n, m, a, b, c;     priority_queue<pair<long long, long long> > pq;     scanf( %lld %lld , &n, &m);     for (i = 0; i < m; i++) {       scanf( %lld %lld %lld , &a, &b, &c);       G[a].push_back({b, c});       G[b].push_back({a, c});     }     for (i = 1; i <= n; i++) {       scanf( %lld , &ans[i]);       pq.push({-ans[i], i});     }     while (!pq.empty()) {       pair<long long, long long> tp = pq.top();       pq.pop();       tp.first *= -1;       if (tp.first > ans[tp.second]) continue;       ans[tp.second] = tp.first;       for (pair<long long, long long> next : G[tp.second]) {         long long poss = next.second * 2 + tp.first;         if (poss < ans[next.first]) {           pq.push({-poss, next.first});         }       }     }     for (i = 1; i <= n; i++) printf( %lld  , ans[i]);     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void k_007() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   bool sortbysec(const pair<long long, long long> &a,                  const pair<long long, long long> &b) {     return (a.second < b.second);   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long prime(long long n) {     long long flag = 1;     for (long long i = 2; i <= sqrt(n); i++) {       if (n % i == 0) {         flag = 0;         break;       }     }     return flag;   }   long long power(long long x, long long y) {     if (y == 0)       return 1;     else if (y % 2 == 0)       return ((power(x, y / 2) % 1000000007) * (power(x, y / 2) % 1000000007)) %              1000000007;     else       return ((x % 1000000007) * (power(x, y / 2) % 1000000007) *               (power(x, y / 2) % 1000000007)) %              1000000007;   }   long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }   int32_t main() {     k_007();     long long n;     cin >> n;     if (n & 1) {       if (n == 1) {         cout << 3 << endl;       } else {         cout << 1 << endl;       }     } else {       long long m = 1;       while (prime(n * m + 1)) {         m++;       }       cout << m << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     for (int i{0}; i < n; i++) {       int found{0};       int num{};       cin >> num;       int res{};       for (int j{0}; j < num; j++) {         int numI;         cin >> numI;         if (numI % 2 == 0) {           found = 1;           res = j + 1;         }       }       if (found == 1) {         cout << 1 <<   n  << res <<   n ;       } else {         if (num > 1)           cout << 2 <<   n  << 1 <<     << 2 <<   n ;         else           cout << -1 <<   n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, k, l, r, st;   map<pair<long long, long long>, long long> M[2];   long long Count(long long len, long long t, bool w) {     if (M[w].count({len, t})) return (M[w][{len, t}]);     if (t > len) return (M[w][{len, t}] = 0);     if (len == t) return (M[w][{len, t}] = 1);     return (M[w][{len, t}] =                 Count(len / 2, t, w) + Count((len - 1) / 2, t, w) + w);   }   long long get(long long len, long long t) {     return (Count(len, t, 0) + Count(len, t + 1, 0));   }   int main() {     scanf( %I64d%I64d , &n, &k);     if (k == 1) return !printf( 1 );     if (k == 2) return !printf( %I64d , n);     n -= 2;     k -= 2;     l = 1;     r = n;     while (r - l > 1) {       if (k > Count(n, (l + r) / 2 * 2 - 1, 1))         r = (l + r) / 2;       else         l = (l + r) / 2;     }     st = l * 2 - 1;     l = 1;     r = n;     k -= Count(n, st + 2, 1);     while (get(r - l + 1, st) && !(st == 1 && r - l <= 1)) {       if (k <= get((l + r) / 2 - l, st))         r = (l + r) / 2 - 1;       else         k -= get((l + r) / 2 - l, st), l = (l + r) / 2 + 1;     }     if (st == 1 && r - l <= 1) l += k;     return !printf( %I64d , l);   }
#include <bits/stdc++.h>   using namespace std;   struct algo {     int n;     map<pair<int, int>, int> v1;     vector<vector<int> > drogi;     vector<pair<int, int> > tab;     vector<pair<int, int> > wy;     algo() {       cin >> n;       string s;       int li = 0;       tab.push_back({});       for (long long a, b, i = 1; i <= n; i++) {         cin >> s;         a = b = 0;         for (int j = 0; j < s.size(); j++) {           a = (a * 33 + s[j] -  a  + 1) % 1000000009;           if (j != 0) b = (b * 33 + s[j] -  a  + 1) % 1000000009;           if (v1.find({a, j + 1}) != v1.end()) continue;           v1[{a, j + 1}] = ++li;           tab.push_back({b, j});         }       }       drogi.resize(li + 10);       for (int i = 1; i <= li; i++) {         drogi[v1[tab[i]]].push_back(i);       }       wy.resize(li + 10);       cout << dfs().second <<   n ;     }     pair<int, int> dfs(int gdzie = 0) {       if (wy[gdzie].second != 0) return wy[gdzie];       int x = 0, y = 0;       pair<int, int> p;       for (int i : drogi[gdzie]) {         p = dfs(i);         x += p.first;         y += p.second;       }       return wy[gdzie] = {max(y + 1, x), x};     }   };   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       algo p;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     int n, ans = 0, i, temp;     vector<int> a;     cin >> n;     for (i = 0; i < n; i++) {       cin >> temp;       a.push_back(temp);     }     sort(a.begin(), a.end());     for (i = 0; i < n; i = i + 2) {       if (a[i] != a[i + 1]) {         ans += (a[i + 1] - a[i]);       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   int main(void) {     int i, w;     char s[3][10];     char res[4] = { F ,  M ,  S ,  ? };     while (scanf(  %s %s %s , s[0], s[1], s[2]) != EOF) {       w = 3;       for (i = 0; i < 3; i++) {         if (s[i][0] ==  r  && s[(i + 1) % 3][0] ==  s  &&             s[(i + 2) % 3][0] ==  s )           w = i;         if (s[i][0] ==  s  && s[(i + 1) % 3][0] ==  p  &&             s[(i + 2) % 3][0] ==  p )           w = i;         if (s[i][0] ==  p  && s[(i + 1) % 3][0] ==  r  &&             s[(i + 2) % 3][0] ==  r )           w = i;       }       printf( %c n , res[w]);     }     return 0;   }
#include <bits/stdc++.h>   int A[2][155] = {{1}, {0, 1}};   int main() {     int n;     std::cin >> n;     for (int i = 2; i <= n; ++i)       for (int j = i - 1; ~j; --j)         A[i & 1][j + 1] = (A[i & 1][j + 1] + A[~i & 1][j]) & 1;     printf( %d n , n);     for (int i = 0; i <= n; ++i) printf(i == n ?  %d n  :  %d  , A[n & 1][i]);     printf( %d n , n - 1);     for (int i = 0; i <= n - 1; ++i)       printf(i == n - 1 ?  %d n  :  %d  , A[~n & 1][i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool isluck() {     unsigned long long n, s1 = 0, s2 = 0;     string s;     cin >> n >> s;     for (int register i = 0; i < n / 2; i++) {       int x = (s[i] -  0 ) % 10;       if (x != 4 && x != 7) return false;       s1 += x;     }     for (int register i = n / 2; i < n; i++) {       int x = (s[i] -  0 ) % 10;       if (x != 4 && x != 7) return false;       s2 += x;     }     return (s1 == s2);   }   int main() {     isluck() ? cout <<  YES  << endl : cout <<  NO  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, j;   std::vector<char> v;   int main() {     std::ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     cin >> n;     int i = 0;     while (i < n) {       j = 0;       while (j < 2 && i < n) {         cout <<  a ;         j++;         i++;       }       j = 0;       while (j < 2 && i < n) {         cout <<  b ;         j++;         i++;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 3000;   char a[N][N];   int n, m, k, vis[N][N];   long long ans = 0;   int main() {     while (~scanf( %d%d%d , &n, &m, &k)) {       ans = 0;       for (register int i = 0; i <= n - 1; ++i) scanf( %s , a[i]);       for (register int i = 0; i <= n - 1; ++i) {         int c = 0;         for (register int j = 0; j <= m - 1; ++j) {           if (a[i][j] ==  . )             c++;           else             c = 0;           if (c >= k) ans++;         }       }       for (register int j = 0; j <= m - 1; ++j) {         int c = 0;         for (register int i = 0; i <= n - 1; ++i) {           if (a[i][j] ==  . )             c++;           else             c = 0;           if (c >= k) ans++;         }       }       if (k == 1) ans /= 2;       printf( %I64d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int getint() {     int res = 0, fh = 1;     char ch = getchar();     while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar();     if (ch ==  - ) fh = -1, ch = getchar();     while (ch <=  9  && ch >=  0 ) res = res * 10 + ch -  0 , ch = getchar();     return res * fh;   }   int n, m, A, B, out[23], mp[23][23];   double p[23], G[23][23], a[485][485], b[485], ans[485];   void gauss() {     for (int i = 1; i <= n * n; i++) {       int id = i;       for (int j = i + 1; j <= n * n; j++)         if (fabs(a[j][i]) > fabs(a[id][i])) id = j;       for (int j = i; j <= n * n; j++) swap(a[i][j], a[id][j]);       swap(b[i], b[id]);       if (a[i][i]) {         for (int j = i + 1; j <= n * n; j++) {           double p = a[j][i] / a[i][i];           for (int k = i; k <= n * n; k++) a[j][k] -= a[i][k] * p;           b[j] -= b[i] * p;         }       }     }     for (int i = n * n; i >= 1; i--) {       if (a[i][i])         ans[i] = b[i] / a[i][i];       else         ans[i] = 0;       for (int j = i - 1; j >= 1; j--) b[j] -= a[j][i] * ans[i];     }   }   int main() {     n = getint();     m = getint();     A = getint();     B = getint();     for (int i = 1; i <= m; i++) {       int x = getint(), y = getint();       mp[x][y] = mp[y][x] = 1;       out[x]++;       out[y]++;     }     for (int i = 1; i <= n; i++) scanf( %lf , &p[i]), G[i][i] = p[i];     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (mp[i][j]) G[i][j] = (1 - p[i]) / (double)out[i];     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) {         a[((i - 1) * n + j)][((i - 1) * n + j)] = -1;         for (int k = 1; k <= n; k++)           for (int l = 1; l <= n; l++)             if (k != l)               a[((i - 1) * n + j)][((k - 1) * n + l)] += G[k][i] * G[l][j];       }     b[((A - 1) * n + B)] = -1;     gauss();     for (int i = 1; i <= n; i++) printf( %.10lf  , ans[((i - 1) * n + i)]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int mark[1000009];   int main() {     long long n, m = 0, i, j, t;     long long sum;     cin >> n;     long long a[n];     for (i = 0; i < n; i++) {       scanf( %lld , &a[i]);     }     t = a[n - 1];     sum = t;     for (j = n - 1; j > 0; j--) {       if (a[j - 1] < t) {         sum += a[j - 1];         t = a[j - 1];       } else {         if (t - 1 >= 0) {           sum += t - 1;           t = t - 1;         } else           break;       }     }     cout << sum;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 200000;   const long long maxp = 1000000007;   int n, m;   struct data {     int adj, p, p_;   };   vector<data> e[maxn];   bool v[maxn];   long long f[maxn][2][2][2];   int con[maxn][2];   void dfs(int x) {     v[x] = false;     bool flag = false;     for (auto y : e[x])       if (v[y.adj]) {         dfs(y.adj);         flag = true;         for (int i = 0; i < 2; ++i)           for (int j = 0; j < 2; ++j)             for (int k = 0; k < 2; ++k)               for (int l = 0; l < 2; ++l)                 (f[x][i][k][l ^ ((y.p ^ i) | (y.p_ ^ j)) ^ con[x][i]] +=                  f[y.adj][j][k][l]) %= maxp;       }     if (!flag) {       f[x][1][1][con[x][1]] = 1;       f[x][0][0][con[x][0]] = 1;     }   }   int ind[maxn];   int main() {     scanf( %d%d , &m, &n);     memset(ind, 0, sizeof(ind));     for (int i = 1; i <= m; ++i) {       int k;       scanf( %d , &k);       int a, b;       if (k == 1) {         scanf( %d , &a);         if (a < 0)           con[-a][0] ^= 1;         else           con[a][1] ^= 1;       } else {         scanf( %d%d , &a, &b);         int p1 = 0, p2 = 0;         if (a < 0) {           p1 = 1;           a = -a;         }         if (b < 0) {           p2 = 1;           b = -b;         }         e[a].push_back({b, p1, p2});         e[b].push_back({a, p2, p1});         ind[a]++;         ind[b]++;       }     }     memset(v, true, sizeof(v));     memset(f, 0, sizeof(f));     long long ans[2], tmp[2];     ans[0] = 1;     ans[1] = 0;     int ab = 3;     for (int i = 1; i <= n; ++i)       if (v[i] && (ind[i] == 1 || ind[i] == 0)) {         dfs(i);         tmp[0] = ans[0];         tmp[1] = ans[1];         ans[0] = 0;         ans[1] = 0;         for (int p1 = 0; p1 < 2; ++p1)           for (int p2 = 0; p2 < 2; ++p2)             for (int u = 0; u < 2; ++u)               for (int v = 0; v < 2; ++v)                 (ans[p1 ^ p2] += tmp[p1] * f[i][u][v][p2]) %= maxp;       }     for (int i = 1; i <= n; ++i)       if (v[i]) {         dfs(i);         tmp[0] = ans[0];         tmp[1] = ans[1];         ans[0] = 0;         ans[1] = 0;         int p = e[i][1].p, p_ = e[i][1].p_;         for (int p1 = 0; p1 < 2; ++p1)           for (int p2 = 0; p2 < 2; ++p2)             for (int u = 0; u < 2; ++u)               for (int v = 0; v < 2; ++v) {                 (ans[p1 ^ p2 ^ ((u ^ p) | (v ^ p_))] +=                  tmp[p1] * f[i][u][v][p2]) %= maxp;               }       }     cout << ans[1] << endl;   }
#include <bits/stdc++.h>   using namespace std;   void setIO(string name) {     ios_base::sync_with_stdio(0);     cin.tie(0);     freopen((name +  .in ).c_str(),  r , stdin);     freopen((name +  .out ).c_str(),  w , stdout);   }   void fast() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   int main() {     fast();     int r, c;     cin >> r >> c;     char a[r][c];     for (int i = 0; i < r; i++) {       for (int j = 0; j < c; j++) {         cin >> a[i][j];         if (a[i][j] ==  . ) {           a[i][j] =  D ;         }       }     }     int f = 1;     for (int i = 0; i < r; i++) {       for (int j = 0; j < c; j++) {         if (a[i][j] ==  S ) {           if ((i - 1 >= 0 && a[i - 1][j] ==  W ) ||               (j - 1 >= 0 && a[i][j - 1] ==  W ) ||               (i + 1 <= r - 1 && a[i + 1][j] ==  W ) ||               (j + 1 <= c - 1 && a[i][j + 1] ==  W )) {             f = 0;             break;           }         }       }     }     if (f) {       cout <<  Yes             <<   n ;       for (int i = 0; i < r; i++) {         for (int j = 0; j < c; j++) {           cout << a[i][j];         }         cout <<   n ;       }     } else {       cout <<  No             <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const long long INF = 0x3f3f3f3f3f3f3f3f;   const int MAXN = 1e5 + 10;   const int N = 102;   const long long mod = 1e9 + 7;   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch - 48;       ch = getchar();     }     return x * f;   }   int a[5];   int main() {     ios::sync_with_stdio(false);     int T;     cin >> T;     while (T--) {       int A, B, C, D;       memset(a, 0, sizeof(a));       cin >> A >> B >> C >> D;       C += B;       D += A;       if ((A + B) % 2 == 0) {         a[1] = a[2] = 0;         if (C > 0) a[3] = 1;         if (D > 0) a[4] = 1;       } else {         a[3] = a[4] = 0;         if (C > 0) a[2] = 1;         if (D > 0) a[1] = 1;       }       for (int i = 1; i <= 4; ++i)         if (a[i])           cout <<  Ya  ;         else           cout <<  Tidak  ;       cout <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   long long n, m, a, b, g[3005 * 3005], x, y, z, A[3005][3005], ans;   deque<long long> q;   vector<long long> v;   vector<vector<long long> > V;   vector<long long> fun(vector<long long> v, long long n, long long b) {     vector<long long> ans;     q.clear();     for (__typeof((b)) i = (0); i < (b); i++) {       while ((long long)q.size() && v[i] <= v[q.back()]) q.pop_back();       q.push_back(i);     }     ans.push_back(v[q.front()]);     for (__typeof((n)) i = (b); i < (n); i++) {       while ((long long)q.size() && q.front() <= i - b) q.pop_front();       while ((long long)q.size() && v[i] <= v[q.back()]) q.pop_back();       q.push_back(i);       ans.push_back(v[q.front()]);     }     return ans;   }   void solve() {     cin >> n >> m >> a >> b >> g[0] >> x >> y >> z;     for (__typeof((3005 * 3005)) i = (1); i < (3005 * 3005); i++)       g[i] = (g[i - 1] * x + y) % z;     for (__typeof((n + 1)) i = (1); i < (n + 1); i++) {       for (__typeof((m + 1)) j = (1); j < (m + 1); j++)         A[i][j] = g[(i - 1) * m + j - 1];     }     for (__typeof((n + 1)) i = (1); i < (n + 1); i++) {       v.clear();       for (__typeof((m + 1)) j = (1); j < (m + 1); j++) v.push_back(A[i][j]);       V.push_back(fun(v, m, b));     }     for (__typeof(((long long)V[0].size())) j = (0); j < ((long long)V[0].size());          j++) {       v.clear();       for (__typeof((n)) i = (0); i < (n); i++) v.push_back(V[i][j]);       vector<long long> res = fun(v, n, a);       for (auto i : res) ans += i;     }     {       cout << ans <<   n ;       return;     };   }   void prep() {}   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     prep();     cout << fixed << setprecision(12);     while (t--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 100005;   const int inf = 0x3f3f3f3f;   int n, e[2][maxn], tot, rd[maxn];   int net[maxn], tail[maxn];   inline int read(void) {     int num, sign = 1;     char c;     while (!isdigit(c = getchar()))       if (c ==  - ) sign = 0;     num = c -  0 ;     while (isdigit(c = getchar())) num = (num << 1) + (num << 3) + c -  0 ;     return sign ? num : -num;   }   int dfs(int u) {     tail[u] = u;     if (!tail[net[u]]) return tail[u] = dfs(net[u]);     return tail[u];   }   signed main(void) {     n = read();     for (register int i = 1; i <= n; ++i) net[i] = read(), ++rd[net[i]];     for (register int i = 1; i <= n; ++i)       if (!rd[i]) {         e[0][++tot] = i;         e[1][tot] = dfs(i);       }     int ft = tot;     for (register int i = 1; i <= n; ++i)       if (!tail[i]) {         e[0][++tot] = i;         e[1][tot] = dfs(i);       }     if (tot == 1 && !ft) tot = 0;     printf( %d n , tot);     for (register int i = 1; i <= tot; ++i)       printf( %d %d n , e[1][i], e[0][i % tot + 1]);     return 0;   }