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 | tourist | 3805 |

2 | maroonrk | 3637 |

3 | Benq | 3513 |

4 | ecnerwala | 3506 |

5 | MiracleFaFa | 3466 |

6 | slime | 3428 |

7 | jiangly | 3415 |

8 | Radewoosh | 3399 |

9 | greenheadstrange | 3393 |

10 | djq_cpp | 3391 |

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

1 | awoo | 192 |

2 | -is-this-fft- | 191 |

3 | Monogon | 185 |

4 | Um_nik | 182 |

4 | YouKn0wWho | 182 |

6 | maroonrk | 170 |

7 | antontrygubO_o | 168 |

8 | errorgorn | 166 |

8 | kostka | 166 |

8 | SecondThread | 166 |

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-2022 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: May/16/2022 18:10:42 (k3).

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.