Every positive integer can be uniquely represented in the form i = k \cdot x^2, where is square-free, that is, not divisible by the square of any prime. The number is called the square-free part of . We denote it by . For example:
6 = 2 \cdot 3, so sf[6] = 6;
12 = 2^2 \cdot 3, so sf[12] = 3;
Theorem. The product a \cdot b is a perfect square if and only if sf[a] = sf[b].
Proof. Let
where k_1 and k_2 are square-free. Then
This expression is a perfect square if and only if k_1k_2 is a perfect square. But k_1 and k_2 are square-free. Therefore, k_1k_2 is a perfect square only if k_1 = k_2.
Reduction to counting groups. It follows from the theorem that the numbers from to are partitioned into groups according to the value of , and a pair is suitable (a \cdot b is a perfect square) if and only if and belong to the same group.
Let be the number of integers i~(1 \le i \le n) for which sf[i] = k. To compute it, it suffices to perform the operation cnt[sf[i]]++ for each .
If a group contains numbers, it contributes
pairs. The answer is the sum over all groups:
Smallest prime factor. To compute quickly, we introduce an array equal to the smallest prime factor of . For example:
The array is computed with a modified sieve of Eratosthenes: when processing a prime , the value spf[j] = p is assigned to every multiple of for which it has not yet been set.
Having the array, we can compute the square-free part of each number. Let be the smallest prime divisor of (spf[i] = p). Consider two cases:
occurs at least twice in the prime factorization of . This means that is divisible by p^2. Two identical prime factors form a perfect square, so they can be removed from the square-free part:
For example:
sf[12] = sf[3], because spf[12] = 2 and is divisible by 2^2;
= , because spf[45] = 3 and is divisible by 3^2;
occurs exactly once in the prime factorization of . Therefore, the factor must remain in the square-free part:
For example:
sf[30] = sf[15] \cdot 2, because spf[30] = 2 and is not divisible by 2^2;
sf[18] = sf[9] \cdot 2, because spf[18] = 2 and is not divisible by 2^2;
Example
Let us compute the answer for n = 10.
i | prime factorization | square-free part |
1 * 2^2 | ||
2 * 3 | ||
2 * 2^2 | ||
1 * 3^2 | ||
2 * 5 |
Group the numbers in the array by .
cnt[1] = 3: (1, 4, 9) | cnt[6] = 1: (6) |
cnt[2] = 2: (2, 8) | cnt[7] = 1: (7) |
cnt[3] = 1: (3) | cnt[8] = 0: () |
cnt[4] = 0: () | cnt[9] = 0: () |
cnt[5] = 1: (5) | cnt[10] = 1: (10) |
The answer is equal to
Algorithm implementation
Define constants and arrays.
#define MAXN 1000000
#define MOD 1000000007
int spf[MAXN + 1]; // smallest prime factor of a number
int sf[MAXN + 1]; // square-free part of a number
int cnt[MAXN + 1]; // number of integers with a given sfRead the input value .
scanf("%d", &n);Using the Sieve of Eratosthenes, fill the array :
should store the smallest prime divisor of the number ;
Initially, assume that each number is divisible by itself, but this is temporary.
for (i = 1; i <= n; i++) spf[i] = i;Iterate the variable over numbers from to \sqrt{n}.
for (i = 2; i * i <= n; i++)
{
if (spf[i] == i)
{If the number is prime, iterate over values i^2, i^2 + i, i^2 + 2i, ... . All these numbers have as a prime divisor (since spf[i] = i). If spf[j] = j, update the smallest prime divisor of .
for (j = i * i; j <= n; j += i)
if (spf[j] == j) spf[j] = i;
}
}For each number , compute its square-free part and store the result in .
sf[1] = 1;
for (i = 2; i <= n; i++)
{The variable contains the smallest prime divisor of the number .
p = spf[i];
x = i / p;If is divisible by p^2, then sf[i] = sf[i / p^2].
if (x % p == 0)
sf[i] = sf[x / p];If is not divisible by p^2 (but is divisible by ), then sf[i] = sf[i / p] \cdot p.
else
sf[i] = sf[x] * p;
}Group numbers by their square-free part.
for (i = 1; i <= n; i++)
cnt[sf[i]]++;Compute the answer — the required number of pairs.
res = 0;
for (i = 1; i <= n; i++)
if (cnt[i] >= 2)
res = (res + cnt[i] * (cnt[i] - 1) / 2) % MOD;Print the answer.
printf("%lld\n", res);