Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- #define ull unsigned ll
- #define ld long double
- #define be(a) a.begin(), a.end()
- //#define l(x, n) for (int x = 0; x < n; ++x)
- //#define f first
- //#define s second
- //#pragma GCC optimize("Ofast")
- using namespace std;
- void f(int x, int gcd1, vector <int> &a, vector <int> &q, map <int, int> &p, int &ans) {
- if (x == q.size()) {
- if (gcd1 <= ans)
- return;
- //cout << gcd1 << endl << endl;
- int gcd2 = 2e9;
- for (int i = 0; i < a.size() && gcd2 >= ans; ++i)
- if (a[i]%gcd1)
- gcd2 = gcd2 == 2e9? a[i] : __gcd(gcd2, a[i]);
- ans = max(ans, min(gcd1, gcd2));
- }
- else {
- int m = q[x], n = p[m], prod = 1;
- for (int i = 0; i <= n; ++i, prod *= m)
- f(x + 1, gcd1*prod, a, q, p, ans);
- }
- }
- int main() {
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- int n, m = 2e9, ans = 1;
- cin >> n;
- vector <int> a(n);
- for (int &i : a)
- cin >> i,
- m = min(m, i);
- int mm = m;
- map <int, int> p;
- vector <int> q;
- for (int i = 2; i*i <= m; ++i) {
- if (m%i == 0)
- q.push_back(i);
- while (m%i == 0)
- ++p[i],
- m /= i;
- }
- if (m != 1)
- ++p[m],
- q.push_back(m);
- f(0, 1, a, q, p, ans);
- cout << (ans == 2e9 ? mm : ans) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment