malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   template <class t>   vector<t> sort(vector<t> &a) {     return sort(a.begin(), a.end());   }   const int N = 200200;   int a[N], b[N];   int main() {     ios_base::sync_with_stdio(false);     cin.tie();     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < m; i++) cin >> b[i];     reverse(a, a + n);     reverse(b, b + m);     a[n] = -1;     int pos = 0, mn = a[0];     while (pos < n && mn > b[0]) pos++, mn = min(mn, a[pos]);     if (pos == n || mn < b[0]) {       puts( 0 );       return 0;     }     assert(mn == b[0]);     long long res = 1;     for (int itr = 0; itr < m - 1; itr++) {       bool f = 1;       int npos = pos;       while (npos < n && mn != b[itr + 1]) {         npos++;         mn = min(mn, a[npos]);         if (f && mn < b[itr]) {           f = 0;           res = (res * (npos - pos)) % 998244353;         }       }       if (npos == n || mn != b[itr + 1]) {         puts( 0 );         return 0;       }       pos = npos;     }     if (*min_element(a + pos, a + n) != b[m - 1]) {       puts( 0 );       return 0;     }     cout << res <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   string str[110];   int rev[110];   bool counted[110];   stack<int> st;   queue<int> q;   int length;   bool isRev(string s, string t) {     for (int i = 0; i < m; i++)       if (s[i] != t[m - i - 1]) return false;     return true;   }   bool same(string s) {     char c = s[0];     for (int i = 1; i < m; i++)       if (c != s[i]) return false;     return true;   }   bool loneRev(string s) {     int i = 0, j = m - 1;     while (i <= j) {       if (s[i] != s[j]) return false;       i++;       j--;     }     return true;   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> str[i];     for (int i = 1; i <= n; i++) {       if (!rev[i]) {         for (int j = i + 1; j <= n; j++) {           if (isRev(str[i], str[j])) {             rev[i] = j;             rev[j] = i;             break;           }         }       }     }     for (int i = 1; i <= n; i++) {       if (rev[i] && !counted[i]) {         counted[i] = true;         counted[rev[i]] = true;         q.push(i);         st.push(rev[i]);         length += (2 * m);       }     }     for (int i = 1; i <= n; i++) {       if (!counted[i]) {         if (same(str[i]) || loneRev(str[i])) {           length += m;           q.push(i);           break;         }       }     }     while (st.size()) {       int x = st.top();       st.pop();       q.push(x);     }     cout << length << endl;     while (q.size()) {       int x = q.front();       q.pop();       cout << str[x];     }   }
#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > g;   vector<vector<int> > tims;   vector<int> part;   vector<int> conf;   int n;   int e;   int T = 0;   void build_part() {     bool ok = false;     while (!ok) {       for (int(i) = (1); (i) < (T + 1); ++(i)) {         part[i] = rand() % 2;       }       for (int(i) = 0; (i) < (n); ++(i)) {         ok = false;         int cnt[] = {0, 0};         for (int(j) = 0; (j) < (tims[i].size()); ++(j)) {           cnt[part[tims[i][j]]] = 1;           if (cnt[0] * cnt[1] == 1) {             ok = true;             break;           }         }         if (!ok) {           break;         }       }     }     for (int(i) = (1); (i) < (T + 1); ++(i)) {       ++part[i];     }   }   void build() {     conf[0] = 1;     for (int(i) = (1); (i) < (n); ++(i)) {       int d[] = {0, 0};       for (int(j) = 0; (j) < (g[i].size()); ++(j)) {         if (conf[g[i][j]] > 0) {           ++d[conf[g[i][j]] - 1];         }       }       if (d[0] > d[1]) {         conf[i] = 2;       } else         conf[i] = 1;     }   }   void solve() {     srand(22812323);     cin >> n >> e;     g.resize(n);     tims.resize(n);     conf.resize(n, 0);     for (int(i) = 0; (i) < (e); ++(i)) {       int a, b;       cin >> a >> b;       --a;       --b;       g[a].push_back(b);       g[b].push_back(a);     }     for (int(i) = 0; (i) < (n); ++(i)) {       int l;       cin >> l;       tims[i].resize(l);       for (int(j) = 0; (j) < (tims[i].size()); ++(j)) {         cin >> tims[i][j];         T = max(T, tims[i][j]);       }     }     part.resize(T + 1);     build_part();     build();     for (int(i) = 0; (i) < (n); ++(i)) {       for (int(j) = 0; (j) < (tims[i].size()); ++(j)) {         if (conf[i] == part[tims[i][j]]) {           cout << tims[i][j] <<    ;           break;         }       }     }     cout << endl;     for (int(i) = (1); (i) < (T + 1); ++(i)) {       cout << part[i] <<    ;     }   }   int main() {     ios_base::sync_with_stdio(false);     solve();     return 0;   }
#include <bits/stdc++.h>   int main(void) {     int stack[100000 + 1], sp = 0, c;     stack[0] =   0 ;     while (c = getchar(), c !=   n ) {       if (c == stack[sp])         --sp;       else         stack[++sp] = c;     }     printf( %s n , sp == 0 ?  Yes  :  No );     exit(EXIT_SUCCESS);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<vector<int> > A(n, vector<int>(m)), B(n, vector<int>(m));     for (int x = 0; x < n; x++)       for (int y = 0; y < m; y++) cin >> A[x][y];     for (int x = 0; x < n; x++)       for (int y = 0; y < m; y++) cin >> B[x][y];     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++)         if (A[i][j] < B[i][j]) swap(A[i][j], B[i][j]);     }     bool legal = true;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (i + 1 < n && A[i][j] >= A[i + 1][j]) legal = false;         if (j + 1 < m && A[i][j] >= A[i][j + 1]) legal = false;       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (i + 1 < n && B[i][j] >= B[i + 1][j]) legal = false;         if (j + 1 < m && B[i][j] >= B[i][j + 1]) legal = false;       }     }     if (legal)       cout <<  Possible n ;     else       cout <<  Impossible n ;   }
#include <bits/stdc++.h>   const int maxi = 2000000000;   const int maxq = 1000000000;   const double eps = 1e-10;   const double pi = 3.1415926535897932;   const double inf = 1e+18;   const int mo = 1000000007;   const int maxn = 500000;   using namespace std;   int n;   string s[555];   vector<char> v;   int le[555][555], color[5555], kol[5555];   void dfs(char ch) {     if (color[ch] == 1) {       puts( Impossible );       exit(0);     } else if (color[ch] == 2)       return;     color[ch] = 1;     for (char i =  a ; i <=  z ; i++)       if (le[ch][i]) {         dfs(i);       }     v.push_back(ch);     color[ch] = 2;   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> s[i];     }     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++) {         bool ok = false;         for (int k = 0; k < min(s[i].size(), s[j].size()); k++)           if (s[i][k] != s[j][k]) {             le[s[i][k]][s[j][k]] = 1;             kol[s[j][k]]++;             ok = true;             break;           }         if (ok == false && s[j].size() < s[i].size()) {           puts( Impossible );           return 0;         }       }     }     for (char ch =  a ; ch <=  z ; ch++)       if (color[ch] == 0 && kol[ch] == 0) dfs(ch);     reverse(v.begin(), v.end());     if (v.size() < 26) {       puts( Impossible );       return 0;     }     for (int i = 0; i < v.size(); i++) cout << v[i];     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct Vec2 {     int x, y;     bool left, right, top, bottom;     Vec2() : x(0), y(0), left(false), right(false), top(false), bottom(false) {}   };   int main() {     int n;     Vec2 points[200];     int ans = 0;     cin >> n;     for (int i = 0; i < n; ++i) cin >> points[i].x >> points[i].y;     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j) {         if (points[i].x > points[j].x && points[i].y == points[j].y)           points[j].right = true;         if (points[i].x < points[j].x && points[i].y == points[j].y)           points[j].left = true;         if (points[i].y < points[j].y && points[i].x == points[j].x)           points[j].bottom = true;         if (points[i].y > points[j].y && points[i].x == points[j].x)           points[j].top = true;       }     for (int i = 0; i < n; ++i)       if (points[i].left && points[i].right && points[i].top && points[i].bottom)         ans += 1;     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long n, x, y;       cin >> n >> x >> y;       long long val;       for (int i = n - 1; i >= 1; i--) {         if (abs(y - x) % i == 0) {           val = (y - x) / i;           break;         }       }       vector<int> v;       for (int i = x; i <= y; i += val) {         v.push_back(i);       }       if (v.size() == n) {         for (int i = 0; i < v.size(); i++) cout << v[i] <<    ;         cout <<   n ;       } else {         long long m = n - v.size();         while (m--) {           if ((v[0] - val) > 0)             v.insert(v.begin(), v[0] - val);           else             v.push_back(v[v.size() - 1] + val);         }         for (int i = 0; i < v.size(); i++) cout << v[i] <<    ;         cout <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   int v[1024];   int vv[1024];   int N;   int try_it(int last) {     for (int i = (0); i < (N); i++) vv[i] = v[i];     for (int i = (last); i < (N); i++) vv[i] = max(0, vv[i] - 1);     vv[N] = 0;     int NN = N;     while (NN > 0 && vv[NN - 1] == 0) --NN;     int moves = 0, p = -1;     while (vv[NN - 1] > 0) {       moves += 3;       ++p;       while (vv[p + 1] >= vv[p]) {         moves += 2;         ++p;       }       ++moves;       --vv[p];       --p;       while (p >= 0 && vv[p] == vv[p + 1] + 1) {         --vv[p];         --p;         moves++;       }       moves++;     }     return moves + N * 3 - 2 * p - last;   }   void do_it(int last) {     for (int i = (0); i < (N); i++) vv[i] = v[i];     for (int i = (last); i < (N); i++) vv[i] = max(0, vv[i] - 1);     vv[N] = 0;     int NN = N;     while (NN > 0 && vv[NN - 1] == 0) --NN;     int p = -1;     while (vv[NN - 1] > 0) {       cout <<  ARA ;       ++p;       while (vv[p + 1] >= vv[p]) {         cout <<  RA ;         ++p;       }       cout <<  L ;       --vv[p];       --p;       while (p >= 0 && vv[p] == vv[p + 1] + 1) {         --vv[p];         --p;         cout <<  L ;       }       cout <<  A ;     }     while (p++ < N - 1) cout <<  AR ;     cout <<  A ;     while (p-- > last) cout <<  L ;     cout <<  A n ;   }   int main() {     ios::sync_with_stdio(false);     cin >> N;     for (int i = (0); i < (N); i++) cin >> v[i];     v[N] = 0;     while (N > 0 && v[N - 1] == 0) --N;     int best = try_it(0);     int last = 0;     for (int i = (1); i < (N); i++) {       int c = try_it(i);       if (c < best) {         best = c;         last = i;       }     }     do_it(last);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t = 1;     cin >> t;     while (t--) {       long long int i, j, k, n, m = 1e10, f = 1;       cin >> n;       vector<long long int> a(n), b(n), in, val;       for (i = 0; i < n; i++) {         cin >> a[i], b[i] = a[i];         m = min(a[i], m);       }       sort(b.begin(), b.end());       for (i = 0; i < n; i++)         if (a[i] % m == 0) in.emplace_back(i), val.emplace_back(a[i]);       sort(val.begin(), val.end());       for (i = 0; i < in.size(); i++) a[in[i]] = val[i];       for (i = 0; i < n; i++)         if (a[i] != b[i]) {           f = 0;           break;         }       if (f)         cout <<  YES n ;       else         cout <<  NO n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9;   const int maxn = 5555;   int s[maxn];   int main(int argc, char const *argv[]) {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; ++i) {       int t;       cin >> t;       s[i] = s[i - 1] + t;     }     int res = 0;     for (int i = 1; i <= n; ++i) {       for (int j = i; j <= n; ++j) {         int sum = s[j] - s[i - 1];         int t = j - i + 1;         if (sum > t * 100 && t > res) res = t;       }     }     cout << res << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a, b, c[101], d, e, f, g;   int main() {     cin >> a >> b;     for (; d < a; d++) {       cin >> e;       c[e]++;       f = max(f, c[e]);       if (c[e] == 1) {         g++;       }     }     f = (f % b) ? (f / b + 1) * b : f;     cout << f * g - a;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct edge {     long long a, b, w, ind;   };   const int LEN = 100100;   const long long INF = 100000000000000;   int n, m, s, t, use[LEN], use1[LEN], ans[LEN], timer, tin[LEN], fup[LEN];   vector<edge> ar[LEN], ar1[LEN], ar2[LEN];   edge ed[LEN];   long long dist[LEN], dist1[LEN], min_dist, a, b, l;   set<pair<long long, int> > q;   void deixtra(long long (&dist)[LEN], vector<edge> (&ar)[LEN], int s);   void get_gate(int v, int ind);   int main() {     scanf( %i %i %i %i , &n, &m, &s, &t);     s--;     t--;     for (int i = 0; i < m; i++) {       scanf( %I64d %I64d %I64d , &a, &b, &l);       ed[i].w = l;       ed[i].ind = i;       ed[i].a = b - 1;       ed[i].b = a - 1;       ar1[b - 1].push_back(ed[i]);       swap(ed[i].a, ed[i].b);       ar[a - 1].push_back(ed[i]);     }     deixtra(dist, ar, s);     deixtra(dist1, ar1, t);     min_dist = dist[t];     for (int i = 0; i < m; i++)       if (dist[ed[i].a] + ed[i].w + dist1[ed[i].b] == min_dist) {         ar2[ed[i].a].push_back(ed[i]);         swap(ed[i].a, ed[i].b);         ar2[ed[i].a].push_back(ed[i]);         swap(ed[i].a, ed[i].b);       }     get_gate(t, -1);     for (int i = 0; i < m; i++)       if (ans[i])         printf( YES n );       else if (dist[ed[i].a] + ed[i].w + dist1[ed[i].b] - min_dist + 1 < ed[i].w)         printf( CAN %I64d n ,                dist[ed[i].a] + ed[i].w + dist1[ed[i].b] - min_dist + 1);       else         printf( NO n );     return 0;   }   void deixtra(long long (&dist)[LEN], vector<edge> (&ar)[LEN], int s) {     int x, to, len;     pair<long long, int> u;     for (int i = 0; i < n; i++) dist[i] = INF;     dist[s] = 0;     q.insert(make_pair(0, s));     while (!q.empty()) {       x = q.begin()->second;       q.erase(q.begin());       for (int i = 0; i < ar[x].size(); i++) {         to = ar[x][i].b;         len = ar[x][i].w;         if (dist[to] > dist[x] + len) {           q.erase(make_pair(dist[to], to));           dist[to] = dist[x] + len;           q.insert(make_pair(dist[to], to));         }       }     }   }   void get_gate(int v, int ind) {     int u;     tin[v] = fup[v] = timer++;     use1[v] = 1;     for (int i = 0; i < ar2[v].size(); i++) {       if (ar2[v][i].ind == ind) continue;       u = ar2[v][i].b;       if (use1[u])         fup[v] = min(fup[v], tin[u]);       else {         get_gate(u, ar2[v][i].ind);         fup[v] = min(fup[v], fup[u]);         if (fup[u] > tin[v]) ans[ar2[v][i].ind] = 1;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9;   const long long INF = 1e18;   const int md = 1e9 + 7;   const int N = 2e5 + 1;   vector<int> g[N];   int p[N], s[N], mx[N], mn[N];   bool did[N];   void make(int v) { p[v] = v, s[v] = 1, mx[v] = v, mn[v] = v; }   int fin(int v) {     if (p[v] == v) return v;     return p[v] = fin(p[v]);   }   void un(int a, int b) {     a = fin(a);     b = fin(b);     if (a == b) return;     if (s[a] < s[b]) swap(a, b);     s[a] += s[b], mx[a] = max(mx[a], mx[b]);     p[b] = a;     mn[a] = min(mn[a], mn[b]);   }   signed main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int n, m;     cin >> n >> m;     for (int f = 0; f < n; ++f) make(f);     for (int j = 0; j < m; ++j) {       int u, v;       cin >> u >> v;       --u, --v;       un(u, v);     }     int curr = 0;     int ans = 0;     int may;     bool isn = 0;     while (curr < n) {       if (mx[fin(curr)] - mn[fin(curr)] + 1 == s[fin(curr)]) {         int lead = fin(curr);         while (curr < n && fin(curr) == lead) {           curr++;         }         isn = 1;       } else {         int lead = fin(curr);         if (isn) may = curr;         isn = 0;         while (may < n && fin(may) == lead) {           may++;         }         un(may, curr);         ans++;       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, nxt[402][26];   string s, t;   int dp[401][401];   bool solve2(string t1, string t2) {     dp[0][0] = -1;     for (int i = 0; i <= t1.size(); ++i) {       for (int j = 0; j <= t2.size(); ++j) {         if (!i && !j) continue;         dp[i][j] = n;         if (i) dp[i][j] = min(dp[i][j], nxt[dp[i - 1][j] + 1][t1[i - 1] -  a ]);         if (j) dp[i][j] = min(dp[i][j], nxt[dp[i][j - 1] + 1][t2[j - 1] -  a ]);       }     }     return dp[t1.size()][t2.size()] < n;   }   void solve() {     cin >> s >> t;     n = s.size();     m = t.size();     for (int i = 0; i < 26; ++i) nxt[n][i] = nxt[n + 1][i] = n;     for (int i = n - 1; ~i; --i) {       memcpy(nxt[i], nxt[i + 1], sizeof(nxt[0]));       nxt[i][s[i] -  a ] = i;     }     for (int i = 0; i < m; ++i)       if (solve2(t.substr(0, i), t.substr(i))) {         cout <<  YES n ;         return;       }     cout <<  NO n ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) solve();   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = 1000000000ll;   const long long inf64 = inf * inf;   const long long base = inf + 7;   const double pi = acos(-1.0);   long long Abs(long long x) { return (x >= 0 ? x : -x); }   int n;   string s;   int par[62 * 62], size[62 * 62], in[62 * 62], out[62 * 62];   vector<int> ans, g[62 * 62];   vector<bool> used;   int _get(char c) {     if (c >=  0  && c <=  9 )       return (c -  0 );     else if (c >=  a  && c <=  z )       return (c -  a  + 10);     else       return (c -  A  + 36);   }   int get(string a) {     int x, y;     x = _get(a[0]);     y = _get(a[1]);     return x * 62 + y;   }   int find(int x) { return (par[x] == x ? x : par[x] = find(par[x])); }   void un(int a, int b) {     int pa = find(a);     int pb = find(b);     if (size[pa] == size[pb])       par[pa] = pb, size[pb]++;     else {       if (size[pa] > size[pb]) swap(pa, pb);       par[pa] = pb;     }   }   char chr(int x) {     if (x < 10)       return char(x +  0 );     else if (x < 36)       return char(x +  a  - 10);     else       return char(x +  A  - 36);   }   void dfs(int v) {     while (!g[v].empty()) {       int x = g[v].back();       g[v].pop_back();       dfs(x);     }     ans.push_back(v);   }   bool solve() {     for (int i(0); i < 62 * 62; i++) size[i] = 1, par[i] = i;     cin >> n;     used.assign(62 * 62, 0);     int Q = -1;     for (int i(0); i < n; i++) {       int x, y;       cin >> s;       x = get(s.substr(0, 2));       if (Q == -1) Q = x;       y = get(s.substr(1, 2));       used[x] = used[y] = 1;       g[x].push_back(y);       in[y]++;       out[x]++;       un(x, y);     }     bool f = 0;     for (int i(0); i < 62 * 62; i++) {       if (!used[i]) continue;       if (par[i] != i) continue;       if (f) {         puts( NO );         return true;         ;       } else         f = 1;     }     int x1, x2;     x1 = x2 = -1;     for (int i(0); i < 62 * 62; i++) {       if (in[i] == out[i]) continue;       if (in[i] - out[i] == 1) {         if (x1 != -1) {           puts( NO );           return true;           ;         }         x1 = i;       } else if (in[i] - out[i] == -1) {         if (x2 != -1) {           puts( NO );           return true;           ;         }         x2 = i;       } else {         puts( NO );         return true;         ;       }     }     if (x1 == -1 && x2 != -1 || x1 != -1 && x2 == -1) {       puts( NO );       return true;       ;     }     used.assign(62 * 62, 0);     dfs(x2 != -1 ? x2 : Q);     reverse((ans).begin(), (ans).end());     puts( YES );     int x, y;     x = ans[0] / 62;     cout << chr(x);     for (int i(0); i < (int)ans.size(); i++) {       x = ans[i] % 62;       cout << chr(x);     }     return true;   }   int main() {     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     char alfabetGainuse[] =          -abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRQSTUVWXYZ0123456789 ;     int T;     cin >> T;     for (int t = 1; t <= T; t++) {       int r, c, k;       cin >> r >> c >> k;       char harta[r + 1][c + 1];       cin.get();       for (int i = 1; i <= r; i++) {         cin.get(harta[i], c + 1);         cin.get();       }       int mancare = 0;       for (int i = 1; i <= r; i++) {         for (int j = 0; j < c; j++) {           if (harta[i][j] ==  R ) mancare++;         }       }       int mancareGainuse[k + 1];       for (int i = 1; i <= k; i++) {         mancareGainuse[i] = mancare / k;       }       for (int i = 1; i <= mancare % k; i++) {         mancareGainuse[i]++;       }       char hartaNoua[r + 1][c + 2];       int indexGaina = 1;       int mancareGasita = 0;       for (int i = 1; i <= r; i++) {         hartaNoua[i][c] =   0 ;         if (i % 2 == 1) {           for (int j = 0; j < c; j++) {             if (indexGaina == k + 1) {               hartaNoua[i][j] = alfabetGainuse[indexGaina - 1];             } else {               hartaNoua[i][j] = alfabetGainuse[indexGaina];               if (harta[i][j] ==  R ) {                 mancareGasita++;                 if (mancareGasita == mancareGainuse[indexGaina]) {                   indexGaina++;                   mancareGasita = 0;                 }               }             }           }         } else {           for (int j = c - 1; j >= 0; j--) {             if (indexGaina == k + 1) {               hartaNoua[i][j] = alfabetGainuse[indexGaina - 1];             } else {               hartaNoua[i][j] = alfabetGainuse[indexGaina];               if (harta[i][j] ==  R ) {                 mancareGasita++;                 if (mancareGasita == mancareGainuse[indexGaina]) {                   indexGaina++;                   mancareGasita = 0;                 }               }             }           }         }       }       for (int i = 1; i <= r; i++) {         cout << hartaNoua[i] <<   n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<long long> temp(3001, 0);   vector<vector<long long> > dp(3001, temp);   void solve() {     long long n;     cin >> n;     vector<long long> v(n);     long long i, j, k, l;     for (i = 0; i < n; i++) cin >> v[i];     for (i = 0; i < 3001; i++) {       for (j = 0; j < 3001; j++) dp[i][j] = 0;     }     for (i = 0; i < n; i++) {       dp[v[i]][i] = dp[v[i]][i] + 1;       if (i == 0) continue;       for (j = 0; j < 3001; j++) dp[j][i] += dp[j][i - 1];     }     long long ans = 0;     for (i = 0; i < n; i++) {       for (j = i + 1; j < n; j++) {         long long a = v[i];         long long b = v[j];         if (a == b) continue;         if (dp[b][i] > 0 && dp[a][j] < dp[a][n - 1]) {           long long val1 = dp[b][i];           long long val2 = dp[a][n - 1] - dp[a][j];           ans = ans + val1 * val2;         }       }     }     for (i = 0; i <= 3000; i++) {       if (dp[i][n - 1] >= 4) {         long long val = 1;         long long count = dp[i][n - 1];         val = val * count;         count--;         val = val * count;         val = val / 2LL;         count--;         val = val * count;         val = val / 3LL;         count--;         val = val * count;         val = val / 4LL;         ans = ans + val;       }     }     cout << ans << endl;   }   int main() {     int t = 1;     cin >> t;     while (t--) solve();     return 0;   }