Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Try checking tiny values of : . For a fixed , it works if at least one array value has no shared prime factor with .
If some composite works, look at any prime divisor of . What can you say about for the same winning ?
The previous idea means the optimal answer never needs to be composite. If works and is composite, one of its prime factors gives an even smaller valid answer. Composites are just bait.
So now only primes matter. A prime fails exactly when every is divisible by .
A prime divides every exactly when it divides . Therefore the answer is the smallest prime that does not divide this global gcd.
Let’s strip the problem down to what it actually asks.
We need the smallest integer such that for some array element ,
That means and share no prime factor.
Key Observation
The answer is always prime.
Suppose some composite number works. Then there is an index such that
Now take any prime divisor of .
Since , if also divided , then and would share , making the gcd bigger than . But they do not share anything, so:
So also works.
And because is composite, .
So any composite valid answer can be replaced by a smaller prime valid answer. In other words, composites are useless here. They look tempting, but nope, they’re just decorative trash.
So we only need to find the smallest prime that works.
When Does A Prime Fail?
For a prime :
is true exactly when .
So works if there exists some not divisible by .
Equivalently, fails if every array element is divisible by .
That condition is exactly:
Let
Then:
Therefore the answer is simply:
Why Checking Only Small Primes Is Enough
Each , so .
If were divisible by many small primes, it would have to be at least their product. The product
is already huge, and multiplying by exceeds . So the first prime not dividing is definitely very small, at most .
In code, we can just keep a short list of primes like up to and find the first one not dividing . The fallback -1 will never actually be reached for valid constraints, but printing it is harmless and matches the statement.
Algorithm
For each test case:
Complexity
Computing the gcd takes:
where .
Checking the fixed prime list is .
Total over all test cases:
which is easily fine.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
vector<ll> primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67,
71, 73, 79, 83, 89, 97};
while (t--) {
int n;
cin >> n;
ll g = 0;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
g = gcd(g, x);
}
ll ans = -1;
for (ll p : primes) {
if (g % p != 0) {
ans = p;
break;
}
}
cout << ans << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
vector<ll> primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67,
71, 73, 79, 83, 89, 97};
while (t--) {
int n;
cin >> n;
ll g = 0;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
g = gcd(g, x);
}
ll ans = -1;
for (ll p : primes) {
if (g % p != 0) {
ans = p;
break;
}
}
cout << ans << '\n';
}
return 0;
}