malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long long _gcd(long long x, long long y) { return (!y ? x : _gcd(y, x % y)); }   long long _lcm(long long x, long long y) { return x / _gcd(x, y) * y; }   int main() {     std::ios_base::sync_with_stdio(0);     long long n;     string s;     cin >> n >> s;     long long a = 0, b = 0, ans = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  ) )         a++;       else         b++;     }     if (n % 2 || a != b) return cout << -1 <<   n , 0;     a = 0, b = 0;     int inx1 = 0, inx2 = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  ( ) {         a++;         inx1 = i;       } else {         b++;         inx2 = i;       }       if (a == b) {         if (inx2 < inx1) ans += a * 2;         a = 0;         b = 0;       }     }     cout << ans <<   n ;   }
#include <bits/stdc++.h>   int main() {     char x[101], y[101], z[101];     int len;     scanf( %s%s , x, y);     len = strlen(x);     for (int i = 0; i < len; i++) {       z[i] = y[i];     }     int flag = 1;     for (int i = 0; i < len; i++) {       if (y[i] > x[i]) {         flag = 0;         printf( -1 n );         break;       }     }     if (flag == 1) printf( %s , y);   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline bool cmax(T &x, T y) {     return x < y ? x = y, 1 : 0;   }   template <typename T>   inline bool cmin(T &x, T y) {     return y < x ? x = y, 1 : 0;   }   const int _ = 2e5 + 55, __ = 5e5 + 55;   int N;   long long a[_], b[_];   char ans[__];   int n, cnt;   bool check() {     for (int i(1), I(N); i <= I; ++i)       if (b[i] <= 0) return 0;     return 1;   }   bool up() {     for (int i(1), I(N - 1); i <= I; ++i)       if (b[i + 1] <= b[i]) return 0;     return 1;   }   bool down() {     for (int i(1), I(N - 1); i <= I; ++i)       if (b[i] <= b[i + 1]) return 0;     return 1;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0);     cin >> N;     for (int i(1), I(N); i <= I; ++i) cin >> a[i];     for (int i(1), I(N); i <= I; ++i) cin >> b[i];     if (N == 1) return printf(a[1] == b[1] ?  SMALL n0 n  :  IMPOSSIBLE n ), 0;     if (N == 2) {       long long x(b[1]), y(b[2]), s(0);       while (x && y) {         if (y == a[1]) swap(x, y);         if (x == a[1] && y >= a[2] && (y - a[2]) % x == 0) {           s += (y - a[2]) / x, y = a[2];           break;         }         if (y == a[2]) swap(x, y);         if (x == a[2] && y >= a[1] && (y - a[1]) % x == 0) {           s += (y - a[1]) / x, y = a[1], swap(x, y);           break;         }         if (x > y) swap(x, y);         s += y / x, y %= x;       }       if (x != a[1] || y != a[2])         return printf( IMPOSSIBLE n ), 0;       else if (s > 2e5)         return printf( BIG n%lld n , s), 0;     }     while (check()) {       int flg = 1;       for (int i(1), I(N); i <= I; ++i)         if (a[i] != b[i]) {           flg = 0;           break;         }       if (!flg) {         flg = 2;         for (int i(1), I(N); i <= I; ++i)           if (a[i] != b[N - i + 1]) {             flg = 0;             break;           }       }       if (flg) {         if (flg == 2 && cnt <= 2e5) ans[++n] =  R ;         if (cnt <= 2e5) {           printf( SMALL n%d n , n);           for (int i(n), I(1); i >= I; --i) putchar(ans[i]);           putchar(  n );         } else           printf( BIG n%d n , cnt);         return 0;       } else if (up()) {         ++cnt;         if (cnt <= 2e5) ans[++n] =  P ;         for (int i(N), I(2); i >= I; --i) b[i] -= b[i - 1];       } else if (down()) {         ++cnt;         if (cnt <= 2e5) ans[++n] =  R , ans[++n] =  P ;         reverse(b + 1, b + N + 1);         for (int i(N), I(2); i >= I; --i) b[i] -= b[i - 1];       } else         break;     }     printf( IMPOSSIBLE n );     return 0;   }
#include <iostream>   #include <cstdio>      using namespace std;      #define MAXN 200005      int t, n;   int a[MAXN];   int pre[MAXN], suf[MAXN];   bool pre_ok[MAXN], suf_ok[MAXN];   bool ans;   int main()   {       //freopen( CF#696D.in ,  r , stdin);       scanf( %d , &t);       while(t --)       {           ans = true;           scanf( %d , &n);           pre_ok[0] = suf_ok[n + 1] = true;           suf[n + 1] = 0;           for(int i = 1;i <= n;i ++) scanf( %d , a + i);              for(int i = 1;i < n;i ++)           {               pre[i] = a[i] - pre[i - 1];               pre_ok[i] = pre_ok[i - 1];               if(pre[i] < 0) {                   pre[i] = 0;                   pre_ok[i] = ans = false;               }           }           if(a[n] != pre[n - 1]) pre_ok[n] = ans = false;           for(int i = n;i > 1;i --)           {               suf[i] = a[i] - suf[i + 1];               suf_ok[i] = suf_ok[i + 1];               if(suf[i] < 0) {                   suf[i] = 0;                   suf_ok[i] = ans = false;               }           }           if(a[1] != suf[2]) suf_ok[1] = ans = false;           for(int i = 1;i < n;i ++)           {               if(pre_ok[i - 1] && suf_ok[i + 2] && a[i + 1] - pre[i - 1] >= 0 && a[i] - suf[i + 2] == a[i + 1] - pre[i - 1]) ans = true;           }           if(ans) printf( YES n );           else printf( NO n );       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 1005;   int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};   int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};   string toBinary(int x) {     string ret =   ;     while (x) {       ret = (x & 1 ?  1  :  0 ) + ret;       x >>= 1;     }     while (((int)(ret).size()) < 8) {       ret =  0  + ret;     }     return ret;   }   int toDec(string x) {     int ret = 0, val = 1;     for (int i = 7; i >= 0; --i) {       if (x[i] ==  1 ) ret += val;       val <<= 1;     }     return ret;   }   string second;   vector<int> s1;   int main() {     getline(cin, second);     s1.push_back(0);     for (int i = 0; i < (((int)(second).size()) - 1); ++i) {       string temp = toBinary(second[i]);       reverse(temp.begin(), temp.end());       s1.push_back(toDec(temp));     }     for (int i = 0; i < (((int)(second).size())); ++i) {       string temp = toBinary(second[i]);       reverse(temp.begin(), temp.end());       int res = toDec(temp);       printf( %d n , (s1[i] - res + 256) & 255);     }   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 5010;   int dp[maxn][maxn], sa[maxn], sb[maxn];   char a[maxn], b[maxn];   void gao(char *p, char *q, int n, int m) {     memset(dp, 0, sizeof(dp));     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j)         if (p[i] == q[j]) dp[i][j] = max(dp[i - 1][j - 1] + 1, dp[i][j]);   }   int main() {     scanf( %s%s , a + 1, b + 1);     int n = strlen(a + 1), m = strlen(b + 1);     gao(a, a, n, n);     for (int i = 1; i <= n; ++i) sa[i] = 1;     for (int i = 1; i <= m; ++i) sb[i] = 1;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= n; ++j)         if (i != j) sa[i] = max(sa[i], dp[i][j] + 1);     gao(b, b, m, m);     for (int i = 1; i <= m; ++i)       for (int j = 1; j <= m; ++j)         if (i != j) sb[i] = max(sb[i], dp[i][j] + 1);     gao(a, b, n, m);     int ans = -1;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j)         if (dp[i][j] >= sa[i] && dp[i][j] >= sb[j])           if (max(sa[i], sb[j]) != 0 && (ans < 0 || ans > max(sa[i], sb[j])))             ans = max(sa[i], sb[j]);     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, V[171];   void cauta(int p) {     vector<pair<int, int> > Queries;     int res, st = 2, dr = n;     for (int m = dr; m >= st; --m) {       cout <<  ?  ;       for (int i = 1; i <= n; ++i) {         if (i != p)           cout <<  1  ;         else           cout << n - m + 2 <<    ;       }       cout <<   n ;       cout.flush();       cin >> res;       if (res == 0) {         V[p] = m;       } else {         if (V[res]) {           V[p] = V[res] - 1 + m - n;         } else if (res != p)           Queries.push_back({res, n - m + 1});       }     }     if (!V[p]) V[p] = 1;     for (auto it : Queries) V[it.first] = it.second + V[p];   }   int gaseste(int dif) {     cout <<  ?  ;     for (int i = 1; i < n; ++i) {       if (dif > 0)         cout <<  1  ;       else         cout << -dif + 1 <<    ;     }     if (dif > 0)       cout << dif + 1 <<   n ;     else       cout <<  1 n ;     cout.flush();     int rep = 0;     cin >> rep;     return rep;   }   void solve() {     cin >> n;     int v, st = 0, dr = 0;     for (int i = -n + 1; i < n; ++i) {       if (!i) continue;       v = gaseste(i);       V[v] = i;       if (!v) {         if (i > 0 && !dr) dr = i;       }       if (v && i < 0 && !st) st = -i;     }     V[n] = st + 1;     for (int i = 1; i < n; ++i) V[i] += V[n];     cout <<  !  ;     for (int i = 1; i <= n; ++i) {       cout << V[i] <<    ;       V[i] = 0;     }     cout <<   n ;     cout.flush();   }   int main() {     cin.tie(0);     ios_base::sync_with_stdio(0);     int te = 1;     while (te--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k, a[55];     cin >> n >> m >> k;     for (int i = 0; i < n; i++) cin >> a[i];     a[n] = 0;     sort(a, a + n);     int ans = -1, sum = 0;     for (int i = 0; i < n + 1; i++) {       sum += a[n - i];       if (sum + k - i >= m) {         ans = i;         break;       }     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e3 + 7;   const int maxa = 1e5 + 7;   int dp[maxn][maxn];   int num[maxn], n;   int get_ans() {     memset(dp, 0, sizeof(dp));     int max_mod[7], max_num[maxa];     memset(max_mod, 0, sizeof(max_mod));     memset(max_num, 0, sizeof(max_num));     int ans = 0;     for (int i = 0; i <= n; ++i) {       for (int j = 0; j <= n; ++j) {         if (i == j) continue;         if (j < i) {           dp[i][j] = dp[j][i];           max_mod[num[j] % 7] = max(max_mod[num[j] % 7], dp[i][j]);           if (j != 0) max_num[num[j]] = max(max_num[num[j]], dp[i][j]);         } else {           dp[i][j] = dp[i][0] + 1;           dp[i][j] = max(max_mod[num[j] % 7] + 1, dp[i][j]);           dp[i][j] = max(max_num[num[j] + 1] + 1, dp[i][j]);           dp[i][j] = max(max_num[num[j] - 1] + 1, dp[i][j]);           max_mod[num[j] % 7] = max(max_mod[num[j] % 7], dp[i][j]);           max_num[num[j]] = max(max_num[num[j]], dp[i][j]);         }         if (i != 0 && j != 0) ans = max(ans, dp[i][j]);       }       memset(max_mod, 0, sizeof(max_mod));       for (int j = 1; j <= n; ++j) {         max_num[num[j]] = 0;       }     }     return ans;   }   int main() {     while (cin >> n) {       for (int i = 1; i <= n; ++i) {         scanf( %d , num + i);       }       int ans = get_ans();       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const int INF = 1000000000;   const int MOD = 1073741824;   const int M = INF;   const double RRR = 180.0 / PI;   vector<vector<pair<int, int> > > g;   vector<int> used;   void dfs(int v, int l, int r) {     used[v] = 1;     for (int i = 0; i < g[v].size(); i++) {       if (g[v][i].second >= l && g[v][i].second <= r) {         continue;       }       int to = g[v][i].first;       if (!used[to]) {         dfs(to, l, r);       }     }   }   int main() {     double TIME_START = clock();     int n, m;     cin >> n >> m;     used.resize(n);     g.resize(n);     int a, b;     for (int i = 0; i < m; i++) {       cin >> a >> b;       a--;       b--;       g[a].push_back(make_pair(b, i));       g[b].push_back(make_pair(a, i));     }     int k;     cin >> k;     int l, r;     for (int i = 0; i < k; i++) {       cin >> l >> r;       l--;       r--;       int col = 0;       used.assign(n, 0);       for (int i = 0; i < n; i++) {         if (!used[i]) {           col++;           dfs(i, l, r);         }       }       cout << col << endl;     }     fprintf(stderr,   n n%.15lf n n ,             (double)(clock() - TIME_START) / CLOCKS_PER_SEC);     return 0;   }
#include <bits/stdc++.h>   #pragma g++ opitmize(3)   #pragma g++ opitmize(2)   #pragma g++ opitmize( fast )   #pragma g++ opitmize( inline )   using namespace std;   int n, m, head[400010], k = -1, vis[400010], h[400010], tot = 1;   long long a[400010], f[400010], f1;   struct node {     int to, nt, fg;   } edge[1000010];   inline void add(int u, int v, int fg) {     edge[++k].nt = head[u];     head[u] = k;     edge[k].to = v;     edge[k].fg = fg;   }   inline long long fr_dfs(int u, int fa) {     long long tmp = 0, t = 0;     vis[u] = 1;     for (register int i = head[u]; ~i; i = edge[i].nt) {       int v = edge[i].to;       if (v == fa) continue;       if (vis[v]) continue;       tmp = fr_dfs(v, u);       t += tmp;       f[(i / 2) + 1] += tmp * edge[i ^ 1].fg;     }     if (a[u] < 0) {       long long hhd = a[u];       a[u] = 0;       return t + hhd;     }     return t;   }   inline void sc_dfs(int u, int fa, long long &flow) {     if (a[u] > 0) {       flow += a[u];       a[u] = 0;     }     vis[u] = 1;     for (register int i = head[u]; ~i; i = edge[i].nt) {       int v = edge[i].to;       if (v == fa || vis[v]) continue;       f[(i / 2) + 1] += (flow * edge[i].fg);       sc_dfs(v, u, flow);       f[(i / 2) + 1] += (flow * edge[i ^ 1].fg);     }   }   int main() {     memset(head, -1, sizeof(head));     scanf( %d , &n);     for (register int i = 1; i <= n; i++) scanf( %lld , &a[i]), f1 += a[i];     int u, v;     scanf( %d , &m);     for (register int i = 1; i <= m; i++) {       scanf( %d%d , &u, &v);       add(u, v, -1);       add(v, u, 1);     }     if (f1 != 0) {       printf( Impossible n );       return 0;     }     memset(vis, 0, sizeof(vis));     a[1] += fr_dfs(1, 0);     h[1] = 1;     for (register int i = 1; i <= n; i++) {       if (!vis[i]) {         a[i] += fr_dfs(i, 0);         h[++tot] = i;       }     }     memset(vis, 0, sizeof(vis));     for (register int i = 1; i <= tot; i++) {       sc_dfs(h[i], 0, a[h[i]]);     }     for (register int i = 1; i <= n; i++) {       if (a[i]) {         printf( Impossible n );         return 0;       }     }     printf( Possible n );     for (register int i = 1; i <= m; i++) printf( %lld n , f[i]);     return 0;   }
#include <bits/stdc++.h>   int a[5][100005];   int b[100005][5];   int main() {     int n, m;     std::cin >> n >> m;     if (n == 1 && m == 1)       std::cout <<  YES n1 n ;     else if (n == 3 && m == 3) {       std::cout <<  YES n ;       std::cout <<  5 1 9 n7 6 4 n2 8 3 n ;     } else if (std::max(n, m) <= 3) {       std::cout <<  NO n ;     } else if (m == 4 || n == 4) {       std::cout <<  YES n ;       if (m == 4) {         for (int i = 0; i < n; i++) {           if (i % 2 == 1) {             std::cout << i * 4 + 2 <<     << i * 4 + 4 <<     << i * 4 + 1 <<                           << i * 4 + 3 <<   n ;           } else {             std::cout << i * 4 + 3 <<     << i * 4 + 1 <<     << i * 4 + 4 <<                           << i * 4 + 2 <<   n ;           }         }       } else if (n == 4) {         for (int j = 1; j <= m; j++) {           if (j % 2 == 1) {             a[3][j] = j + 2 * m;             a[2][j] = j;             a[1][j] = j + 3 * m;             a[0][j] = j + m;           } else {             a[0][j] = j + 2 * m;             a[1][j] = j;             a[2][j] = j + 3 * m;             a[3][j] = j + m;           }         }         for (int i = 0; i < n; i++) {           for (int j = 1; j <= m; j++) {             std::cout << a[i][j] <<    ;           }           std::cout <<   n ;         }       }     } else {       std::cout <<  YES n ;       if (m > 4) {         for (int i = 0; i < n; i++) {           int offset = i * m;           if (i % 2 == 0) {             for (int j = 1; j < std::ceil(m / 2.0); j++) {               std::cout << offset + j <<     << offset + j + std::ceil(m / 2.0)                         <<    ;             }             if (m % 2 == 0)               std::cout << offset + std::ceil(m / 2.0) <<     << offset + m                         <<   n ;             else               std::cout << offset + std::ceil(m / 2.0) <<   n ;           } else {             for (int j = 1; j < std::ceil(m / 2.0); j++) {               std::cout << offset + j + (m / 2) <<     << offset + j <<    ;             }             if (m % 2 == 0)               std::cout << offset + m <<     << offset + std::ceil(m / 2.0)                         <<   n ;             else               std::cout << offset + m <<   n ;           }         }       } else {         for (int j = 0; j < m; j++) {           int offset = j;           if (j % 2 == 0) {             for (int i = 0; i < std::ceil(n / 2.0); i++) {               b[2 * i][j] = offset + 1 + i * m;               b[2 * i + 1][j] = offset + 1 + i * m + std::ceil(n / 2.0) * m;             }           } else {             for (int i = 0; i < std::ceil(n / 2.0); i++) {               b[2 * i][j] = offset + 1 + i * m + (n / 2) * m;               b[2 * i + 1][j] = offset + 1 + i * m;             }           }         }         for (int i = 0; i < n; i++) {           for (int j = 0; j < m; j++) {             std::cout << b[i][j] <<    ;           }           std::cout <<   n ;         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6 + 5;   int n, k;   int v[MAXN], c[MAXN], a[MAXN];   double f[MAXN];   int rv[MAXN][20], rc[MAXN][20];   inline int GetV(int l, int r) {     int x = log2(r - l + 1);     return max(rv[l][x], rv[r + 1 - (1 << x)][x]);   }   inline int GetC(int l, int r) {     int x = log2(r - l + 1);     return min(rc[l][x], rc[r + 1 - (1 << x)][x]);   }   inline int Cal(int pos) {     if (100 * v[pos] > c[pos]) return c[pos];     int left = pos, right = n, t;     while (left <= right) {       int mid = (left + right) >> 1;       if (GetV(pos, mid) * 100 <= GetC(pos, mid)) {         t = mid;         left = mid + 1;       } else         right = mid - 1;     }     int ans = GetV(pos, t) * 100;     if (t < n) ans = max(ans, GetC(pos, t + 1));     return ans;   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; ++i) scanf( %d , &v[i]);     for (int i = 1; i <= n; ++i) scanf( %d , &c[i]);     for (int i = 1; i <= n; ++i) rv[i][0] = v[i], rc[i][0] = c[i];     int lgn = log2(n);     for (int j = 1; j <= lgn; ++j) {       int last = n + 1 - (1 << j);       for (int i = 1; i <= last; ++i) {         rv[i][j] = max(rv[i][j - 1], rv[i + (1 << (j - 1))][j - 1]);         rc[i][j] = min(rc[i][j - 1], rc[i + (1 << (j - 1))][j - 1]);       }     }     for (int i = 1; i <= n; ++i) a[i] = Cal(i);     f[0] = 1.0 * k / (n - k + 1);     for (int i = 1; i <= n - k + 1; ++i)       f[i] = f[i - 1] * (n - i - k + 2) / (n - i + 1);     sort(a + 1, a + n + 1);     double ans = 0;     for (int i = 1; i <= n - k + 1; ++i) ans += a[i] * f[i];     cout << fixed << setprecision(9) << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = (1 << 30);   int dp[110][700][40], g[35][35];   string s;   int solve(int p, int k, int q) {     if (p < 0 && k >= 0) return 0;     if (p < 0 || k < 0) return -inf;     if (dp[p][k][q] != -1) return dp[p][k][q];     char ch = q +  a ;     int ans = -inf;     for (char curr =  a ; curr <=  z ; curr++) {       int dk = 0;       if (curr != s[p]) dk++;       int x = solve(p - 1, k - dk, curr -  a );       if (x == -inf) continue;       ans = max(ans, x + g[curr -  a ][q]);     }     return dp[p][k][q] = ans;   }   int main() {     fill(&dp[0][0][0], &dp[0][0][0] + 110 * 700 * 40, -1);     int k;     cin >> s >> k;     int t;     cin >> t;     for (int i = 0; i < t; i++) {       string tmp;       char c1, c2;       int w;       cin >> c1 >> c2 >> w;       g[c1 -  a ][c2 -  a ] = w;     }     cout << solve(s.size() - 1, k, 30);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int N;     cin >> N;     vector<pair<int, int>> t(N), b(N);     for (int i = 0; i < N; i++)       cin >> b[i].first >> b[i].second >> t[i].first >> t[i].second;     multiset<int> tm[2], bm[2];     for (int i = 0; i < N; i++) {       tm[0].insert(t[i].first);       tm[1].insert(t[i].second);       bm[0].insert(-b[i].first);       bm[1].insert(-b[i].second);     }     for (int i = 0; i < N; i++) {       tm[0].erase(tm[0].find(t[i].first));       tm[1].erase(tm[1].find(t[i].second));       bm[0].erase(bm[0].find(-b[i].first));       bm[1].erase(bm[1].find(-b[i].second));       pair<int, int> tp = make_pair(*tm[0].begin(), *tm[1].begin());       pair<int, int> bp = make_pair(-*bm[0].begin(), -*bm[1].begin());       if (tp.first >= bp.first && tp.second >= bp.second) {         cout << tp.first <<     << tp.second << endl;         return 0;       }       tm[0].insert(t[i].first);       tm[1].insert(t[i].second);       bm[0].insert(-b[i].first);       bm[1].insert(-b[i].second);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int count = 0;     char k[4];     cin >> k;     int a[5];     for (int i = 0; i < 4; i++) {       a[i] = k[i] -  0 ;     }     int b[5];     for (int i = 0; i < 4; i++) {       b[i] = a[i];     }     while (1) {       if (b[3] == 9) {         b[3] = 0;         if (b[2] == 9) {           b[2] = 0;           if (b[1] == 9) {             b[1] = 0;             b[0] = b[0] + 1;           } else             b[1] = b[1] + 1;         } else           b[2] = b[2] + 1;       } else         b[3] = b[3] + 1;       int i = 0;       for (int v = 0; v < 4; v++)         for (int z = v + 1; z < 4; z++) {           if (b[v] == b[z]) count++;         }       if (count == 0)         break;       else         count = 0;     }     for (int m = 0; m < 4; m++) {       cout << b[m];     }   }
#include <bits/stdc++.h>   using namespace std;   long long mod = 1e9 + 7;   long long INF = 1e18;   bool isPrime(long long n) {     if (n <= 1) return false;     if (n <= 3) return true;     if (n % 2 == 0 || n % 3 == 0) return false;     for (long long i = 5; i * i <= n; i = i + 6)       if (n % i == 0 || n % (i + 2) == 0) return false;     return true;   }   long long nCr(long long n, long long r) {     long long res = 1;     if (n < r) return -1;     r = min(r, n - r);     for (long long i = 0; i < r; i++) {       res *= (n - i);       res /= (i + 1);     }     return res;   }   long long nPr(long long n, long long r) {     long long res = 1;     if (n < r) return -1;     for (long long i = 0; i < r; i++) res *= (n - i);     return res;   }   void solve() {     long long n, m;     cin >> n >> m;     vector<long long> a(n), b(n);     for (long long i = 0; i < n; i++) {       cin >> a[i];     }     for (long long i = 0; i < n; i++) {       cin >> b[i];     }     sort(b.begin(), b.end());     long long ans = INF;     for (long long i = 0; i < n; i++) {       long long x = (b[i] - a[0] + m) % m;       vector<long long> v(n);       for (long long j = 0; j < n; j++) v[j] = (a[j] + x) % m;       sort(v.begin(), v.end());       if (v == b) ans = min(ans, x);     }     cout << ans;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t = 1;     while (t--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const int N = 1e6 + 4;   long long fac[N], infac[N];   long long me(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) res = (res * a) % mod;       b >>= 1;       a = (a * a) % mod;     }     return res;   }   long long mi(long long n) { return me(n, mod - 2); }   void factorial() {     fac[0] = 1;     for (long long i = 1; i < N; i++) fac[i] = (i * fac[i - 1]) % mod;     infac[N - 1] = mi(fac[N - 1]);     for (long long i = N - 1; i > 0; i--) infac[i - 1] = (i * infac[i]) % mod;   }   long long ncr(long long n, long long r) {     if (n < r) return 0;     long long res = fac[n];     res = (res * infac[r]) % mod;     res = (res * infac[n - r]) % mod;     return res;   }   int main() {     factorial();     long long n, m;     scanf( %lld , &n);     scanf( %lld , &m);     long long ans = me(m, n);     for (long long s = 1; s <= n; s++) {       long long val = me(m, s), temp = me(m - 1, n - s);       val = (val * temp) % mod;       val = (val * ncr(n, s - 1));       ans = (ans + val) % mod;     }     printf( %lld , ans);     return 0;   }