Apkawa

Untitled

Sep 2nd, 2021
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define ull unsigned ll
  4. #define ld long double
  5. #define be(a) a.begin(), a.end()
  6. //#define l(x, n) for (int x = 0; x < n; ++x)
  7. //#define f first
  8. //#define s second
  9. //#pragma GCC optimize("Ofast")
  10. using namespace std;
  11.  
  12. void f(int x, int gcd1, vector <int> &a, vector <int> &q, map <int, int> &p, int &ans) {
  13.     if (x == q.size()) {
  14.         if (gcd1 <= ans)
  15.             return;
  16.         //cout << gcd1 << endl << endl;
  17.         int gcd2 = 2e9;
  18.         for (int i = 0; i < a.size() && gcd2 >= ans; ++i)
  19.             if (a[i]%gcd1)
  20.                 gcd2 = gcd2 == 2e9? a[i] : __gcd(gcd2, a[i]);
  21.         ans = max(ans, min(gcd1, gcd2));
  22.     }
  23.     else {
  24.         int m = q[x], n = p[m], prod = 1;
  25.         for (int i = 0; i <= n; ++i, prod *= m)
  26.             f(x + 1, gcd1*prod, a, q, p, ans);
  27.     }
  28. }
  29.  
  30. int main() {
  31.     //freopen("input.txt", "r", stdin);
  32.     //freopen("output.txt", "w", stdout);
  33.     ios_base::sync_with_stdio(0);
  34.     cin.tie(0);
  35.  
  36.     int t;
  37.     cin >> t;
  38.     while (t--) {
  39.         int n, m = 2e9, ans = 1;
  40.         cin >> n;
  41.         vector <int> a(n);
  42.         for (int &i : a)
  43.             cin >> i,
  44.             m = min(m, i);
  45.         int mm = m;
  46.         map <int, int> p;
  47.         vector <int> q;
  48.         for (int i = 2; i*i <= m; ++i) {
  49.             if (m%i == 0)
  50.                 q.push_back(i);
  51.             while (m%i == 0)
  52.                 ++p[i],
  53.                 m /= i;
  54.         }
  55.         if (m != 1)
  56.             ++p[m],
  57.             q.push_back(m);
  58.         f(0, 1, a, q, p, ans);
  59.         cout << (ans == 2e9 ? mm : ans) << endl;
  60.     }
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment