[Universal OJ Round #3] Fusion Reaction Strength — Solution
Main Idea
Pretty easy to come up with. Consider brute force: after computing the \(\gcd\) of the two, enumerate the largest factor, and that is the answer. So how do we speed it up? We notice that the largest factor of \(gcd\) must also be one of the factors of \(a_1\), so we just precompute the factors of \(a_i\), and then when answering queries we directly enumerate in \(\log n\).
Code
// UOJ#3A.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
const int MAX_N = 1e6 + 200;
ll n, ai[MAX_N], primes[MAX_N];
int main()
{
scanf("%lld", &n);
for (int i = 1; i <= n; i++)
scanf("%lld", &ai[i]);
for (int i = 1; 1LL * i * i <= ai[1]; i++)
if (ai[1] % i == 0)
{
primes[++primes[0]] = i;
if (ai[1] / i != i)
primes[++primes[0]] = ai[1] / i;
}
sort(primes + 1, primes + 1 + primes[0]);
for (int i = 1; i <= n; i++)
{
bool flag = true;
ll di = gcd(ai[1], ai[i]);
for (int j = 1; j <= primes[0]; j++)
if (di % primes[j] == 0 && di / primes[j] < di)
{
printf("%lld ", di / primes[j]);
flag = false;
break;
}
if (flag)
printf("-1 ");
}
return 0;
}