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 | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
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.