malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int maxi = 1e6 + 10;   const long long mo = 1e9 + 7;   long long a[maxi];   long long m, l, r, k;   string s;   vector<int> v[maxi];   long long mat[53][3][3];   void mul_mat(int idx1, int idx2, int idx) {     for (int i = 1; i <= 2; i++)       for (int j = 1; j <= 2; j++)         for (int k = 1; k <= 2; k++)           mat[idx][i][j] += mat[idx1][i][k] * mat[idx2][k][j];     for (int i = 1; i <= 2; i++)       for (int j = 1; j <= 2; j++) mat[idx][i][j] %= m;   }   void fib(long long x) {     long long fi = 1;     long long fj = 1;     mat[51][1][1] = 1;     mat[51][2][2] = 1;     for (int i = 0; i <= 50; i++)       if (x & (1ll << i)) {         mul_mat(i, 51, 52);         for (int j = 1; j <= 2; j++)           for (int o = 1; o <= 2; o++) {             mat[51][j][o] = mat[52][j][o];             mat[52][j][o] = 0;           }       }     cout << mat[51][2][1] << endl;     return;   }   int main() {     cin >> m >> l >> r >> k;     long long x = 1;     mat[0][1][1] = 1;     mat[0][1][2] = 1;     mat[0][2][1] = 1;     mat[0][2][2] = 0;     for (long long i = 1; i * i <= r; i++) {       long long last = r / i;       long long first = (r / (i + 1)) + 1;       if (i - k >= 0) {         long long val = i - k;         long long li = last;         long long ri = first + 1;         while (li < ri - 1) {           long long mid = (li + ri) / 2;           if ((l - 1) / mid <= val)             li = mid;           else             ri = mid;         }         if ((l - 1) / li <= val) {           x = max(x, li);         }       }     }     long long sqr = floor(sqrt(r));     for (long long num = sqr; num > 0; num--) {       long long i = r / num;       long long last = r / i;       long long first = r / (i + 1) + 1;       if (i - k >= 0) {         long long val = i - k;         long long li = last;         long long ri = first + 1;         while (li < ri - 1) {           long long mid = (li + ri) / 2;           if ((l - 1) / mid <= val)             li = mid;           else             ri = mid;         }         if ((l - 1) / li <= val) {           x = max(x, li);         }       }     }     for (int i = 1; i <= 50; i++) mul_mat(i - 1, i - 1, i);     fib(x);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long read() {     char c = getchar();     long long x = 1;     long long s = 0;     while (c <  0  || c >  9 ) {       if (c ==  - ) x = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       s = s * 10 + c -  0 ;       c = getchar();     }     return s * x;   }   const long long N = 4e5 + 5;   vector<long long> v[N];   long long n, x, y, siz[N], tong;   void dfs(long long u, long long fa) {     long long cnt = 1, tmp = 0;     for (unsigned long long i = 0; i < v[u].size(); i++) {       if (v[u][i] == fa) continue;       dfs(v[u][i], u);       cnt++;       if (siz[v[u][i]] == 1) tmp++;     }     siz[u] = cnt;     if (tmp) {       tong = tong + tmp - 1;     } else {       siz[u] = 1;     }   }   void solve() {     n = read();     for (long long i = 1; i < n; i++) {       x = read();       y = read();       v[x].push_back(y);       v[y].push_back(x);     }     if (n == 2) {       cout << 1 <<   n ;     } else {       dfs(1, 0);       cout << tong + 1 <<   n ;     }     for (long long i = 1; i <= n; i++) v[i].clear(), siz[i] = 0;     tong = 0;   }   signed main() {     long long T;     cin >> T;     while (T--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1100;   const long long MOD = 998244353;   int N, K;   int arr[MAXN];   int dp[MAXN][MAXN];   int ps[MAXN][MAXN];   long long figure(int k) {     if (k * (K - 1) > arr[N - 1]) return 0;     for (int i = 0; i <= N; i++)       for (int j = 0; j <= K; j++) dp[i][j] = ps[i][j] = 0;     for (int i = 1; i <= N; i++) dp[i][1] = 1;     int cstop = 0;     for (int i = 0; i < N; i++) {       while (cstop < i && arr[i] - arr[cstop] >= k) cstop++;       for (int j = 1; j < K; j++) {         dp[i + 1][j + 1] = ps[cstop][j];       }       for (int j = 0; j <= K; j++) {         ps[i + 1][j] = (ps[i][j] + dp[i + 1][j]) % MOD;       }     }     return ps[N][K];   }   int main() {     ios_base::sync_with_stdio(0);     cin >> N >> K;     for (int i = 0; i < N; i++) cin >> arr[i];     sort(arr, arr + N);     long long ans = 0;     for (int i = 1; i <= 100000; i++) ans = (ans + figure(i)) % MOD;     cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200010;   inline int gi() {     register int x = 0;     register char c = getchar();     bool f = 0;     while (c !=  -  && (c <  0  || c >  9 )) c = getchar();     if (c ==  - ) c = getchar(), f = 1;     while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar();     return f ? -x : x;   }   struct node {     int to, next;   } g[N << 1];   int last[N], gl;   inline void add(int x, int y) {     g[++gl] = (node){y, last[x]};     last[x] = gl;     return;   }   int sum, f[N], siz[N], rt;   bool vis[N];   void getroot(int u, int fa) {     siz[u] = 1;     f[u] = 0;     for (int i = last[u]; i; i = g[i].next) {       int v = g[i].to;       if (v == fa || vis[v]) continue;       getroot(v, u);       f[u] = max(f[u], siz[v]);       siz[u] += siz[v];     }     f[u] = max(f[u], sum - siz[u]);     if (f[rt] > f[u]) rt = u;     return;   }   char a[N];   int s[N], t[10050000];   void dfs(int x, int fa, int p, int S) {     t[S ^= (1 << s[x])] += p;     for (int i = last[x]; i; i = g[i].next) {       int v = g[i].to;       if (v == fa || vis[v]) continue;       dfs(v, x, p, S);     }   }   long long ans[N];   long long calc(int x, int fa, int S) {     S ^= (1 << s[x]);     long long cnt = t[S];     for (int i = 0; i < 20; i++) cnt += t[S ^ (1 << i)];     for (int i = last[x]; i; i = g[i].next) {       int v = g[i].to;       if (v == fa || vis[v]) continue;       cnt += calc(v, x, S);     }     ans[x] += cnt;     return cnt;   }   void solve(int x) {     vis[x] = 1;     dfs(x, 0, 1, 0);     long long cnt = t[0];     for (int i = 0; i < 20; i++) cnt += t[1 << i];     for (int i = last[x]; i; i = g[i].next) {       int v = g[i].to;       if (vis[v]) continue;       dfs(v, x, -1, 1 << s[x]);       cnt += calc(v, x, 0);       dfs(v, x, 1, 1 << s[x]);     }     dfs(x, 0, -1, 0);     ans[x] += cnt / 2;     for (int i = last[x]; i; i = g[i].next) {       int v = g[i].to;       if (vis[v]) continue;       sum = siz[v];       rt = 0;       getroot(v, 0);       solve(rt);     }     return;   }   int main() {     int n = gi();     for (int i = 1; i < n; i++) {       int u = gi(), v = gi();       add(u, v);       add(v, u);     }     scanf( %s , a);     for (int i = 0; i < n; i++) s[i + 1] = a[i] -  a ;     f[0] = sum = n;     rt = 0;     getroot(1, 0);     solve(rt);     for (int i = 1; i <= n; i++) printf( %lld  , ans[i] + 1);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int r[200], l[200];   int main() {     memset(l, 10, sizeof(l));     memset(r, -1, sizeof(r));     cin.sync_with_stdio(false);     cin >> n >> m;     string s;     int w = 0;     for (int i = 0; i < n; ++i) {       cin >> s;       for (int j = 0; j < m; ++j)         if (s[j] ==  W ) {           if (l[i] > m) l[i] = j;           r[i] = j;           w++;         }     }     if (!w) {       cout << 0;       return 0;     }     int e = 0;     for (int i = n - 1; i >= 0; --i)       if (l[i] < m) {         e = i + 1;         break;       }     int j = 0;     int res = 0;     for (int i = 0; i < n; ++i) {       if (i == e) {         --res;         break;       }       if (i & 1) {         if (i == n - 1)           res += j - l[i];         else {           int k = min(l[i], l[i + 1]);           if (k >= m) k = j;           res += abs(j - k) + 1;           j = k;         }       } else {         if (i == n - 1)           res += r[i] - j;         else {           int k = max(r[i], r[i + 1]);           if (k == -1) k = j;           res += abs(k - j) + 1;           j = k;         }       }     }     cout << res;     return 0;   }
#include <bits/stdc++.h>   int main() {     int sum, n, i, t, p, sum1;     char z[260];     scanf( %d , &n);     getchar();     for (i = 0; i < n; i++) {       scanf( %c , &z[i]);     }     getchar();     sum = 0;     sum1 = 0;     p = 0;     t = 0;     for (i = 0; i < n; i++) {       if (z[i] >=  a  && z[i] <=  z ) {         sum++;         if (sum >= t) t = sum;       }       if (z[i] >=  A  && z[i] <=  Z ) {         sum++;         if (sum >= t) t = sum;       }       if (z[i] ==  _ ) {         sum = 0;       }       if (z[i] ==  ( ) {         sum = 0;         i++;         for (;; i++) {           if (z[i] >=  a  && z[i] <=  z ) {             sum++;             if (sum > p) p = sum;           }           if (z[i] >=  A  && z[i] <=  Z ) {             sum++;             if (sum > p) p = sum;           }           if (z[i] ==  _ ) {             p = sum;             if (sum != 0) sum1++;             sum = 0;           }           if (z[i] ==  ) ) {             if (sum != 0) sum1++;             sum = 0;             break;           }         }       }     }     printf( %d %d , t, sum1);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class A, class B>   istream &operator>>(istream &o, pair<A, B> &p) {     o >> p.first >> p.second;     return o;   }   template <class type>   istream &operator>>(istream &o, vector<type> &v) {     for (int i = 0; i < (int)v.size(); i++) o >> v[i];     return o;   }   template <class A, class B>   ostream &operator<<(ostream &o, pair<A, B> p) {     o <<  (  << p.first <<  ,  << p.second <<  ) ;     return o;   }   template <class type>   ostream &operator<<(ostream &o, vector<type> v) {     o <<  { ;     for (int i = 0; i < (int)v.size(); i++) {       if (i > 0) o <<  , ;       o << v[i];     }     o <<  } ;     return o;   }   long long toNum(string a) {     if (a.size() == 0u) return 0;     long long ret = 0;     for (int i = (a[0] ==  - ); i < int(a.size()); i++)       ret = ret * 10 + a[i] -  0 ;     return ret * (a[0] ==  -  ? -1 : 1);   }   string toStr(long long a) {     string ret =   ;     bool sgn = a < 0;     a = a < 0 ? -a : a;     do {       ret.push_back(a % 10 +  0 );       a /= 10;     } while (a > 0);     if (sgn) ret.push_back( - );     reverse(ret.begin(), ret.end());     return ret;   }   int dp[1005][2][2];   vector<int> cost, a, b;   int countCost(int a, int t) {     switch (t) {       case 0:         return a * cost[t];       default:         return cost[t];     }   }   int solve(int n, int bs, int tr) {     if (n < 0) return 0;     if (bs == 1 && tr == 1) return 0;     int &ret = dp[n][bs][tr];     if (ret != -1) return ret;     ret = (1 << 30);     for (int i = 0; i < int(3); i++)       for (int j = 0; j < int(3); j++) {         int t = countCost(a[n] * (!bs), i) + countCost(b[n] * (!tr), j);         ret = min(ret, t + solve(n - 1, (i == 2) || bs, (j == 2) || tr));       }     return ret;   }   int main() {     ios_base::sync_with_stdio(false);     cost = vector<int>(4);     cin >> cost;     int m, n;     cin >> m >> n;     a = b = vector<int>(max(m, n));     for (int i = 0; i < int(m); i++) cin >> a[i];     for (int i = 0; i < int(n); i++) cin >> b[i];     n = max(m, n);     memset(dp, -1, sizeof(dp));     cout << min(cost[3], solve(n - 1, 0, 0));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a[100010], b[100010], x[100010], y[100010];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long n, m, ans = 0;     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> a[i];     a[0] = 0;     a[n + 1] = m;     for (int i = 1; i <= n + 1; i++) {       b[i] = a[i] - a[i - 1];       if (i & 1) ans += b[i];       x[i] = x[i - 1];       y[i] = y[i - 1];       if (i & 1)         x[i] += b[i];       else         y[i] += b[i];     }     for (int i = 1; i <= n + 1; i++) {       if (b[i] > 1) {         ans = max(ans, x[i - 1] + y[n + 1] - y[i] + b[i] - 1);       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int S = 1003;   bool a[S];   bool b[S];   string s;   int k;   int i, j;   int main() {     cin >> s;     j = s.size() - 1;     while (i < j) {       if (s[i] ==  (  && s[j] ==  ) ) {         b[i] = 1;         b[j] = 1;         k++;         i++;         j--;       } else {         if (s[i] ==  ) ) i++;         if (s[j] ==  ( ) j--;       }     }     if (k == 0) {       cout <<  0 ;       return 0;     };     cout << 1 << endl;     cout << 2 * k << endl;     for (int i = 0; i < s.size(); i++)       if (b[i]) cout << i + 1 <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int N, M;   string str[2005];   long long pr[2005][2005], pc[2005][2005];   long long dpr[2005][2005], dpd[2005][2005];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> N >> M;     for (int i = 0; i <= N - 1; i++) cin >> str[i];     bool has = 0;     for (int i = N - 1; i >= 0; i--) {       for (int j = M - 1; j >= 0; j--) {         pr[i][j] = (i == N - 1) ? 0 : pr[i + 1][j] + (str[i + 1][j] ==  R );         pc[i][j] = (j == M - 1) ? 0 : pc[i][j + 1] + (str[i][j + 1] ==  R );         if (str[i][j] ==  R ) has = 1;       }     }     if (N == 1 || M == 1) {       cout << (has ? 0 : 1) <<   n ;       return 0;     }     long long ans = 0;     for (int i = N - 1; i >= 0; i--) {       for (int j = M - 1; j >= 0; j--) {         if (i == N - 1) {           if (pc[i][j] == 0) dpr[i][j] = 1;           dpr[i][j] = (dpr[i][j] + dpr[i + 1][j]) % 1000000007;         } else if (j == M - 1) {           if (pr[i][j] == 0) dpd[i][j] = 1;           dpd[i][j] = (dpd[i][j] + dpd[i][j + 1]) % 1000000007;         } else {           int c = pc[i][j];           dpr[i][j] = (dpd[i][j + 1] - dpd[i][M - c] + 1000000007) % 1000000007;           c = pr[i][j];           dpd[i][j] = (dpr[i + 1][j] - dpr[N - c][j] + 1000000007) % 1000000007;           if (i == 0 && j == 0) ans = (dpd[0][0] + dpr[0][0]) % 1000000007;           dpr[i][j] = (dpr[i][j] + dpr[i + 1][j]) % 1000000007;           dpd[i][j] = (dpd[i][j] + dpd[i][j + 1]) % 1000000007;         }       }     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > b, c;   map<vector<pair<int, int> >, int> d;   ;   int n, m, ask, a[100086];   long long ans;   int main() {     scanf( %d %d , &n, &m);     ans = 0ll;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       b.clear();       c.clear();       int qs = a[i];       for (int j = 2; j * j <= qs; j++) {         if (a[i] % j == 0) {           int sum = 0;           while (a[i] % j == 0) {             a[i] /= j;             sum++;           }           sum %= m;           if (sum) {             b.push_back(make_pair(j, sum));             c.push_back(make_pair(j, m - sum));           }         }       }       if (a[i] > 1) {         b.push_back(make_pair(a[i], 1));         c.push_back(make_pair(a[i], m - 1));       }       ans = ans + 1ll * d[c];       d[b]++;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long _pow(long long base, long long exp) {     return exp == 0 ? 1 : base * _pow(base, exp - 1);   }   string int_to_str(int x) {     string xs;     stringstream ss;     ss << x;     xs = ss.str();     return xs;   }   int str_vec_find(string x, vector<string> y) {     vector<string>::iterator it;     it = find(y.begin(), y.end(), x);     int pos = distance(y.begin(), it);     return pos;   }   int main() {     long long temp, tempa, tempb, maxa, maxb;     vector<long long> a, b;     int n;     cin >> n;     maxa = maxb = -1;     for (int i = 0; i < n; i++) {       cin >> tempa >> tempb;       if (tempa >= maxa) maxa = tempa;       if (tempb >= maxb) maxb = tempb;       a.push_back(tempa);       b.push_back(tempb);     }     for (int w = 0; w < b.size(); w++) {       for (int r = 0; r < b.size() - 1; r++) {         if (a[r] > a[r + 1]) {           temp = a[r];           a[r] = a[r + 1];           a[r + 1] = temp;           temp = b[r];           b[r] = b[r + 1];           b[r + 1] = temp;         }       }     }     if (n == 1)       cout << maxb << endl;     else {       long long stamp = b[0];       for (int i = 1; i < n; i++) {         if (b[i] >= stamp)           stamp = b[i];         else {           if (a[i] != a[i - 1]) stamp = a[i];         }       }       cout << stamp << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long nc2[(int)1e5];   void pre() {     for (long long i = 1; i < 1e5; ++i) {       nc2[i] = i * (i - 1) / 2;     }   }   int binSearch(int l, int r, int val) {     while (l <= r) {       int mid = (l + r) / 2;       if (nc2[mid] == val)         return mid;       else if (nc2[mid] < val)         l = mid + 1;       else         r = mid - 1;     }     return -1;   }   bool isPossible(int a00, int a01, int a10, int a11, int n, int m) {     if (a10 + a01 != n * m or abs(a10 - a01) % 2 != n * m % 2) {       return false;     }     if (!n) {       for (int i = 0; i < m; ++i) cout << 0;       return true;     }     if (!m) {       for (int i = 0; i < n; ++i) cout << 1;       return true;     }     for (int i = 0; i < a01 / n; ++i) cout << 0;     for (int i = 0; i < n - (a01 % n); ++i) cout << 1;     if (a01 / n < m) cout << 0;     for (int i = 0; i < a01 % n; ++i) cout << 1;     for (int i = 0; i < (m - a01 / n - 1); ++i) cout << 0;     return true;   }   int main() {     pre();     int a00, a01, a10, a11;     cin >> a00 >> a01 >> a10 >> a11;     int n = binSearch(1, 1e5 - 1, a11), m = binSearch(1, 1e5 - 1, a00);     if (n == -1 or m == -1 or (n == 0 and m == 0)) {       cout <<  Impossible ;       return 0;     }     if (!n) {       for (int i = 0; i < m; ++i) cout << 0;       return 0;     }     if (!m) {       for (int i = 0; i < n; ++i) cout << 1;       return 0;     }     bool ans = false;     if (n == 1 and m == 1) {       ans = ans or isPossible(a00, a01, a10, a11, n, m) or             isPossible(a00, a01, a10, a11, n, 0) or             isPossible(a00, a01, a10, a11, 0, m);       if (!ans) cout <<  Impossible ;     } else if (n == 1) {       ans = ans or isPossible(a00, a01, a10, a11, 0, m) or             isPossible(a00, a01, a10, a11, n, m);       if (!ans) cout <<  Impossible ;     } else if (m == 1) {       ans = ans or isPossible(a00, a01, a10, a11, n, 0) or             isPossible(a00, a01, a10, a11, n, m);       if (!ans) cout <<  Impossible ;     } else {       ans = isPossible(a00, a01, a10, a11, n, m);       if (!ans) cout <<  Impossible ;     }   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   ostream& operator<<(ostream& os, vector<T> v) {     for (T x : v) os << x <<    ;     return os;   }   template <class T>   istream& operator>>(istream& is, vector<T> v) {     for (T& x : v) is >> x;     return is;   }   const long long INF = 1LL << 60;   bool startsWith(string s, string t) {     return s.size() >= t.size() && s.substr(0, t.size()) == t;   }   string chomp(string s) {     if (s.empty()) return   ;     long long start = -1, end = -1;     for (long long i = 0; i < (long long)(s.size()); i++) {       if (s[i] ==    ) continue;       start = i;       break;     }     for (long long i = s.size() - 1; i >= 0; i--) {       if (s[i] ==    ) continue;       end = i;       break;     }     string ans =   ;     for (long long i = start; i <= end; i++) ans.push_back(s[i]);     return ans;   }   string parseThrow(string s) {     long long a, b;     for (long long i = 0; i < (long long)(s.size()); i++) {       char c = s[i];       if (c ==  ( )         a = i;       else if (c ==  ) )         b = i;     }     return chomp(s.substr(a + 1, b - a - 1));   }   pair<string, string> parseCatch(string s) {     string a, b;     long long x, y = -1, z, w;     for (long long i = 0; i < (long long)(s.size()); i++) {       char c = s[i];       if (c ==  ( ) {         x = i;       } else if (c ==    ) {         if (y == -1)           y = i;         else           z = i;       } else if (c ==  , )         w = i;     }     a = chomp(s.substr(x + 1, w - x - 1));     b = chomp(s.substr(y + 1, z - y - 1));     return make_pair(a, b);   }   void run() {     long long n;     cin >> n;     cin.ignore();     vector<string> v;     string buf;     for (long long i = 0; i < (long long)(n); i++) {       getline(cin, buf);       v.push_back(chomp(buf));     }     string exn;     bool flg = false;     long long depth = 0;     for (long long i = 0; i < (long long)(n); i++) {       string s = v[i];       if (s.empty()) continue;       if (startsWith(s,  throw )) {         flg = true;         exn = parseThrow(s);       }       if (!flg) continue;       if (startsWith(s,  try )) {         depth++;       } else if (startsWith(s,  catch )) {         if (depth > 0) {           depth--;           continue;         } else {           pair<string, string> e = parseCatch(s);           if (e.first == exn) {             cout << e.second << endl;             return;           }         }       }     }     cout <<  Unhandled Exception  << endl;   }   int main() {     run();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000 + 5;   const int mod = 1000 * 1000 * 1000 + 7;   vector<int> p[maxn];   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       int k;       cin >> k;       p[i].resize(k);       for (int j = 0; j < k; j++) cin >> p[i][j];       sort(p[i].begin(), p[i].end());     }     int ans = 0;     for (int i = 0; i < n; i++) {       int pre = i ? i - 1 : n - 1;       int nxt = (i + 1) % n;       int p1 = 0, p2 = 0;       for (int j = 0; j < (int)p[i].size(); j++) {         int cnt = 0;         while (p1 < (int)p[pre].size() && p[pre][p1] < p[i][j]) cnt++, p1++;         while (p2 < (int)p[nxt].size() && p[nxt][p2] < p[i][j]) cnt--, p2++;         if (cnt != 0 && j != 0) ans++;       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   int t;   long long n;   vector<int> v;   long long cnt[30];   long long pos[30];   long long fact[21];   void init() {     for (int i = 0; i < int(30); i++) cnt[i] = pos[i] = 0;     v.clear();   }   int main() {     cin.tie(0);     ios_base::sync_with_stdio(false);     fact[0] = 1;     for (int i = 1; i <= 20; i++) fact[i] = fact[i - 1] * i;     cin >> t;     while (t--) {       init();       long long n;       cin >> n;       int cur = 2;       while (n) {         v.push_back(n % cur);         n /= cur;         cur++;       }       sort(v.begin(), v.end());       for (int i = 0; i < int(v.size()); i++) cnt[v[i]]++;       pos[1] = cnt[0];       for (int i = 2; i <= v.size() + 1; i++) pos[i] = pos[i - 1] + cnt[i - 1];       long long A = 1;       for (int i = 2; i <= v.size() + 1; i++) A *= (pos[i] - (i - 2));       long long B = 1;       if (cnt[0] == 0)         B = 0;       else {         B = cnt[0];         for (int i = 2; i <= v.size(); i++) B *= (pos[i] - (i - 1));       }       long long ans = A - B;       for (int i = 0; i < int(21); i++) ans /= fact[cnt[i]];       cout << ans - 1 <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int n, a[12345];   int calc(int x, int y) {     int ret = 0;     vector<int> v;     for (int i = 0; i < 2 * n; ++i) {       if (i != x && i != y) v.push_back(a[i]);     }     for (int i = 0; i < v.size(); i += 2) ret += abs(v[i] - v[i + 1]);     return ret;   }   int main() {     cin >> n;     int mn = 1e9, i;     for (i = 0; i < 2 * n; i++) cin >> a[i];     sort(a, a + 2 * n);     for (int i = 0; i < n * 2; i++) {       for (int j = i + 1; j < n * 2; j++) {         mn = min(mn, calc(i, j));       }     }     cout << mn;   }