Editorial for TLE '16 Contest 2 P3 - Fax's Thanksgiving Dinner
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
First, observe that since each person only wants some sum of pieces that total to , there must be some way for Fax to cut the turkey into equal pieces and give some number of pieces to each person. Thus, the largest piece should be , where denotes the least common multiple function. For 40% of the points, we calculate the LCM and check if it can be made from the cuts. We can do this by seeing if the LCM can be divided by the distinct prime factors of the cuts. It is sufficient to use the distinct prime factors since cuts will be multiples of those distinct primes. It might not be possible to form the LCM exactly, but it is guaranteed that we will form some multiple of the LCM (for example, LCM = 3, and the only cut is 27).
Since calculating the LCM of 1000 numbers that are up to will likely overflow any data-type (and time out otherwise due to the large number of digits), we need to make one more observation. We note that in order to form , all distinct prime factors in all must be present in the LCM. Since a cut can be repeated many times, all we need to check is if the distinct prime factors in all are present in the set of distinct prime factors of the cuts.
In order to quickly generate primes, we can use the sieve of Eratosthenes in order to quickly generate primes, which has a time complexity of , where is the largest number to check.
Time Complexity: , where is the maximum of all and and denotes the pi function, which counts the number of primes less than or equal to .
Comments