Problem Author: | Al Koasar Hussain (Al_Kaosar) |
Prepared by: | Mohammad Nasser (MOAGEZA) |
Edited by: | Hasan Saleh (MathModel) |
Solution
General Idea: We need to find if there exists a positive integer such that \gcd(a_i, a_j) = g for all pairs in the arithmetic sequence.
First, check if is divisible by . If not, it's impossible.
If a = g, then the answer is always "YES" because we can choose any that is a multiple of .
Otherwise, let x = a / g. The key observation is:
If has a prime factor and m > p, then we cannot avoid creating pairs with \gcd > g.
If m \le p where is the smallest prime factor of , there exists a valid (specifically one where \gcd(a, d) = g and is a multiple of all primes less than ) such that the condition holds.
Therefore, we find the smallest prime factor of and check if is at most this value.
The solution runs in \mathcal{O}(\sqrt{x}) time per test case.
#include <bits/stdc++.h>
using namespace std;
int smallest_prime_factor(int x) {
for (long long i = 2; i * i <= x; i++) {
if (x % i == 0) return i;
}
return x;
}
void solve(){
int a, g, m;
cin >> a >> g >> m;
if(m == 1){
cout << "YES";
return;
}
if (a % g != 0) {
cout << "NO";
return;
}
int x = a / g;
if (x == 1) {
cout << "YES";
return;
}
int p = smallest_prime_factor(x);
if (m <= p) cout << "YES";
else cout << "NO";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
cout << '\n';
}
return 0;
}