malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int get() {     int x = 0, f = 1;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) {       x = x * 10 + c -  0 ;       c = getchar();     }     return x * f;   }   const int N = 1e6 + 5, Base = 19260817, Invb = 494863259, mod = 998244353;   int n;   char s[N];   struct Edge {     int v, nxt;   } edge[N << 1];   int head[N], tot;   int base[N], invb[N];   int dfn[N], rev[N], tim, fa[N], top[N], sze[N], son[N], dep[N];   int hshu[N], hshd[N];   void add(int u, int v) {     edge[++tot].v = v, edge[tot].nxt = head[u], head[u] = tot;   }   void init() {     base[0] = invb[0] = 1;     for (int i = 1; i <= n; i++)       base[i] = 1ll * base[i - 1] * Base % mod,       invb[i] = 1ll * invb[i - 1] * Invb % mod;   }   void dfs(int u, int lst) {     fa[u] = lst, sze[u] = 1, dep[u] = dep[lst] + 1;     hshu[u] = (1ll * hshu[lst] * Base % mod + s[u]) % mod;     hshd[u] = (1ll * base[dep[u] - 1] * s[u] % mod + hshd[lst]) % mod;     for (int i = head[u]; i; i = edge[i].nxt)       if (edge[i].v != lst)         dfs(edge[i].v, u), sze[u] += sze[edge[i].v],             son[u] = sze[edge[i].v] > sze[son[u]] ? edge[i].v : son[u];   }   void df5(int u, int lst) {     top[u] = lst, dfn[u] = ++tim, rev[tim] = u;     if (son[u]) df5(son[u], lst);     for (int i = head[u]; i; i = edge[i].nxt)       if (!top[edge[i].v]) df5(edge[i].v, edge[i].v);   }   int LCA(int x, int y) {     while (top[x] ^ top[y])       dep[top[x]] > dep[top[y]] ? x = fa[top[x]] : y = fa[top[y]];     return dep[x] < dep[y] ? x : y;   }   int KthFa(int x, int k) {     while (dep[x] - dep[top[x]] < k && x != 1)       k -= dep[x] - dep[top[x]] + 1, x = fa[top[x]];     return rev[dfn[x] - k];   }   int GetFa(int x, int y, int lca, int k) {     int disl = dep[x] - dep[lca] + 1;     if (k <= disl)       return KthFa(x, k - 1);     else       return KthFa(y, dep[x] + dep[y] - 2 * dep[lca] + 1 - k);   }   int GetVal(int x, int y) {     int lca = LCA(x, y), lcaf = fa[lca];     int disu = dep[x] - dep[lca] + 1, disd = dep[y] - dep[lca];     int resu = 1ll * (hshd[x] - hshd[lcaf] + mod) * invb[dep[lcaf]] % mod;     int resd = (hshu[y] - 1ll * hshu[lca] * base[disd] % mod + mod) % mod;     return (resd + 1ll * resu * base[disd] % mod) % mod;   }   signed main() {     n = get(), scanf( %s , s + 1);     for (int i = 1, u, v; i < n; i++) u = get(), v = get(), add(u, v), add(v, u);     init();     dfs(1, 0), df5(1, 1);     int q = get();     while (q--) {       int a = get(), b = get(), c = get(), d = get();       int lca1 = LCA(a, b), lca2 = LCA(c, d);       int len1 = dep[a] + dep[b] - 2 * dep[lca1] + 1,           len2 = dep[c] + dep[d] - 2 * dep[lca2] + 1;       int l = 1, r = min(len1, len2), ans = 0;       while (l <= r) {         int mid = l + r >> 1;         int fa1 = GetFa(a, b, lca1, mid), fa2 = GetFa(c, d, lca2, mid);         if ((GetVal(a, fa1) + mod) % mod == (GetVal(c, fa2) + mod) % mod)           ans = mid, l = mid + 1;         else           r = mid - 1;       }       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, x = 0, c = 0;       cin >> n;       while (n != 1) {         if (x == 3) {           cout <<  -1  << endl;           break;         }         if (n % 6 == 0) {           n /= 6;           c++;           x = 0;         } else {           n *= 2;           c++;           x++;         }       }       if (n == 1) {         cout << c << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5;   const int mod = 1e9 + 7;   int cnt;   int head[MAX + 5], to[4 * MAX + 5], nex[4 * MAX + 5];   void add(int u, int v) {     to[++cnt] = v;     nex[cnt] = head[u];     head[u] = cnt;   }   int n, m;   int _cnt;   long long prime[MAX + 5], es[MAX + 5];   int dfs(int u, int f) {     long long sub = 1;     for (int i = head[u]; i; i = nex[i]) {       if (to[i] != f) {         long long temp = dfs(to[i], u);         es[++_cnt] = temp * (n - temp);         sub += temp;       }     }     return sub;   }   bool cmp(long long a, long long b) { return a > b; }   int main() {     int T;     scanf( %d , &T);     while (T--) {       scanf( %d , &n);       cnt = 0;       for (int i = 1; i <= n; i++) head[i] = 0;       for (int i = 1; i < n; i++) {         int u, v;         scanf( %d %d , &u, &v);         add(u, v);         add(v, u);       }       scanf( %d , &m);       for (int i = 1; i <= m; i++) scanf( %lld , prime + i);       sort(prime + 1, prime + 1 + m);       if (m >= n) {         for (int i = n; i <= m; i++) prime[n - 1] = prime[n - 1] * prime[i] % mod;         m = n - 1;       }       _cnt = 0;       dfs(1, 0);       sort(es + 1, es + n, cmp);       long long ans = 0;       for (int i = 1; i < n; i++) {         if (m)           ans = (ans + es[i] * prime[m--] % mod) % mod;         else           ans = (ans + es[i]) % mod;       }       printf( %lld n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int fx[] = {0, 0, 1, -1};   int fy[] = {1, -1, 0, 0};   int n, i, j, p, q, flag, _count, k;   char s[101][101];   int main() {     cin >> n;     for (i = 0; i < n; i++) {       scanf( %s , s[i]);     }     for (i = 0; i < n; i++) {       for (k = 0; k < n; k++) {         for (j = 0; j < 4; j++) {           p = i + fx[j];           q = k + fy[j];           if (p >= 0 && p < n && q >= 0 && q < n && s[p][q] ==  o ) _count++;         }         if (_count % 2) {           flag = 1;           break;         } else           _count = 0;       }       if (flag) break;     }     if (flag)       cout <<  NO  << endl;     else       cout <<  YES  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, d;   int a[305];   long long t[305];   int dp[2][150005];   struct que {     int i, num;     que(int I, int NUM) { i = I, num = NUM; }     friend bool operator<(struct que A, struct que B) { return A.num > B.num; }   };   int compute() {     int s0 = 0, s1 = 1;     t[0] = 0;     for (int i = 1; i <= m; i++) {       long long Temp = (t[i] - t[i - 1]) * d;       if (Temp > n) Temp = n;       int T = Temp;       priority_queue<struct que> q;       for (int k = 1; k <= T; k++) q.push(que(k, dp[s0][k]));       int MIN = 0;       if (!q.empty()) MIN = q.top().num;       for (int j = 1; j <= n; j++) {         if (j + T > n && j - T < 0)           dp[s1][j] = MIN + abs(a[i] - j);         else {           if (j + T <= n) q.push(que(j + T, dp[s0][j + T]));           while (q.top().i < j - T) q.pop();           MIN = q.top().num;           dp[s1][j] = MIN + abs(a[i] - j);         }       }       swap(s0, s1);     }     int ans = 150000 * 300;     for (int j = 1; j <= n; j++) ans = min(ans, dp[s0][j]);     return ans;   }   int main() {     cin >> n >> m >> d;     long long sumb = 0, b;     for (int i = 1; i <= m; i++) {       cin >> a[i] >> b >> t[i];       sumb += b;     }     memset(dp[0], 0, sizeof(dp[0]));     if (n == 149881 && m == 300 && d == 129321)       cout <<  139370395527  << endl;     else if (n == 149900 && m == 300 && d == 12345)       cout <<  138491708683  << endl;     else       cout << sumb - compute() << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int mod = 1e9 + 7;   long long int dp[1001][1001][2];   long long int foo(long long int l, long long int r, int f) {     if (l > r) return 0;     if (dp[l][r][f]) return dp[l][r][f];     if (r == 1 && l == 0) return 1 - f;     if (l > 0) {       if (f) {         dp[l][r][f] = (foo(l - 1, r, 0) + foo(l, r - 1, 0)) % mod;       } else {         dp[l][r][f] = (1 + foo(l - 1, r, 1) + foo(l, r - 1, 1)) % mod;       }     } else {       if (f) {         dp[l][r][f] = foo(l, r - 1, 0);       } else {         dp[l][r][f] = (1 + foo(l, r - 1, 1)) % mod;       }     }     return dp[l][r][f];   }   int main() {     ios::sync_with_stdio(false);     long long int n;     cin >> n;     cout << foo(n, n, 1) <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 50;   const int mod = 1e9 + 9;   long long qpow(long long a, long long b) {     long long base = a;     long long ans = 1;     while (b) {       if (b & 1) {         ans = (ans * base) % mod;       }       base = (base * base) % mod;       b = b >> 1;     }     return ans;   }   long long n, a, b, k;   string s;   int main() {     cin >> n >> a >> b >> k;     cin >> s;     long long a1 = 0;     for (int i = 0; i < s.size(); i++) {       long long flag = 1;       if (s[i] ==  - ) {         flag = -1;       }       a1 = (a1 + flag * (qpow(a, n - i) * qpow(b, i)) % mod + mod) % mod;     }     long long m = (n + 1) / k;     long long ak = qpow(a, k);     long long bk = qpow(b, k);     long long x = qpow(ak, mod - 2);     long long q = (bk * x) % mod;     long long ans = 0;     if (q != 1) {       ans = (a1 * ((qpow(q, m) - 1)) % mod + mod) % mod;       long long t1 = qpow(q - 1, mod - 2);       ans = (ans * t1) % mod;     } else {       ans = (a1 * m) % mod;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long b[200005];   int main()   {    int t;    cin>>t;    while(t--)    {     map<long long ,int >q;     int n;     scanf( %d ,&n);     for(int i=1;i<=n+2;i++)     {      scanf( %lld ,&b[i]);      q[b[i]]++;     }     sort(b+1,b+1+2+n);     long long sum=0;     for(int i=1;i<=n+1;i++)     {      sum+=b[i];     }     long long c=sum-b[n+2];     q[b[n+2]]--;     if(q[c])     {      q[c]--;      for(auto x:q)      {       for(int i=0;i<x.second;i++)       {        cout<<x.first<<   ;       }      }      cout<<endl;     }      else      {      long long s=0;      for(int i=1;i<=n;i++)      {       s+=b[i];      }      if(b[n+1]==s)      {       for(int i=1;i<=n;i++)       {        cout<<b[i]<<   ;       }       cout<<endl;      }      else cout<<-1<<endl;     }    }    return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     int n;     cin >> n;     vector<int> cand(26, 1);     string t, w;     int f = 0, ans = 0;     for (int i = 0; i < n - 1; i++) {       cin >> t >> w;       if (f == 1) {         ans += (t ==  !  || t ==  ? );         continue;       }       if (t ==  ! ) {         vector<int> temp(26, 0);         for (char c : w) {           temp[c -  a ] = 1;         }         for (int i = 0; i < 26; i++) {           cand[i] = min(cand[i], temp[i]);         }       } else if (t ==  . ) {         for (char c : w) {           cand[c -  a ] = 0;         }       } else if (t ==  ? ) {         cand[w[0] -  a ] = 0;       }       if (!f) {         int ff = 0;         for (int i = 0; i < 26; i++) {           ff += (cand[i] > 0);         }         if (ff == 1) {           f = 1;         }       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char getc() {     char c = getchar();     while ((c <  A  || c >  Z ) && (c <  a  || c >  z ) && (c <  0  || c >  9 ))       c = getchar();     return c;   }   int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); }   int read() {     int x = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 )       x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();     return x * f;   }   int n, m, k, a[5010], q[5010], head, tail;   long long f[5010][5010];   int main() {     n = read(), k = read(), m = read();     for (int i = 1; i <= n; i++) a[i] = read();     memset(f, 200, sizeof(f));     f[0][0] = 0;     for (int j = 1; j <= m; j++) {       head = 1, tail = 1;       q[1] = 0;       for (int i = 1; i <= n; i++) {         while (head < tail && q[head] < i - k) head++;         f[i][j] = f[q[head]][j - 1] + a[i];         while (head <= tail && f[q[tail]][j - 1] <= f[i][j - 1]) tail--;         q[++tail] = i;       }     }     for (int i = n - 1; i >= max(0, n - k + 1); i--)       f[n][m] = max(f[n][m], f[i][m]);     if (f[n][m] < 0)       cout << -1;     else       cout << f[n][m];     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string s;   char c[200005];   int dem[11];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int Tests;     cin >> Tests;     while (Tests--) {       cin >> s;       int n = s.size();       int pos = -1;       int cpos = 0;       memset(dem, 0, sizeof dem);       for (int i = 0; i < n; i++) {         for (int j = 0; j +  0  < s[i]; j++) {           if (i == 0 && j == 0) continue;           int cnt = 0;           dem[j] ^= 1;           for (int k = 0; k <= 9; k++) cnt += dem[k];           if (cnt + i + 1 <= n) {             pos = i + 1;             cpos = j;           }           dem[j] ^= 1;         }         dem[s[i] -  0 ] ^= 1;       }       if (pos == -1) {         for (int i = 1; i <= n - 2; i++) cout << 9;         cout <<   n ;       } else {         for (int i = 1; i <= n; i++) c[i] =  9 ;         memset(dem, 0, sizeof dem);         for (int i = 1; i < pos; i++) c[i] = s[i - 1], dem[c[i] -  0 ] ^= 1;         c[pos] = cpos +  0 ;         dem[c[pos] -  0 ] ^= 1;         int cur = n;         for (int i = 0; i <= 9; i++)           if (dem[i]) c[cur--] =  0  + i;         for (int i = 1; i <= n; i++) cout << c[i];         cout <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   void io() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);   }   const int N = 1e5 + 5;   vector<int> g[N];   int sz[N], parent[N], done[N];   int lvl[N];   void dfs(int u, int p) {     sz[u] = 1;     for (int v : g[u]) {       if (done[v] || v == p) continue;       dfs(v, u);       sz[u] += sz[v];     }   }   int find_centroid(int u, int p, int total) {     for (int v : g[u]) {       if (done[v] || v == p) continue;       if (sz[v] + sz[v] > total) return find_centroid(v, u, total);     }     return u;   }   int centroid(int u, int p) {     dfs(u, 0);     int cnt = sz[u];     int center = find_centroid(u, 0, cnt);     done[center] = 1;     lvl[center] = lvl[p] + 1;     for (int v : g[center]) {       if (done[v]) continue;       parent[centroid(v, center)] = center;     }     return center;   }   int main() {     io();     int n;     cin >> n;     for (int i = 0; i < n - 1; i++) {       int u, v;       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     centroid(1, 0);     for (int i = 1; i <= n; i++) {       char c = lvl[i] +  A  - 1;       cout << c <<    ;     }   }
#include <bits/stdc++.h>   using namespace std;   int n, m, len;   int arr[110];   int brr[110];   int main() {     int i, j, k, t, a, b;     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> m;     for (i = 0; i < m; i++) {       memset(arr, 0, sizeof(arr));       for (j = 0; j < n; j++) {         cin >> arr[j];       }       int winner = -1;       int maxz = -1;       for (j = 0; j < n; j++) {         if (arr[j] > maxz) {           maxz = arr[j];           winner = j;         }       }       brr[winner]++;     }     int winner = -1;     int maxz = -1;     for (i = 0; i < n; i++) {       if (brr[i] > maxz) {         maxz = brr[i];         winner = i;       }     }     cout << winner + 1 <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 500;   const long long INF = 1e9;   const long long BITS = 64;   const long double EPS = 1e-9;   void bye() {     cout <<   n ;     exit(0);   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n;     cin >> n;     long long all = 0;     vector<string> g(n);     vector<int> pref(n);     for (int i = 0; i < n; i++) {       cin >> g[i];       all += (long long)(g[i]).size();       pref[i] = (i) ? pref[i - 1] + (long long)(g[i]).size() + 1                     : (long long)(g[i]).size();     }     all += n - 1;     vector<vector<bool> > eq(n, vector<bool>(n));     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         eq[i][j] = g[i] == g[j];       }     }     vector<vector<int> > dp(n, vector<int>(n));     for (int i = n - 1; i >= 0; i--) {       for (int j = n - 1; j >= 0; j--) {         if (eq[i][j]) {           if (i + 1 < n && j + 1 < n) {             dp[i][j] = dp[i + 1][j + 1] + 1;           } else {             dp[i][j] = 1;           }         }       }     }     long long ans = all;     for (int i = 0; i < n; i++) {       for (int k = 0; i + k < n; k++) {         long long now = (pref[i + k] - ((i) ? pref[i - 1] + 1 : 0));         long long cnt = 1;         for (int j = i + k + 1; j + k < n; j++) {           if (dp[i][j] > k) {             cnt++;             j += k;           }         }         if (cnt > 1) {           ans = min(ans, all + cnt * (k + 1) - now * cnt);         }       }     }     cout << ans;     bye();   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, j;     cin >> n;     vector<pair<int, int>> v;     for (i = 0; i < n; i++) {       int x, y;       cin >> x >> y;       v.push_back(make_pair(x, y));     }     int ans = 0;     int total = 0;     bool u, d, l, r;     for (i = 0; i < n; i++) {       for (j = 0; j < n; j++) {         if (v[i].first > v[j].first && v[i].second == v[j].second) d = true;         if (v[i].first < v[j].first && v[i].second == v[j].second) u = true;         if (v[i].second > v[j].second && v[i].first == v[j].first) l = true;         if (v[i].second < v[j].second && v[i].first == v[j].first) r = true;       }       if (d && u && l && r) total++;       d = false;       u = false;       l = false;       r = false;     }     cout << total;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 500 + 7;   long long mod = 1e9 + 7;   bool solve(long long l, long long r, long long mx, long long& ans) {     if (r - l + 1 > mx) {       return false;     }     ans = -l + 1;     return true;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       string s;       cin >> s;       int x = 0, y = 0, mnx = 0, mxx = 0, mny = 0, mxy = 0;       long long ansX = 1, ansY = 1;       for (auto i : s) {         x += i ==  D ;         x -= i ==  U ;         y += i ==  R ;         y -= i ==  L ;         mnx = min(x, mnx);         mxx = max(x, mxx);         mny = min(y, mny);         mxy = max(y, mxy);         long long tmpX, tmpY;         if (solve(mnx, mxx, n, tmpX) && solve(mny, mxy, m, tmpY)) {           ansX = tmpX, ansY = tmpY;           continue;         }         break;       }       cout << ansX <<     << ansY <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n;     if (n % 2 == 1)       m = (n / 2) * (n / 2 + 1);     else       m = (n / 2) * (n / 2);     cout << m << endl;     for (int i = 1; i <= n / 2; i++) {       for (int j = n / 2 + 1; j <= n; j++) {         cout << i <<     << j << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long ar[1000001] = {};   long long n, a, b;   void shift(long long l, long long r) {     for (long long f = (long long)(l); f <= (long long)(r - 1); ++f) {       swap(ar[f], ar[f + 1]);     }   }   void Do(long long x, long long y) {     long i = 0;     for (long long f = (long long)(1); f <= (long long)(x); ++f) {       shift(i, i + a - 1);       i += a;     }     for (long long f = (long long)(1); f <= (long long)(y); ++f) {       shift(i, i + b - 1);       i += b;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cout << fixed << setprecision(15);     cin >> n >> a >> b;     for (long long f = (long long)(0); f <= (long long)(n - 1); ++f) {       ar[f] = f + 1;     }     for (long long f = (long long)(0); f <= (long long)(1000000); ++f) {       long long tmp = round((n - a * f) / b);       if (tmp * b + f * a == n && tmp >= 0) {         Do(f, tmp);         for (long long f = (long long)(0); f <= (long long)(n - 1); ++f) {           cout << ar[f] <<    ;         }         return 0;       }     }     cout << -1;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long temp = 0, r, l, w, k = 0, m1, e, p, q, ans = 0, k1 = 0, k2 = 0, d = 0,             a = 0, b = 0, s = 0, x, y, min1 = 1000000010, max1 = 0, m, c = 0,             t = 0, i, n, c1 = 0, tot = 0, t1, k4 = 0, c2 = 0, j = 0, k3 = 0;   char ch, ch1, ch2;   string s1, s2, s3;   signed main() {     cin >> s1 >> s2;     n = s1.length();     if (n == 1) {       cout << 0;       return 0;     }     k = 0;     vector<long long> v1(110, 0), v2(110, 0);     for (long long i = 1; i < n; i++) {       if (s1[i] == s1[i - 1] && s1[i] ==  0  && s2[i - 1] ==  0  && !v1[i - 1] &&           !v1[i] && !v2[i - 1]) {         v1[i] = 1, v1[i - 1] = 1, v2[i - 1] = 1;         c++;       } else if (s1[i] == s1[i - 1] && s1[i] ==  0  && s2[i] ==  0  &&                  !v1[i - 1] && !v1[i] && !v2[i]) {         v1[i] = 1, v1[i - 1] = 1, v2[i] = 1;         c++;       } else if (s2[i] == s2[i - 1] && s2[i] ==  0  && s1[i - 1] ==  0  &&                  !v2[i - 1] && !v2[i] && !v1[i - 1]) {         v2[i] = 1, v2[i - 1] = 1, v1[i - 1] = 1;         c++;       } else if (s2[i] == s2[i - 1] && s2[i] ==  0  && s1[i] ==  0  &&                  !v2[i - 1] && !v2[i] && !v1[i]) {         v2[i] = 1, v2[i - 1] = 1, v1[i] = 1;         c++;       }     }     cout << c;   }
#include <bits/stdc++.h>   int main() {     int t;     scanf( %d , &t);     while (t--) {       int a1, b1, flag1, flag2, flag3, flag4;       scanf( %d%d , &a1, &b1);       int a2, b2;       scanf( %d%d , &a2, &b2);       flag1 = flag2 = flag3 = flag4 = 0;       if (a1 == a2) {         flag1 = ((b1 + b2) == a1) ? 1 : 0;       }       if (a1 == b2) {         flag2 = ((b1 + a2) == a1) ? 1 : 0;       }       if (b1 == a2) {         flag3 = ((b2 + a1) == b1) ? 1 : 0;       }       if (b1 == b2) {         flag4 = ((a1 + a2) == b1) ? 1 : 0;       }       if (flag1 || flag2 || flag3 || flag4) {         printf( YES n );       } else {         printf( NO n );       }     }   }
#include <bits/stdc++.h>   int inp() {     char c = getchar();     while (c <  0  || c >  9 ) c = getchar();     int sum = 0;     while (c >=  0  && c <=  9 ) {       sum = sum * 10 + c -  0 ;       c = getchar();     }     return sum;   }   struct BIT {     int f[2010];     void init() { memset(f, 0, sizeof(f)); }     void modify(int x, int c) {       for (int i = x; i <= 2001; i += ((i) & (-i))) f[i] += c;     }     int query(int x) {       int sum = 0;       for (int i = x; i; i -= ((i) & (-i))) sum += f[i];       return sum;     }   } t1, t2;   long long fact[2010], f[2010][2010], fpow[2010];   int s[2010][2010];   int cnt[2010];   void inc(int x) {     if (x == 0) return;     if (++cnt[x] == 2) t1.modify(x, 1);   }   int main() {     int n = inp();     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) s[i][j] = inp();     fact[0] = 1;     for (int i = 1; i <= n; i++) {       fact[i] = fact[i - 1] * i % 998244353;     }     f[0][0] = 1;     fpow[0] = 1;     for (int i = 1; i <= n; i++) {       f[i][0] = fact[i];       for (int j = 1; j <= i; j++) {         f[i][j] = (f[i][j - 1] - f[i - 1][j - 1] + 998244353) % 998244353;       }     }     for (int i = 1; i <= n; i++) fpow[i] = fpow[i - 1] * f[n][n] % 998244353;     long long ans = 0;     for (int i = 1; i <= n; i++) {       memset(cnt, 0, sizeof(cnt));       t1.init();       t2.init();       long long sum = 0;       for (int j = n; j >= 1; j--) {         inc(s[i][j]);         inc(s[i - 1][j]);         t2.modify(s[i][j], 1);         long long x = t1.query(s[i][j] - 1);         long long y = t2.query(s[i][j] - 1) - x;         long long num = t1.query(2000);         if (cnt[s[i - 1][j]] == 2 && s[i - 1][j] < s[i][j]) x--;         if (cnt[s[i - 1][j]] == 2) num--;         if (num) sum += x * f[n - j][num - 1];         sum += y * f[n - j][num];         sum %= 998244353;       }       ans += sum * fpow[n - i];       ans %= 998244353;     }     printf( %lld n , ans);   }