malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include<bits/stdc++.h>   using namespace std;   #define mod        1000000007   #define pb         push_back   #define mp         make_pair   #define rep(n)     for(int i=0;i<n;i++)   #define repit(x)   for(auto it : x)   #define ll         long long   #define vi         vector<int>   #define vc         vector<char>   #define vs         vector<string>   #define mii        map<int ,int>   #define mci        map<char ,int>   /* bool prime[100001];    const int N=100000;    void sieve() {    int i,j;   for(i=2;i<=N;i++)   prime[i]=true;    prime[0]=prime[1]=false;   for(i=2;i*i<=N;i++){   if(prime[i]){   for(j=i*i;j<=N;j+=i)   prime[j]=false;   }   }   }*/   int gcd(int a,int b){   if(b==0)   return a;   else   return gcd(b,a%b);   }   void swap(int *x,int *y){   int temp=*x;   *x=*y;   *y=temp;   }   int main()   {          ios::sync_with_stdio(0);   cin.tie(0);   cout.tie(0);   int t;   cin>>t;   while(t--){   int i,n,c=0,j;   cin>>n;   string s,s2;   cin>>s;   map<string,int>m;   for(i=97;i<=122;i++){       string s1;       s1.push_back(i);       m.insert(make_pair(s1,0));   }   for(i=0;i<n;i++){       string s1;       s1.push_back(s[i]);       m[s1]++;   }   for(auto it=m.begin();it!=m.end();it++){       if(it->second==0){           c=1;           s2=it->first;           break;       }   }   if(c){       cout<<s2<<  n ;   }   else{       m.clear();       for(i=97;i<=122;i++){           for(j=97;j<=122;j++){               string s1;               s1.push_back(i);               s1.push_back(j);               m.insert(make_pair(s1,0));           }       }       for(i=0;i<n-1;i++){           string s1;           s1.push_back(s[i]);           s1.push_back(s[i+1]);           m[s1]++;       }       for(auto it=m.begin();it!=m.end();it++){           if(it->second==0){               c=1;               s2=it->first;               break;           }       }       if(c)       cout<<s2<<  n ;       else{           m.clear();           for(i=97;i<=122;i++){               for(j=97;j<=122;j++){                   for(int k=97;k<=122;k++){                       string s1;                       s1.push_back(i);                       s1.push_back(j);                       s1.push_back(k);                       m.insert(make_pair(s1,0));                   }               }           }           for(i=0;i<n-2;i++){               string s1;               s1.push_back(s[i]);               s1.push_back(s[i+1]);               s1.push_back(s[i+2]);               m[s1]++;                          }           for(auto it=m.begin();it!=m.end();it++){               if(it->second==0){                   c=1;                   s2=it->first;                   break;               }           }           cout<<s2<<  n ;       }   }   }   return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int table[1000][1000];   int main() {     int k = 0;     scanf( %d %d %d , &n, &m, &k);     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         scanf( %d , &table[i][j]);       }     }     while (k) {       char command;       int x, y;       scanf(  %c %d %d , &command, &x, &y);       if (command ==  g )         printf( %d n , table[x - 1][y - 1]);       else if (command ==  c )         for (int i = 0; i < n; ++i) swap(table[i][x - 1], table[i][y - 1]);       else         for (int i = 0; i < m; ++i) swap(table[x - 1][i], table[y - 1][i]);       k--;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   ifstream Cin( input.txt );   ofstream Cout( output.txt );   int main() {     long long i, n4 = 0, n7 = 0, n;     string a, b;     cin >> a >> b;     for (i = 0; i < a.length(); i++)       if (a[i] != b[i]) {         if (a[i] ==  4 )           n4++;         else           n7++;       }     cout << n4 + n7 - min(n4, n7);   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   ll min(ll a, ll b) {     if (a < b) return a;     return b;   }   void solve() {     ll testCases;     cin >> testCases;     while (testCases--) {       ll n;       cin >> n;       vector<ll> arr(n);       vector<ll> res(n, 1);       for (int i = 0; i < n; i++) cin >> arr[i];       int i = 0;       for (i = 0; i < n; i++) {         if (arr[i] != arr[(i + 1) % n]) break;       }       if (i == n) {         cout <<  1 n ;         for (i = 0; i < n; i++) cout << 1 <<    ;         cout <<   n ;         continue;       }       if (n % 2 == 0) {         cout <<  2 n ;         for (i = 0; i < n; i++) cout << (i % 2) + 1 <<    ;         cout <<   n ;         continue;       } else {         bool isTwo = 0;         for (i = 0; i < n; i++) {           if (arr[i] == arr[(i + 1) % n]) {             for (int j = 0, k = i + 1; k < n; k++, j ^= 1) res[k] = j + 1;             for (int j = 0, k = i; k >= 0; k--, j ^= 1) res[k] = j + 1;             cout <<  2 n ;             for (auto e : res) cout << e <<    ;             cout <<   n ;             isTwo = 1;             break;           }         }         if (!isTwo) {           cout <<  3 n ;           for (i = 0; i < n - 1; i++) cout << i % 2 + 1 <<    ;           cout <<  3 n ;         }       }     }   }   int main() {     {       ios_base::sync_with_stdio(false);       cin.tie(NULL);     };     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxN = 3000 + 1;   pair<int, int> a[maxN];   pair<int, int> b[maxN];   int nx[maxN], pr[maxN], v[maxN];   int r, c, n;   inline int gg(int i) {     pair<int, int> x = {a[i].second, a[i].first};     return lower_bound(b, b + n + 2, x) - b;   }   inline int ff(int i) { return b[i].first; }   int nex(int i) {     if (v[nx[i]]) return nx[i];     return nx[i] = nex(nx[i]);   }   int prev(int i) {     if (v[pr[i]]) return pr[i];     return pr[i] = prev(pr[i]);   }   int L[11], R[11];   int main() {     cin >> r >> c >> n;     int k;     cin >> k;     for (int i = 0; i < n; i++) {       cin >> a[i].first >> a[i].second;       b[i] = {a[i].second, a[i].first};     }     b[n] = {0, 0}, b[n + 1] = {c + 1, 0};     sort(a, a + n);     sort(b, b + n + 2);     v[0] = v[n + 1] = 1;     pr[0] = 0, nx[n + 1] = n + 1;     long long ans = 0;     for (int i = 1; i <= r; i++) {       int ind = 0;       for (; ind < n && a[ind].first < i; ind++)         ;       if (ind == n) break;       for (int j = ind; j < n; j++) v[gg(j)] = 1;       for (int j = 1; j <= n; j++) {         nx[j] = j + 1;         pr[j] = j - 1;       }       int curr = 0;       for (int j = 1; j <= n; j++)         if (v[j]) {           int x = j;           for (int t = 1; t < k; t++, x = nex(x))             ;           curr += (ff(j) - ff(prev(j))) * (c + 1 - ff(x));         }       int ptr = n;       for (int i2 = r; i2 >= i; i2--) {         ans += curr;         for (; ptr > ind && a[ptr - 1].first >= i2; ptr--) {           L[0] = R[0] = gg(ptr - 1);           for (int t = 1; t < k; t++) {             L[t] = prev(L[t - 1]);             R[t] = nex(R[t - 1]);           }           for (int t = 0; t < k; t++) {             int left = L[t];             int right = R[k - t - 1];             curr -= (ff(left) - ff(prev(left))) * (ff(nex(right)) - ff(right));           }           v[L[0]] = 0;         }       }     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int arr[500005], brr[500005];   pair<int, int> tree[500005 * 4];   vector<int> vv[500005];   vector<pair<int, int> > qr[500005];   int ans[500005];   void build(int node, int b, int e) {     if (b == e) {       tree[node] = make_pair(500000000, 500000000);       return;     }     int left = 2 * node, right = left + 1, mid = (b + e) / 2;     build(left, b, mid);     build(right, mid + 1, e);     tree[node] = tree[left];   }   void update(int node, int b, int e, int i, int v) {     if (e < i || b > i) return;     if (b == e && b == i) {       tree[node] = make_pair(v, arr[b]);       return;     }     int left = 2 * node, right = left + 1, mid = (b + e) / 2;     update(left, b, mid, i, v);     update(right, mid + 1, e, i, v);     if (tree[left].first <= tree[right].first) {       tree[node] = tree[left];     } else       tree[node] = tree[right];   }   pair<int, int> query(int node, int b, int e, int l, int r) {     if (b > r || e < l) return make_pair(500000000, 500000000);     if (b >= l && e <= r) return tree[node];     int left = 2 * node, right = left + 1, mid = (b + e) / 2;     pair<int, int> p = query(left, b, mid, l, r);     pair<int, int> q = query(right, mid + 1, e, l, r);     if (p.first <= q.first) return p;     return q;   }   int main() {     int n;     scanf( %d , &n);     int mx = 0;     for (int i = 1; i <= n; i++) {       scanf( %d , &arr[i]);       vv[arr[i]].push_back(i);       mx = max(mx, arr[i]);     }     int q;     scanf( %d , &q);     for (int i = 1; i <= q; i++) {       int l, r;       scanf( %d %d , &l, &r);       qr[r].push_back(make_pair(l, i));     }     build(1, 1, n);     for (int i = 1; i <= n; i++) {       int id = lower_bound(vv[arr[i]].begin(), vv[arr[i]].end(), i) -                vv[arr[i]].begin();       int v;       if (id == 0) {         v = 1;         brr[arr[i]] = i;       } else {         v = vv[arr[i]][id - 1] + 1;         update(1, 1, n, brr[arr[i]], 500000000);         brr[arr[i]] = i;       }       update(1, 1, n, i, v);       for (int j = 0; j < qr[i].size(); j++) {         pair<int, int> ret = query(1, 1, n, qr[i][j].first, i);         if (qr[i][j].first >= ret.first) ans[qr[i][j].second] = ret.second;       }     }     for (int i = 1; i <= q; i++) printf( %d n , ans[i]);   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   const int MAXN = 200000;   int n, tot;   bool bz[N];   vector<int> g[N], edge[N];   int e[N * 2][2], gg[N];   int sz;   long long ans[N];   void add(int x, int y) {     e[++tot][0] = y;     e[tot][1] = gg[x];     gg[x] = tot;   }   void dfs(int x) {     bz[x] = 0;     sz++;     for (int i = gg[x]; i; i = e[i][1])       if (bz[e[i][0]]) dfs(e[i][0]);     gg[x] = 0;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       int x;       scanf( %d , &x);       for (int j = 1; 1ll * j * j <= x; j++)         if ((x % j) == 0) {           g[j].push_back(i);           if (j * j != x) g[x / j].push_back(i);         }     }     for (int i = 1; i < n; i++) {       int x, y;       scanf( %d %d , &x, &y);       edge[x].push_back(y);       edge[y].push_back(x);     }     for (int i = 1; i <= MAXN; i++)       if (g[i].size()) {         tot = 0;         for (auto u : g[i]) bz[u] = 1;         for (auto u : g[i])           for (auto v : edge[u])             if (bz[v]) add(u, v);         for (auto u : g[i])           if (bz[u]) {             sz = 0;             dfs(u);             ans[i] += 1ll * sz * (sz - 1) / 2 + sz;           }       }     for (int i = MAXN; i; i--)       for (int j = 2; 1ll * j * i <= MAXN; j++) ans[i] -= ans[i * j];     for (int i = 1; i <= MAXN; i++)       if (ans[i]) printf( %d %lld n , i, ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int k, n;     scanf( %d%d , &n, &k);     vector<int> a(n), b(n), distributed(n), p(n);     vector<vector<int> > cart(k);     for (int i = 0; i < n; ++i) {       scanf( %d%d , &a[i], &b[i]);       distributed[i] = 0;       p[i] = i;     }     for (int i = 0; i < n; ++i) {       for (int j = i + 1; j < n; ++j) {         if (a[p[i]] < a[p[j]]) {           swap(p[i], p[j]);         }       }     }     int cart_ptr = 0;     for (int i = 0; i < n && cart_ptr < k; ++i) {       if (b[p[i]] == 1) {         distributed[p[i]] = 1;         cart[cart_ptr].push_back(p[i]);         ++cart_ptr;       }     }     deque<int> available;     for (int i = 0; i < n; ++i) {       if (!distributed[p[i]]) {         available.push_back(p[i]);       }     }     while (cart_ptr < k) {       cart[cart_ptr].push_back(available.front());       available.pop_front();       ++cart_ptr;     }     while (!available.empty()) {       cart[k - 1].push_back(available.front());       available.pop_front();     }     long long ans = 0;     for (int i = 0; i < k; ++i) {       int min_cost = 2000000000;       for (int j = 0; j < (int)cart[i].size(); ++j) {         ans += a[cart[i][j]] * 2;         min_cost = min(min_cost, a[cart[i][j]]);       }       if (b[cart[i][0]] == 1) {         ans -= min_cost;       }     }     cout << ans / 2;     if (ans % 2 == 0) {       cout <<  .0  << endl;     } else {       cout <<  .5  << endl;     }     for (int i = 0; i < k; ++i) {       cout << cart[i].size();       for (int j = 0; j < (int)cart[i].size(); ++j) {         cout <<     << cart[i][j] + 1;       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = (int)1e9;   const int mod = (int)1e9 + 7;   const double pi = acos(-1.0);   const double eps = 1e-9;   const int maxn = (int)2e6 + 11;   long long n;   long long a[maxn];   long long sum[maxn];   int main() {     cin >> n;     a[1] = 2;     sum[1] = 2;     for (int i = 2; i <= 2000000; i++) {       a[i] = a[i - 1] + 3;       sum[i] = sum[i - 1] + a[i];     }     int ans = 0;     for (int i = 1; sum[i] <= n; i++) {       if ((n - sum[i]) % 3 == 0) ans++;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   map<long long, long long> mp[12];   long long a[200005], dig[200005];   long long state[200005][12];   int main() {     long long n, k;     scanf( %lld %lld , &n, &k);     for (int i = 0; i < n; i++) scanf( %lld , &a[i]);     for (int i = 0; i < n; i++) {       dig[i] = log10(a[i]) + 1;     }     for (int i = 0; i < n; i++) {       long long x = a[i];       long long tencnt = 1;       while (tencnt < 11) {         x = (x * 10LL) % k;         mp[tencnt][x]++;         state[i][tencnt] = x;         tencnt++;       }     }     long long ans = 0;     for (int i = 0; i < n; i++) {       long long len = dig[i];       long long m = (k - (a[i] % k)) % k;       if (mp[len].count(m) == 0) continue;       ans += mp[len][m];     }     for (int i = 0; i < n; i++) {       long long len = dig[i];       long long x = state[i][len];       if ((x + a[i]) % k == 0) {         ans--;       }     }     printf( %lld n , ans);   }
#include <bits/stdc++.h>   using namespace std;   const long long LINF = ~0ULL >> 1;   const int N = 1e3 + 7;   const int INF = 2e8 + 7;   const int MOD = 1e9 + 7;   const double PI = acos(-1.0);   const double EPS = 1e-8;   int n;   long long calc(long long A, long long B, long long C) {     return (A + 1) * (B + 2) * (C + 2);   }   void solve() {     long long mn = LINF, mx = -LINF, tmp;     for (int i = 1; 1ll * i * i * i <= n; ++i) {       if (n % i) continue;       for (int j = i, J = n / i; 1ll * j * j <= J; ++j) {         if (J % j) continue;         int k = n / i / j;         tmp = calc(i, j, k);         mn = min(mn, tmp), mx = max(mx, tmp);         tmp = calc(j, i, k);         mn = min(mn, tmp), mx = max(mx, tmp);         tmp = calc(k, i, j);         mn = min(mn, tmp), mx = max(mx, tmp);       }     }     cout << mn - n <<     << mx - n << endl;   }   int main() {     std::ios_base::sync_with_stdio(false);     cin >> n;     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, x, r[100000], pos[100000], flag = 1, flag2;   pair<int, int> a[100000];   vector<int> ans, ans2;   int l(int n) {     if (!n) return 1;     if (n % 10 == 4 || n % 10 == 7) return l(n / 10);     return 0;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &x);       a[i] = pair<int, int>(x, i);     }     sort(a, a + n);     for (int i = 0; i < n; i++) {       r[a[i].second] = i;       pos[i] = a[i].second;       if (a[i].second != i) flag = 0;       if (!flag2 && l(a[i].first)) x = i, flag2 = 1;     }     if (flag) {       cout << 0 << endl;       return 0;     }     if (!flag2) {       cout << -1 << endl;       return 0;     }     for (int i = 0; i < n; i++)       if (i != x) {         if (pos[i] == i) continue;         if (pos[x] != i) {           ans.push_back(pos[x]), ans2.push_back(i);           int temp = pos[x];           swap(pos[x], pos[r[i]]);           swap(r[temp], r[i]);         }         ans.push_back(pos[x]), ans2.push_back(pos[i]);         swap(pos[x], pos[i]);         swap(r[pos[x]], r[pos[i]]);       }     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); i++)       cout << ans[i] + 1 <<     << ans2[i] + 1 << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e3 + 10;   const int MOD = 998244353;   const int INF = 10;   int fact[MAXN], invfact[MAXN];   int add(int a, int b) {     int sum = a + b;     return (sum >= MOD ? sum -= MOD : (sum < 0 ? sum += MOD : sum));   }   int mul(int a, int b) { return 1LL * a * b % MOD; }   int pwr(int a, int b) {     return (!b ? 1                : (b & 1 ? 1LL * a * pwr(1LL * a * a % MOD, b >> 1) % MOD                         : pwr(1LL * a * a % MOD, b >> 1) % MOD));   }   int inv(int x) { return pwr(x, MOD - 2); }   void first() {     fact[0] = 1;     for (int i = 1; i < MAXN; i++) fact[i] = mul(fact[i - 1], i);     for (int i = 0; i < MAXN; i++) invfact[i] = inv(fact[i]);   }   int C(int n, int k) { return mul(mul(fact[n], invfact[k]), invfact[n - k]); }   int P(int n, int k) { return mul(fact[n], invfact[n - k]); }   int n, a[MAXN], dp[MAXN];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     first();     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     dp[n] = 1;     for (int i = n - 1; i >= 0; i--) {       if (a[i] <= 0 || a[i] + i - 1 > n - 1) {         continue;       }       for (int j = n - 1; j >= a[i] + i; j--) {         dp[i] = add(dp[i], mul(dp[j + 1], C(j - i, a[i])));       }     }     int ans = 0;     for (int i = 0; i < n; i++) {       ans = add(ans, dp[i]);     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int* m = new int[n];     int* r = new int[n];     for (int i = 0; i < n; i++) {       cin >> m[i];     }     for (int i = 0; i < n; i++) {       cin >> r[i];     }     double total = 0;     for (int i = 0; i < 1000000; i++) {       for (int j = 0; j < n; j++) {         if (i % m[j] == r[j]) {           total++;           break;         }       }     }     double percentage = total / 1000000;     cout << fixed << setprecision(6) << percentage << endl;   }
#include <bits/stdc++.h>   using namespace std;   int dx[] = {1, 1, -1, -1};   int dy[] = {1, -1, -1, 1};   long long int n, k, d1, d2, rem;   bool _solve(long long int w1, long long int w2, long long int w3) {     long long int tar = n / 3;     if (w1 < 0 || w2 < 0 || w3 < 0) return false;     if (tar >= w1 && tar >= w2 && tar >= w3 && abs(w1 - w2) == d1 &&         abs(w2 - w3) == d2)       return true;     return false;   }   bool solve() {     long long int _d1, w1, w2, w3, _d2;     for (int i = 0; i < 4; i++) {       _d1 = d1 * dx[i];       _d2 = d2 * dy[i];       w1 = (k + 2 * _d1 + _d2) / 3;       w2 = w1 - _d1;       w3 = w2 - _d2;       if (w1 + w2 + w3 != k) continue;       if (_solve(w1, w2, w3)) return true;     }     return false;   }   int main() {     ios_base ::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) {       cin >> n >> k >> d1 >> d2;       if (n % 3 == 0) {         if (solve())           cout <<  yes  << endl;         else           cout <<  no  << endl;       } else         cout <<  no  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 1.01e9;   const double eps = 1e-9;   const int N = 1010;   char s[N][N];   char res[N * N];   int resc = 0;   char ch[4] = { D ,  L ,  R ,  U };   int dx[4] = {1, 0, 0, -1};   int dy[4] = {0, -1, 1, 0};   pair<int, int> q[N * N];   int st = 0, en = 0;   int d[N][N];   int n, m;   void bfs(int x, int y) {     for (int i = 0; (i) < (n); ++i)       for (int j = 0; (j) < (m); ++j) d[i][j] = inf;     d[x][y] = 0;     q[en++] = {x, y};     while (st < en) {       x = q[st].first;       y = q[st].second;       st++;       for (int i = 0; (i) < (4); ++i) {         int xx = x + dx[i];         int yy = y + dy[i];         if (0 <= xx && xx < n && 0 <= yy && yy < m && s[xx][yy] ==  .  &&             d[xx][yy] == inf) {           d[xx][yy] = d[x][y] + 1;           q[en++] = {xx, yy};         }       }     }   }   int main() {     int k;     scanf( %d%d%d , &n, &m, &k);     if (k & 1) {       printf( IMPOSSIBLE n );       return 0;     }     for (int i = 0; (i) < (n); ++i) scanf( %s , s[i]);     int px = -1, py = -1;     for (int i = 0; (i) < (n); ++i)       for (int j = 0; (j) < (m); ++j)         if (s[i][j] ==  X ) {           px = i;           py = j;           s[i][j] =  . ;         }     bfs(px, py);     int x = px, y = py;     for (int it = 0; (it) < (k); ++it) {       int ok = 0;       for (int i = 0; (i) < (4); ++i) {         int xx = x + dx[i];         int yy = y + dy[i];         if (0 <= xx && xx < n && 0 <= yy && yy < m && d[xx][yy] <= k - it - 1) {           res[resc++] = ch[i];           x = xx;           y = yy;           ok = 1;           break;         }       }       if (!ok) {         printf( IMPOSSIBLE n );         return 0;       }     }     printf( %s n , res);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char s[100010], a[110], b[110];   int pa[110], pb[110];   int main() {     scanf( %s%s%s , s, a, b);     int i, j;     j = -1;     pa[0] = -1;     int lena = strlen(a);     for (i = 1; i < lena; i++) {       while (j > -1 && a[j + 1] != a[i]) j = pa[j];       if (a[j + 1] == a[i]) j++;       pa[i] = j;     }     j = -1;     pb[0] = -1;     int lenb = strlen(b);     for (i = 1; i < lenb; i++) {       while (j > -1 && b[j + 1] != b[i]) j = pb[j];       if (b[j + 1] == b[i]) j++;       pb[i] = j;     }     int len = strlen(s);     int minn = len + 1;     j = -1;     for (i = 0; i < len; i++) {       while (j > -1 && a[j + 1] != s[i]) j = pa[j];       if (a[j + 1] == s[i]) j++;       if (j == lena - 1) {         minn = i;         break;       }     }     bool flag1 = false, flag2 = false;     j = -1;     for (i = minn + 1; i < len; i++) {       while (j > -1 && b[j + 1] != s[i]) j = pb[j];       if (b[j + 1] == s[i]) j++;       if (j == lenb - 1) {         flag1 = true;         break;       }     }     for (i = 0; i < len / 2; i++) swap(s[i], s[len - i - 1]);     j = -1;     minn = len;     for (i = 0; i < len; i++) {       while (j > -1 && a[j + 1] != s[i]) j = pa[j];       if (a[j + 1] == s[i]) j++;       if (j == lena - 1) {         minn = i;         break;       }     }     j = -1;     for (i = minn + 1; i < len; i++) {       while (j > -1 && b[j + 1] != s[i]) j = pb[j];       if (b[j + 1] == s[i]) j++;       if (j == lenb - 1) {         flag2 = true;         break;       }     }     if (flag1 && flag2)       printf( both n );     else if (flag1)       printf( forward n );     else if (flag2)       printf( backward n );     else       printf( fantasy n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long N, a[100010], s[100100], cnt[100010];   void update(long long *t, long long pos, long long val) {     for (; pos <= N; pos += pos & (-pos)) t[pos] += val;   }   long long query(long long *t, long long r) {     long long res = 0;     for (; r > 0; r -= r & (-r)) res += t[r];     return res;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> N;     int i;     long long nr, sum;     long double res = 0;     for (i = 1; i <= N; i++) {       cin >> a[i];       update(cnt, a[i], 1);       update(s, a[i], i);     }     for (i = 1; i <= N; i++) {       nr = query(cnt, a[i] - 1);       sum = query(s, a[i] - 1);       res += nr - (1.0 * i * (nr * (N + 1) - sum)) / (N * (N + 1));       nr = N - i - nr;       sum = query(s, N) - sum - i;       res += (1.0 * i * (nr * (N + 1) - sum)) / (N * (N + 1));       update(cnt, a[i], -1);       update(s, a[i], -i);     }     cout << fixed << setprecision(15) << res <<   n ;     return 0;   }