malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007ll;   const double EPS = 1e-11;   int n;   char s[1252525];   int cntright[1252525];   int cntleft[1252525];   int revright[1252525];   int revleft[1252525];   long long sumright[1252525];   long long sumleft[1252525];   int main() {     scanf( %d , &n);     scanf( %s , s);     for (int i = 0; i < (int)(n); ++i) {       if (s[i] ==  U ) {         cntright[i + 1] = cntright[i] + 1;         cntleft[i + 1] = cntleft[i];         revright[cntright[i + 1]] = i + 1;       } else {         cntright[i + 1] = cntright[i];         cntleft[i + 1] = cntleft[i] + 1;         revleft[cntleft[i + 1]] = i + 1;       }     }     for (int i = 0; i < (int)(n); ++i) {       if (s[i] ==  U ) {         sumright[i + 1] = sumright[i] + (n - i);         sumleft[i + 1] = sumleft[i];       } else {         sumright[i + 1] = sumright[i];         sumleft[i + 1] = sumleft[i] + (i + 1);       }     }     for (int q = 0; q < (int)(n); ++q) {       int l = cntright[q];       int r = cntleft[n] - cntleft[q + 1];       if (s[q] ==  U ) {         if (r > l) {           int rightbnd = l + 1;           int leftbnd = l;           int rrr = revleft[cntleft[q + 1] + rightbnd];           int lll = 0;           long long ans = 0ll;           ans +=               (sumright[q] - sumright[lll] - (long long)leftbnd * (n - q)) * 2ll;           ans += (sumleft[rrr] - sumleft[q + 1] - (long long)rightbnd * (q + 1)) *                  2ll;           ans += q + 1;           printf( %I64d , ans);         } else {           int rightbnd = r;           int leftbnd = r;           int rrr = n;           int lll = revright[cntright[q] - leftbnd];           long long ans = 0ll;           ans +=               (sumright[q] - sumright[lll] - (long long)leftbnd * (n - q)) * 2ll;           ans += (sumleft[rrr] - sumleft[q + 1] - (long long)rightbnd * (q + 1)) *                  2ll;           ans += n - q;           printf( %I64d , ans);         }       } else {         if (l > r) {           int rightbnd = r;           int leftbnd = r + 1;           int rrr = n;           int lll = revright[cntright[q] - leftbnd];           long long ans = 0ll;           ans +=               (sumright[q] - sumright[lll] - (long long)leftbnd * (n - q)) * 2ll;           ans += (sumleft[rrr] - sumleft[q + 1] - (long long)rightbnd * (q + 1)) *                  2ll;           ans += n - q;           printf( %I64d , ans);         } else {           int rightbnd = l;           int leftbnd = l;           int rrr = revleft[cntleft[q + 1] + rightbnd];           int lll = 0;           long long ans = 0ll;           ans +=               (sumright[q] - sumright[lll] - (long long)leftbnd * (n - q)) * 2ll;           ans += (sumleft[rrr] - sumleft[q + 1] - (long long)rightbnd * (q + 1)) *                  2ll;           ans += q + 1;           printf( %I64d , ans);         }       }       if (q != n - 1) {         putchar(   );       } else {         putchar(  n );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long n, k, m;     cin >> n >> k >> m;     vector<long long> a(k);     for (long long& v : a) cin >> v;     sort(a.begin(), a.end());     a.push_back(0);     k++;     a.insert(a.begin(), 0);     for (int i = 1; i <= k; i++) a[i] += a[i - 1];     vector<long long> dp((k + 1) * n, 1e18);     dp[0] = 0;     for (int i = 0; i < n; i++) {       auto ndp = dp;       for (int j = 0; j <= k; j++) {         for (int l = dp.size() - 1 - j; l >= 0; l--)           ndp[l + j] = min(ndp[l + j], dp[l] + a[j]);       }       dp = ndp;     }     cout << upper_bound(dp.begin(), dp.end(), m) - dp.begin() - 1 << endl;   }
#include <bits/stdc++.h>   using namespace std;   long long int M = 1e9 + 7;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t;     cin >> t;     vector<long long int> v;     for (long long int i = 1; i <= 100000; ++i) {       long long int x = (i * (3 * i + 1)) / 2;       if (x <= 2e9) {         v.push_back(x);       }     }     while (t--) {       long long int n;       cin >> n;       long long int ans = 0;       while (n >= 2) {         long long int x = lower_bound(v.begin(), v.end(), n) - v.begin();         if (v[x] != n) x--;         n -= v[x];         ans++;       }       cout << ans <<   n ;     }   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   int n;   vector<long long> a(200013);   bool f(long long m) {     long long l = (long long)(a[0] + 2 * m), r = (long long)(a[n - 1] - 2 * m);     if (l >= r) {       return 1;     }     if ((r - l) <= 2 * m) {       return 1;     }     long long ll =         a[int(upper_bound(a.begin(), a.begin() + n, l) - a.begin())] + 2 * m;     long long rr = a[int(upper_bound(a.begin(), a.begin() + n, ll) - a.begin())];     if (rr == 0) {       return 1;     }     return (rr >= r);   }   int main() {     cin >> n;     for (long long i = 0; i < n; i++) {       cin >> a[i];       a[i] *= 2;     }     sort(a.begin(), a.begin() + n);     long long l = 0, r = 2000000000;     while (l + 1 < r) {       long long m = (l + r) / 2;       if (!f(m)) {         l = m;       } else {         r = m;       }     }     if (!f(l)) l++;     cout.setf(ios::fixed);     cout.precision(6);     cout << (double)l / 2.0 << endl          << (double)(a[0] + l) / 2.0 <<     << (double)(a[n - 1] - l) / 2.0          <<    ;     long long ll = (long long)(a[0] + 2 * l);     long long lll = a[int(upper_bound(a.begin(), a.begin() + n, ll) - a.begin())];     cout << (double)(lll + l) / 2.0;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000009;   long long modexp_recursion(long long a, long long b, long long n) {     long long t = 1;     if (b == 0) return 1;     if (b == 1) return a % n;     t = modexp_recursion(a, b >> 1, n);     t = t * t % n;     if (b & 0x1) {       t = t * a % n;     }     return t;   }   int main() {     long long n, m, k;     cin >> n >> m >> k;     long long a = m / (k - 1);     if (a <= n - m)       cout << m << endl;     else {       long long ret = 0;       a = n - (n - m) * k;       long long b = a / k;       if (b > 0) {         ret = 2 * k * modexp_recursion(2, b, MOD) - 2 * k;         ret = ret % MOD;       }       ret = (ret + (n - m) * (k - 1)) % MOD;       ret = (ret + (a - a / k * k)) % MOD;       cout << ret << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int read(int f = 1, int x = 0, char ch =    ) {     while (!isdigit(ch = getchar()))       if (ch ==  - ) f = -1;     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar();     return f * x;   }   const int N = 5e5 + 5;   int n, m, f[N], g[N], fs[N], gs[N], ft[N], gt[N], cls[N];   vector<pair<int, long long> > s[N];   int find(int *p, int x) { return p[x] == x ? x : find(p, p[x]); }   void merge(int *p, int *s, int *t, int x, int y, int z) {     x = find(p, x), y = find(p, y);     if (s[x] > s[y]) swap(x, y);     p[x] = y, s[y] += s[x], t[x] = z;   }   long long ask(vector<pair<int, long long> > &s, int t) {     return s.back().second -            (--lower_bound(s.begin(), s.end(), pair<int, long long>(t, 0)))                ->second;   }   int main() {     n = read(), m = read();     for (int i = 1; i <= n; ++i)       f[i] = g[i] = i, fs[i] = gs[i] = 1, s[i].emplace_back(-1, 0);     for (int i = 1; i <= m; ++i) {       char op[2];       int x, y;       scanf( %s , op);       if (op[0] ==  U )         x = read(), y = read(), merge(f, fs, ft, x, y, i);       else if (op[0] ==  M )         x = read(), y = read(), merge(g, gs, gt, x, y, i);       else if (op[0] ==  A )         x = read(), x = find(f, x),         s[x].emplace_back(i, s[x].back().second + fs[x]);       else if (op[0] ==  Z )         x = read(), x = find(g, x), cls[x] = i;       else {         int x = read(), y, t = cls[x];         for (y = x; g[y] != y; y = g[y])           if (cls[g[y]] > gt[y]) t = max(t, cls[g[y]]);         long long ans = ask(s[x], t);         for (y = x; f[y] != y; y = f[y]) ans += ask(s[f[y]], max(t, ft[y]));         printf( %lld n , ans);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = (int)1e9 + 7;   const int MAXN = (int)1003;   const int LOG = (int)11;   const int infint = (int)1e9 + 3;   const long long inf = (long long)1e12;   string A, B[MAXN];   int cost[MAXN], dp[MAXN][MAXN], last[MAXN], n, mx, sza;   pair<int, int> radix[MAXN];   void build() {     memset(dp, -63, sizeof dp);     for (auto &ch : A) ch -=  0 ;     mx++;     reverse(A.begin(), A.end());     sza = A.size();     while ((int)A.size() < mx) A += (char)(0);     for (int i = 0; i < n; i++) {       last[i] = B[i].size();       reverse(B[i].begin(), B[i].end());       while ((int)B[i].size() < mx) B[i] +=  0 ;       radix[i] = {B[i][0], i};     }     sort(radix, radix + n);     reverse(radix, radix + n);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> A >> n;     mx = A.size();     for (int i = 0; i < n; i++) {       cin >> B[i];       mx = max(mx, (int)B[i].size());     }     for (int i = 0; i < 10; i++) cin >> cost[i];     build();     dp[0][0] = 0;     for (int i = 0; i < mx; i++) {       int l, r;       if (A[i] ==  ?  -  0 )         l = 0, r = 9;       else         l = r = A[i];       if (i == sza - 1 && A[i] ==  ?  -  0 ) l = 1;       for (int k = l; k <= r; k++) {         int pt = 0, add = 0;         for (int j = 0; j < n; j++) {           int ind = radix[j].second;           int nxt = B[ind][i] -  0  + k;           if (nxt >= 10) pt++;           if (max(last[ind], sza) > i || nxt > 0) add += cost[nxt % 10];         }         for (int j = 0; j <= n; j++) {           dp[i + 1][pt] = max(dp[i + 1][pt], dp[i][j] + add);           if (j == n) continue;           int ind = radix[j].second;           int nxt = B[ind][i] -  0  + k;           if (nxt == 9) pt++;           if (max(last[ind], sza) > i || nxt > 0) add -= cost[nxt % 10];           add += cost[(nxt + 1) % 10];         }       }       reverse(radix, radix + n);       for (int j = 0; j < n; j++) {         int ind = radix[j].second;         radix[j].first = n * B[ind][i] + j;       }       sort(radix, radix + n);       reverse(radix, radix + n);     }     cout << dp[mx][0];   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     int a, b, c, i, j, cnt = 0;     cin >> a >> b >> c;     for (i = 0; i <= c; i++) {       for (j = c; j >= 0; j--) {         if ((j * a + i * b) == c) {           cout <<  Yes  << endl;           cnt = 1;           break;         }       }       if (cnt == 1) break;     }     if (cnt == 0) cout <<  No  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = (long long)(1e6) + 322;   const long long MAXN = (long long)(1e7) + 100;   const long long inf = (long long)(3e18) + 322;   const long long mod = (long long)(1e9) + 7;   const double eps = 1e-6;   inline void upd(long long &mn, long long x, long long y, long long z) {     mn = min(mn, (x - y) * (x - y) + (y - z) * (y - z) + (z - x) * (z - x));   }   void solve() {     long long mn = inf;     long long n1, n2, n3;     vector<long long> v[4];     cin >> n1 >> n2 >> n3;     for (long long i = 1; i <= n1; ++i) {       long long x;       cin >> x;       v[0].push_back(x);     }     sort((v[0]).begin(), (v[0]).end());     for (long long i = 1; i <= n2; ++i) {       long long x;       cin >> x;       v[1].push_back(x);     }     sort((v[1]).begin(), (v[1]).end());     for (long long i = 1; i <= n3; ++i) {       long long x;       cin >> x;       v[2].push_back(x);     }     sort((v[2]).begin(), (v[2]).end());     vector<long long> ids;     for (long long i = 0; i < 3; ++i) {       ids.push_back(i);     }     do {       for (long long i = 0; i < (long long)(v[ids[0]].size()); ++i) {         long long x = v[ids[0]][i];         auto z = lower_bound((v[ids[2]]).begin(), (v[ids[2]]).end(), x);         if (z == v[ids[2]].end()) continue;         auto y =             lower_bound((v[ids[1]]).begin(), (v[ids[1]]).end(), (x + *z) / 2);         if (y != v[ids[1]].end()) upd(mn, x, *y, *z);         if (y != v[ids[1]].begin()) {           auto y2 =               --lower_bound((v[ids[1]]).begin(), (v[ids[1]]).end(), (x + *z) / 2);           upd(mn, x, *y2, *z);         }       }     } while (next_permutation((ids).begin(), (ids).end()));     cout << mn <<   n ;   }   signed main() {     ios_base ::sync_with_stdio(false);     cin.tie(0);     long long test;     cin >> test;     for (long long tt = 1; tt <= test; ++tt) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   int chkmax(T &a, T b) {     if (b > a) {       a = b;       return 1;     }     return 0;   }   template <class T>   int chkmin(T &a, T b) {     if (b < a) {       a = b;       return 1;     }     return 0;   }   template <class iterator>   void output(iterator begin, iterator end, ostream &out = cerr) {     while (begin != end) {       out << (*begin) <<    ;       begin++;     }     out << endl;   }   template <class T>   void output(T x, ostream &out = cerr) {     output(x.begin(), x.end(), out);   }   void fast_io() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   const int MOD = 998244353;   int mul(int a, int b) { return (1LL * a * b) % MOD; }   int power(int a, int deg) {     int res = 1;     for (; deg; a = mul(a, a), deg >>= 1) {       if (deg & 1) {         res = mul(res, a);       }     }     return res;   }   int inverse(int x) { return power(x, MOD - 2); }   void add(int &a, int b) {     a += b;     if (a >= MOD) {       a -= MOD;     }   }   const int mx = 2e5 + 228;   int n, fact[mx];   long long k;   void calc_fact() {     fact[0] = 1;     for (int i = 1; i < mx; ++i) {       fact[i] = mul(fact[i - 1], i);     }   }   int C(int n, int k) {     return (k > n ? 0 : mul(fact[n], inverse(mul(fact[k], fact[n - k]))));   }   signed main() {     fast_io();     calc_fact();     cin >> n >> k;     if (k >= n) {       cout << 0 << endl;       exit(0);     }     int x = n - k;     int ans = 0;     for (int i = 0; i <= x; ++i) {       int sign = (i % 2 ? MOD - 1 : 1);       add(ans, mul(sign, mul(C(x, i), power(x - i, n))));     }     ans = mul(ans, C(n, x));     if (k) {       ans = mul(ans, 2);     }     cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 5;   const long long mod = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     string str;     cin >> str;     long long n = str.length();     vector<pair<long long, long long> > arr;     arr.push_back(make_pair(1, str[0] -  a ));     for (long long i = 1; i < n; i++) {       if (str[i] == str[i - 1])         arr[arr.size() - 1].first++;       else         arr.push_back(make_pair(1, str[i] -  a ));     }     long long ans = 0;     while (arr.size() > 1) {       ans++;       vector<pair<long long, long long> > tmp;       for (long long i = 0; i < arr.size(); i++) {         if (i == 0 || i == arr.size() - 1)           arr[i].first -= 1;         else           arr[i].first -= 2;         if (tmp.size() == 0 || arr[i].second != tmp[tmp.size() - 1].second) {           if (arr[i].first > 0) tmp.push_back(arr[i]);         } else if (arr[i].first > 0)           tmp[tmp.size() - 1].first += arr[i].first;       }       arr = tmp;     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   class Graph {    public:     vector<vector<int> > adjList;     vector<int> indegree;     Graph(int v) {       adjList.resize(v);       indegree.resize(v, 0);     }     void add(int u, int v) {       adjList[u].push_back(v);       indegree[v]++;     }   };   struct subset {     int rank;     int parent;   };   int find(subset subsets[], int i) {     if (subsets[i].parent != i)       subsets[i].parent = find(subsets, subsets[i].parent);     return subsets[i].parent;   }   void Union(subset subsets[], int x, int y) {     int xroot = find(subsets, x);     int yroot = find(subsets, y);     if (subsets[xroot].rank > subsets[yroot].rank) {       subsets[yroot].parent = xroot;     } else if (subsets[xroot].rank < subsets[yroot].rank) {       subsets[xroot].parent = yroot;     } else {       subsets[yroot].parent = xroot;       subsets[xroot].rank++;     }   }   bool TopologicalSort(Graph const &graph, vector<int> &ans, int v,                        subset subsets[]) {     vector<int> indegree = graph.indegree;     queue<int> S;     for (int i = 0; i < v; i++) {       if (!indegree[i]) {         S.push(i);         ans[find(subsets, i)] = 1;       }     }     while (!S.empty()) {       int n = S.front();       S.pop();       for (int m : graph.adjList[n]) {         indegree[m] -= 1;         ans[find(subsets, m)] = ans[find(subsets, n)] + 1;         if (!indegree[m]) {           S.push(m);         }       }     }     for (int i = 0; i < v; i++) {       if (indegree[i]) {         return false;       }     }     return true;   }   int main() {     int n, m;     cin >> n >> m;     subset *subsets = new subset[n + m];     for (int v = 0; v < n + m; v++) {       subsets[v].parent = v;       subsets[v].rank = 0;     }     Graph graph(n + m);     char input[n][m];     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> input[i][j];         if (input[i][j] ==  = ) {           Union(subsets, i, j + n);         }       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         int x = find(subsets, i);         int y = find(subsets, j + n);         if (input[i][j] ==  < ) {           graph.add(x, y);         } else if (input[i][j] ==  > ) {           graph.add(y, x);         }       }     }     vector<int> ans(n + m);     if (TopologicalSort(graph, ans, n + m, subsets)) {       cout <<  YES  << endl;       for (int i = 0; i < n; i++) {         cout << ans[find(subsets, i)] <<    ;       }       cout << endl;       for (int i = n; i < m + n; i++) {         cout << ans[find(subsets, i)] <<    ;       }       cout << endl;     } else {       cout <<  NO  << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     char trash[100];     int n;     cin >> n;     cin.getline(trash, 10);     for (int i = 0; i < n; ++i) {       string s;       getline(cin, s);       bool fredas = (s.find( lala. , s.length() - 5) == s.length() - 5);       bool rainbows = (s.find( miao. ) == 0);       if (fredas ^ rainbows) {         cout << (fredas ?  Freda s  :  Rainbow s );       } else {         cout <<  OMG>.< I don t know! ;       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int SET(int N, int pos) { return N = N | (1 << pos); }   int RESET(int N, int pos) { return N = N & ~(1 << pos); }   bool CHECK(int N, int pos) { return (bool)(N & (1 << pos)); }   template <typename T>   inline T __lcm(T a, T b) {     return (a * b) / __gcd(a, b);   }   struct node {     long long x, y, cnt;   };   int dx[] = {0, 1, 0, -1, -1, 1, -1, 1};   int dy[] = {1, 0, -1, 0, 1, 1, -1, -1};   long long a[1000000];   long long b[1000000];   long long c[1000000];   void solve() {     long long n;     cin >> n;     if (n % 2 == 0) {       cout << -1 << endl;       return;     }     for (long long i = 0; i < n; i++) {       c[i] = n - i - 1;     }     for (long long i = 0; i < n; i++) {       a[i] = i;     }     for (long long i = 0; i < n; i++) {       b[i] = c[i] - a[i];       if (b[i] < 0) {         b[i] += n;       }     }     for (long long i = 0; i < n; i++) {       cout << a[i] <<    ;     }     cout << endl;     for (long long i = 0; i < n; i++) {       cout << b[i] <<    ;     }     cout << endl;     for (long long i = 0; i < n; i++) {       cout << c[i] <<    ;     }     cout << endl;   }   int main() {     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double long EPS = 1e-7;   vector<vector<long long>> precost(long long n, vector<long long> &p,                                     vector<long long> &id) {     vector<vector<long long>> res(n, vector<long long>(n));     for (int i = 0; i < n; i++) {       vector<long long> deg(p.size());       for (int j = 1; j < p.size(); j++) {         deg[p[j]]++;       }       long long val = 0;       for (int j = i; j < n; j++) {         long long cur = id[j];         while (cur != 0 && deg[cur] == 0) {           val++;           deg[p[cur]]--;           cur = p[cur];         }         res[i][j] = val;       }     }     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     vector<vector<long long>> adj(2);     vector<vector<long long>> id(2, vector<long long>(n));     for (int i = 0; i < 2; i++) {       long long a;       cin >> a;       adj[i].resize(a);       for (int j = 1; j <= a - 1; j++) {         cin >> adj[i][j];         adj[i][j]--;       }       for (int j = 0; j < n; j++) {         cin >> id[i][j];         id[i][j]--;       }     }     vector<vector<vector<long long>>> cost(2);     for (int i = 0; i < 2; i++) {       cost[i] = precost(n, adj[i], id[i]);     }     vector<long long> dp(n + 1);     for (int i = 0; i < n; i++) {       for (int j = i + 1; j <= n; j++) {         dp[j] = max(dp[j], dp[i] + max(cost[0][i][j - 1], cost[1][i][j - 1]));       }     }     cout << dp[n];     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   long long mpow(long long x, long long y) {     long long res = 1;     x = x % 1000000007;     while (y) {       if (y & 1) {         res *= x;         res = res % 1000000007;       }       x *= x;       x %= 1000000007;       y >>= 1;     }     return res;   }   bool isprime(long long n) {     for (long long i = 2; i * i <= n; i++) {       if (n % i == 0) return false;     }     return true;   }   long long gcd(long long a, long long b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   void solves_1() {     long long n;     cin >> n;     vector<long long> v(n);     for (int i = 0; i < n; i++) cin >> v[i];     sort(v.rbegin(), v.rend());     set<long long> st;     long long tmp = 0;     for (int i = 0; i < n; i++) {       if (st.find(v[i]) != st.end()) continue;       st.insert(v[i]);       while (v[i] % 2 == 0) {         v[i] /= 2;         tmp++;         st.insert(v[i]);       }     }     cout << tmp << endl;     return;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     ;     long long t;     cin >> t;     while (t--) solves_1();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline long long mod(long long n, long long m) {     long long ret = n % m;     if (ret < 0) ret += m;     return ret;   }   long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }   long long exp(long long a, long long b, long long m) {     if (b == 0) return 1;     if (b == 1) return mod(a, m);     long long k = mod(exp(a, b / 2, m), m);     if (b & 1) {       return mod(a * mod(k * k, m), m);     } else       return mod(k * k, m);   }   map<vector<long long>, long long> mp;   long long a[500100];   vector<long long> adj[500100];   void solve() {     mp.clear();     long long n, m;     cin >> n >> m;     for (long long i = 1; i <= n; i++) {       adj[i].clear();       cin >> a[i];     }     for (long long i = 0; i < m; i++) {       long long a, b;       cin >> a >> b;       adj[b].push_back(a);     }     for (long long i = 1; i <= n; i++) {       sort(adj[i].begin(), adj[i].end());       if (adj[i].size()) {         mp[adj[i]] += a[i];       }     }     long long g = -1;     for (map<vector<long long>, long long>::iterator it = mp.begin();          it != mp.end(); it++) {       if (g == -1)         g = it->second;       else         g = gcd(g, it->second);     }     if (g == -1) g = 0;     cout << g <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long t;     cin >> t;     while (t--) {       solve();     }   }