Hi, I'm having a hard time understanding the author's solution for this problem . I would appreciate it if anyone could explain his/her accepted solution.
Thank you in advance.
# | 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 |
Hi, I'm having a hard time understanding the author's solution for this problem . I would appreciate it if anyone could explain his/her accepted solution.
Thank you in advance.
Name |
---|
In order for any 2 numbers a,b to have gcd(a,b) = 1, there should not share any prime factors. So given a number x, we want to count the numbers that are not multiples of any of x's prime factors. This can be done by inclusion and exclusion by first adding all then subtracting the count of each of the primes. Since some numbers can be multiples of several primes, inclusion and exclusion is used. The problem is now reduced to be storing the frequency of all prime combinations. For example, if the added number is 60, 60 = 2*2*3*5, so we add 1 to the entries: f[2], f[3], f[5], f[2*3], f[2*5], f[3*5], f[2*3*5]. To count the number of coprimes of 60, the answer would be M (total count of numbers on shelf)-(f[2]+f[3]+f[5])+(f[2*3]+f[2*5]+f[3*5])-(f[2*3*5]). It is guranteed that the number of operations per query is small since the maximum number of distinct primes for any input number is 6 with 2^6 combinations.
Thank you.