Problem Name: Ray Gun

Problem Link: http://lightoj.com:81/volume/problem/1144

Any hints for the solution will be really appreciated.

# | User | Rating |
---|---|---|

1 | Benq | 3650 |

2 | Radewoosh | 3648 |

3 | tourist | 3579 |

4 | ecnerwala | 3551 |

5 | Um_nik | 3494 |

6 | Petr | 3452 |

7 | ksun48 | 3432 |

8 | jiangly | 3349 |

9 | maroonrk | 3338 |

10 | Miracle03 | 3314 |

# | User | Contrib. |
---|---|---|

1 | 1-gon | 210 |

2 | awoo | 188 |

3 | Errichto | 186 |

4 | rng_58 | 185 |

5 | SecondThread | 182 |

6 | Ashishgup | 176 |

7 | Um_nik | 175 |

8 | maroonrk | 173 |

9 | antontrygubO_o | 171 |

10 | -is-this-fft- | 168 |

Problem Name: Ray Gun

Problem Link: http://lightoj.com:81/volume/problem/1144

Any hints for the solution will be really appreciated.

↑

↓

Codeforces (c) Copyright 2010-2021 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: May/16/2021 08:56:48 (i1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

There are many observations to make in order to get to a working solution.

i,j), the ray that intersects it is unique and it's identified by the pair , wheregis thegcdofiandj.a≤Nandb≤M. This is the same as counting for everyibetween 1 andN, the amount of numbers in the range [1,M] that are coprime withi.xwith prime factorsp_{1},p_{2}. How do we know how many numbers in range [1,M] are coprime with it? That's equal to M minus the amount of multiples ofp_{1}minus the amount of multiples ofp_{2}plus the amount of multiples ofp_{1}*p_{2}. This is inclusion-exclusion, and in general, if the amount of elements is even, we add, otherwise, we subtract.slow) solution: Iterate over everyiin the range [1,N] and for everyi, factorize it, try out all combinations of primes and then, for every combination that results in a numberk, add if the amount of primes is even or subtract if the amount of primes is odd.k. A crucial observation is that the higher exponent of that numberkwill be 1, because we're trying combinations of different primes. Another crucial observation is that this numberkwill be seen times in total. Finally, each time we see it, it will contribute by to the final answer (or if the amount of primes is odd).O(N). We should precalculate the amount of prime factors of every number in the range [1, 10^{6}] (this can be done with a simple sieve), and we should cross out numbers that have some prime with an exponent higher than 1 (in other words, multiples of some square). Once we have precalculated all that, we simply iterate from 1 toNand for every numberxthat we didn't cross out, we add (or subtract) to our answer.N= 0, the answer is 1, exceptM= 0 too, in which case the answer is 0.Here's the simple C++ code: Ray Gun

Thanks, really appreciate your help.