given n,k find the number of pair x,y such that gcd(x,y)==k where 1<=x,y<=n and n,k ->1e6.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
given n,k find the number of pair x,y such that gcd(x,y)==k where 1<=x,y<=n and n,k ->1e6.
Name |
---|
If gcd(x, y) = k than x and y are both divisible by k. Also gcd(x / k, y / k) = 1. Let x / k = x1, y / k = y1.
Now our problem is to count number of coprime pairs such that x1 * k <= n && y1 * k <= n. Another way to write it is x1 <= int(n / k) and y1 <= int(n / k). Let n1 = int(n / k).
Using Sieve of Eratothenes we can find every prime divisor of numbers <= n1. Now let's iterate once more from 1 to n1. Let P be the array of prime divisors for every number from 1 to n1. For every number i there are i * ∏((P[i][j] — 1)/P[i][j]) (https://en.wikipedia.org/wiki/Euler%27s_totient_function) coprime numbers in range from 1 to i. So, for number of unordered pairs we should just add up all products for every i. For number of ordered pairs we might change the Euler's function to n1 * ∏((P[i][j] — 1)/P[i][j]) for every i.
Time complexity is O(nlogn) while n1 <= n and there are no more than log(n1) prime divisors for every number.
$$$\sum\limits_{x=1}^{n}\sum\limits_{y=1}^{n}[gcd(x,y)=k]=\sum\limits_{kx=1}^{n}\sum\limits_{ky=1}^{n}[gcd(kx,ky)=k]=\sum\limits_{x=1}^{\lfloor\frac {n}{k}\rfloor}\sum\limits_{y=1}^{\lfloor\frac {n}{k}\rfloor}[gcd(kx,ky)=k]=$$$ $$$ \sum\limits_{x=1}^{\lfloor\frac {n}{k}\rfloor}\sum\limits_{y=1}^{\lfloor\frac {n}{k}\rfloor}[gcd(x,y)=1]=\sum\limits_{x=1}^{\lfloor\frac {n}{k}\rfloor}\sum\limits_{y=1}^{\lfloor\frac {n}{k}\rfloor}\sum\limits_{d|gcd(x,y)}\mu(d)=\sum\limits_{x=1}^{\lfloor\frac {n}{k}\rfloor}\sum\limits_{y=1}^{\lfloor\frac {n}{k}\rfloor}\sum\limits_{d=1}^{\lfloor\frac {n}{k}\rfloor}[d|x][d|y]\mu(d)=\sum\limits_{d=1}^{\lfloor\frac {n}{k}\rfloor}{\lfloor\frac {n}{kd}\rfloor}^2\mu(d)$$$
and we can get mu by:
thanks,love from china