In this problem you just need to implement following algorithm. Split input data into 3 vectors: first will contain negative numbers, second positive numbers, third zeroes. If size of first vector is even move one number from it to the third vector. If second vector is empty, then move two numbers from first vector to the second vector. This solution works in O(n).
Input data represents a graph. If there is a connected component with at least 4 vertexes, then answer is - 1. Every connected component with 3 vertexes is a complete team. Other teams are made from 1 or 2-vertex components. If amount of 2-vertex components is greater than 1-vertex answer is - 1. Otherwise match 2-vertex components with 1-vertex. If there are some 1-vertex components left then split them into groups of three. This algorithm works in O(n + m). Also you could implement O(n4) solution.
Let's MOD = 1000000007. Let's precalc factorial values modulo MOD. fact[i] = i!%MOD, . Let i be an amount of digits equal to a in current excellent number. In this case we can find sum of digits in this number: sum = ai + b(n - i). If sum is good, then add C[n][i] to answer. In this problem it's impossible to calculate binomial coefficients using Pascal's triangle, because of large n. However it can be done this way C[n][i] = fact[n]inv(fact[n - i]fact[i]). inv(a) is multiplicative inverse element(modulo MOD). MOD is a prime number, so inv(a) = aMOD - 2. Calculating this values for each i from 0 to n will give correct answer in O(nlog(MOD)).
This picture is helpful for understanding.
Let's consider problem D in graph terms:
We have matrix n × n, which represents a graph:
- It is tree.
- Every vertex, except leaves, has 4 children.
- There are 4k distinct vertexes, with distance k from root.
We need to color k vertexes of this graph. By that we mean also to color all vertexes on path from i to 1(root).
Knowing height of tree we can build it in unique way. Let's find height of tree in this way:
int height = 0;
while (n > 1 && n % 2 == 1) {
n /= 2; height++;
}
Let's consider following DP: z[i][j] — number of ways to color graph height i in j steps.
Naive solution in O(k4log(n)):
z[0][0] = 1; z[0][i] = 0, i > 0; z[i][0] = 1, i > 0
z[i][j] = 0;
for(int k1 = 0; k1 <= j - 1; k1++)
for(int k2 = 0; k2 <= j - 1 - k1; k2++)
for(int k3 = 0; k3 <= j - 1 - k1 - k2; k3++)
{
int k4 = j - 1 - k1 - k2 - k3;
z[i][j] += z[i-1][k1] * z[i-1][k2] * z[i-1][k3] * z[i-1][k4];
z[i][j] %= mod;
}
But it is not what we whant in time terms. Let's consider current DP as polynomial coefficients: z[i][j] — coefficient of power j of polynomial i. In that case z[i + 1][j + 1] — coefficient of power j of polynomial i to the 4-th power. This approach allows to solve problem in O(k2log(n)). However this solution is quite slow, because of modulo operations. As you see, this modulo is not so big ( ≤ 107), that allows us to reduce number of modulo operations, thus giving huge perfomance boost. Also it is possible to use FFT to solve in O(klog(k)log(n)).
Аuthor's solution. Without FFT
Let's . val is upper bound for answer. val! is divisible by
, you can easily prove it using facts about prime powers in factorial and following inequality
. By the way,
is called multinomial coefficient. So answer can't exceed 1013.
If n! divisible by den, then (n + 1)! is also divisible by den. That means that function of divisibility is monotonic and we can use binary search.
For every i, i = 2..., 107, let's precalc max prime in i using linear sieve of Eratosthenes. For i it will be lp[i]. After that let's create a vector, with all primes less then 107.
Now let's calculate following values cnt[i] — amount of numbers a, i < = a.
Now me can factorize denominator like this:
for(int i = max; i>=2; i--) {
if (lp[i] != i)
cnt[lp[i]] += cnt[i];
cnt[i / lp[i]] += cnt[i];
}
Finally we use binary search from lf = 1 to .
What is O(n^4) solution for B?
I used Floyd-Warshall which works in O(n^3).
Authors solution for E looks to be a different program from the intended.
thanks. Now fixed.
I think by mistake both are same link in D
Thank you.
Really great to have the analysis so quickly, btw!
About solution C, I will appreciate a theory reference for the MOD-2 theorem in the inverse! Thanks
You can read it here, for example. It's based on Euler's theorem.
For solution D, can you (or someone) explain what are the following pieces of code doing?
It's basically splitting power of 4 into two square operations. The first calculates power of 2 on the polynomial, stored into a temporary array, then the second calculated power of 2 on the temporary array.
Thanks! but I don't really understand this too:
Let's consider current DP as polynomial coefficients: z[i][j] — coefficient of power j of polynomial i. In that case z[i + 1][j + 1] — coefficient of power j of polynomial i to the 4-th power.
what does that mean by "to the 4-th power" and why is that the answer?
Is it just me or is E actually easier than D? (I used an approach different to the max-prime, instead I used a modified sieve of Erathostenes to determine the exponent of each prime in the product by looping through all the integers divisible by it and getting max. powers of this prime dividing each of them.) Almost did it during the contest, too... or maybe I just like number theory :D
can anybody clarify the naive solution of D ? I cant understand it although read again and again..