UVa 12358 - Candy's Candy

UVa 12358 - Candy's Candy

 * 

Summary
There are several candies of different flavors. A valid packing of candies must respect all the following restrictions:


 * Each piece of candy must be placed in exactly one pack.
 * Each pack, regardless of its type, must contain at least 2 pieces of candy.
 * Each pack, regardless of its type, must contain the same number of pieces of candy.
 * Within each variety pack, the number of pieces of candy of each flavor must be the same.
 * There must be at least one variety pack.
 * There must be at least one flavored pack of each flavor.

How many valid packing are there?

Limits
Number of flavors N   2 <= N <= 10^5 Number of candies of flavor x   1 <= C_x <= 10^9,   for 0 <= x < N

Explanation
Let's look at the problem in a more mathematical way. Count how many tuples (V, S) are there respecting the following equations: V * (S / N) + F_x * (S) = C_x,   para 0 <= x < N    V >= 1 F_x >= 1   =>   V <= (C_x - (S/N))/S Where: S  = Pack Size; V  = Quantity of Variety Packs; F_x = Quantity of Flavored Packs with flavor x.

These equations represent the conditions imposed. In special, the first one can be obtained by counting the number of candies of flavor x.

It is important to notice how limited is the set of possible values of S. We have the following restrictions: N | S              (So we can mount the Variety Packs) S | sum(C_x)       (So we can divide all candies in packs)

Therefore, the set is contained in: PS = {N * D : D | [sum(C_x) / N]}

That means we only need to calculate the divisors of [sum(C_x) / N]. From C <= MAX_C = 1e9: sum(C_x) / N <= (N * MAX_C) / N = MAX_C Therefore: |PS| <= sqrt(1e9)

However, there are more restrictions to S. We still need to impose that all the diophantine equations relative to the flavors are simultaneously respected.

V * (S / N) + F_i * (S) = C_i -  V * (S / N) + F_j * (S) = C_j

.:. (F_i - F_j) * S = C_i - C_j .:. (F_i - F_j) = (C_i - C_j) / S

.:. S | (C_i - C_j),           for all i | 0 <= i < N,  0 <= j < N .:. S | gcd(C_i - C_j),       for all i | 0 <= i < N,  0 <= j < N

Still, treating all these cases leads us to a quadratic complexity (Too much for this problem). We can reduce it with this property: gcd(A, B) = gcd(A - B, A),  for A > B

.:. gcd(A - B, A - C, C - B)  = gcd(A - B, A - C, A - B)   = gcd(A - B, A - C)

The idea can be extended for more variables. Therefore, S | G = gcd(C_0 - C_i),      for all 1 <= i < N, C_0 != C_i

We must refine the construction of the set PS from this new restriction, so we only have valid values of S.   VS = {N * D : D | gcd(sum(C_x), G) / N}

Now we can, for each S in VS, count how many values of V respect the diophnatine equations.

It's only left to impose the restriction below: 1 <= V <= (C_x - (S/N))/S

Notice that the superior limite has the same character of groth of C_x, therefore we may only consider the minimum C_x given. Let us call it C_min.

By anyalising the equation below, we can find the least valid increment k we can apply to V so the restriction holds. (V + k) * (S / N) + (F + l) * (S) = C_x V * (S / N) + F_x * (S) + k * (S / N) + l * S = C_x k * (S / N) + l * S = 0 k / N + l = 0 l = -k / N

.:. N | k

Therefore, the number of valid values of V is: VV = max(                   0, ceil(  ( (C_x - (S/N))/S - 1 ) / N  )             )

The, we have the answer

ANS = sum(VV),   for all S in {N * D : D | gcd(sum(C_x), G) / N}

Complexity
Let T be the number of test cases. O( T * ( N * gcd + getDiv ) ) O( T * ( N * log(N) + sqrt(MAX_C) ) ) = T * 2e6

Input
3 15 33 21 2 1 1 2 2 2 2 3 3 3 1000000000 1000000000 1000000000 0

Output
4 0 0 1 832519396