---
id: 1774
lang: en
translation_of: index.md
source_sha256: 9752cedbb2c6627dd0cd54899a08faa80d53595dca9dfb25d57efb66b817034b
title: "[Universal OJ Round #3] Fusion Reaction Strength — Solution"
description: "Pretty easy to come up with: take the gcd of the two numbers, then find its largest factor by scanning the precomputed factors of the first number."
tags: ["OI","Number Theory","gcd","UOJ"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "「Universal OJ Round #3」 kept as a bracketed source tag, [Universal OJ Round #3], following the 「」 → [] convention."
  - "最大因素 is read as a typo for 最大因数 and rendered “largest factor”; 最大的因数 is likewise rendered literally as “largest factor”, without specifying “proper”."
  - "预处理$a_i$的因数 keeps $a_i$ as written, although the code precomputes the factors of $a_1$."
---

## 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$.

<!--more-->

## Code

```cpp
// 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;
}
```
