How can I approach for this problem ? https://codeforces.com/gym/102055/problem/L

Reminder: in case of any technical issues, you can use the lightweight website
m1.codeforces.com,
m2.codeforces.com,
m3.codeforces.com.
×

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

1 | tourist | 3645 |

2 | Radewoosh | 3403 |

3 | Um_nik | 3348 |

4 | LHiC | 3336 |

5 | Benq | 3316 |

6 | V--o_o--V | 3275 |

7 | mnbvmar | 3241 |

8 | yutaka1999 | 3190 |

9 | ainta | 3180 |

10 | Petr | 3106 |

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

1 | Errichto | 191 |

2 | Radewoosh | 177 |

3 | rng_58 | 164 |

4 | PikMike | 161 |

5 | Vovuh | 160 |

6 | majk | 158 |

7 | 300iq | 154 |

7 | antontrygubO_o | 154 |

9 | Um_nik | 151 |

10 | kostka | 149 |

How can I approach for this problem ? https://codeforces.com/gym/102055/problem/L

↑

↓

Codeforces (c) Copyright 2010-2019 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Aug/20/2019 18:01:32 (f3).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

If $$$n \leq 11$$$ then answer is obviously -1. Otherwise, if $$$n$$$ is odd, then write first four primes as $$$2,2,2,3$$$ and show $$$n-9$$$ as sum of two primes. If $$$n$$$ is even, choose first four as $$$2,2,2,2$$$ and write $$$n-8$$$ as sum of two primes. It is always possible* because of Goldbach conjecture.

*Well, at least for such small numbers.

But how will you find two prime numbers such that their sum is n (even) as the range for n is too high.

my solution: calculate all primes $$$< 100$$$ then calculate all $$$5$$$ tuples of those primes and add them to a map (sum of those primes to used primes). Now for each input number just try if $$$n-x$$$ is prime and $$$x$$$ is in the map. If you found a valid $$$x$$$ then $$$n-x$$$ and the $$$5$$$ other primes are the result, if no valid $$$x$$$ is there (propably) is no solution.

As TooNewbie pointed out before, first you can conclude that the answer always exists. In fact, a very similar argument can lead you to conclude that any number greater than 9 can be written as the sum of 5 prime numbers. Now, you aren't required to find some particular combination and that's the key. So, something which is good to know is that the prime gap is usually not that big (it's less than 2000 for very large numbers) and then, you can easily iterate over all the numbers between $$$[n - 2010, n - 10]$$$ and find a suitable prime in this range. Say that $$$p$$$ is this prime, then given that $$$10 \leq n - p \leq 2010$$$, you can also easily find the other 5 primes.

How can you test if such large numbers (10^12) are prime? Looping over all primes < 10^6 seems like it would take too long.

Miller Rabin Primality Test

You can certainly use Miller Rabin or some probabilistic algortihm. Also, you can notice that the number of primes less than n is around $$$\frac{n}{logn}$$$. So, up to $$$10^6$$$, there are ~$$$10^5$$$ primes and therefore, the complexity for this iteration will be $$$10^5*2000 = 2*10^8$$$ (if you only test with the precomputed primes), which is usually fast enough.

Ok, but my implementation is getting TLE :(

link : https://pastebin.com/jnDMVA0Z

I implemented what I explained you before here and it's getting accepted.