The problem was suggested by unprost.
Consider the process from the end. The last buyer will always buy a half of an apple and get a half for free (so the last string always is halfplus). After that each buyer increases the number of apples twice and also maybe by one. So we simply have the binary presentation of the number of apples from the end. To calculate the answer we should simply restore that value from the end and also calculate the total money grandma should have.
С++ solution by me.
The problem was suggested by Lewin Gan lewin.
Let's calculate the prefix sums for all numbers (and store it in array s1) and for numbers with letter B (and store it in array s2). Now we can find the sum of all numbers in any segment in O(1) time and the sum of numbers with letter B.
Let's iterate over prefix or suffix to flip and calculate the sum in that case by formulas: sum(s1, 0, n - 1) + sum(s2, 0, i) - 2·sum(s1, 0, i) for prefixes and sum(s1, 0, n - 1) + sum(s2, i, n - 1) - 2·sum(s1, i, n - 1) for suffixes.
C++ solution by me.
The problem was suggested by Lewin Gan lewin. The proof of the transitivity also belongs to him.
Let's sort all the strings by comparator a + b < b + a and concatenate them. Let's prove that it's the optimal answer. Let that operator be transitive (so if ). Consider an optimal answer with two strings in reverse order by that operator. Because of the transitivity of operator we can assume that pair of strings are neighbouring. But then we can swap them and get the better answer.
Let's prove the transitivity of operator. Consider the strings as the 26-base numbers. Then the relation a + b < b + a equivalent to . The last is simply the relation between real numbers. So we proved the transitivity of the relation a + b < b + a.
C++ solution by me.
Complexity: O(nLlogn), where L is the maximal string length.
The problem was suggested by Denis Bezrukov pitfall.
Let cnt x be the number of occurences of the number x in the given array (easy to see that we can ignore the numbers greater than m). Let's iterate over and 1 ≤ k, x·k ≤ m and increase the value in the position k·x in some array z by the value cnt x. So the value z l equals the number of numbers in the given array which divide l. Let's find the minimal l with the maximum value z l (1 ≤ l ≤ m). Easy to see that the answer to the problem is the numbers which divide l.
Let's calculate the complexity of the solution. The number of the pairs (k, x) we can bound with the value .
C++ solution by me.
Complexity: O(n + mlogm).
The problem was suggested by Alexey Chesnokov CleRIC.
Let k = 2, then it is the standard problem which can be solved by FFT (Fast Fourier Transform). The solution is the following: consider the polynomial which the i-th coefficient equals to one if and only if there is the number i in the given array. Let's multiply that polynomial by itself and find i for which the coefficient in square not equals to 0. Those values i will be in the answer. Easy to modificate the solution for the arbitrary k. We should simply calculate the k-th degree of the polynomial. The complexity will be WlogWlogk, where W is the maximal sum.
We can improve that solution. Instead of calculating the k-th degree of the polynomial we can calculate the k-th degree of the DFT of the polynomial. The only problem is the large values of the k-th degrees. We can't use FFT with complex numbers, because of the precision problems. But we can do that with NTT (Number-theoretic transform). But that solution also has a problem. It can happen that some coefficients became equals to zero modulo p, but actually they are not equal to zero. To get round that problem we can choose two-three random modules and get the complexity O(W(logW + logk)).
The main author solution has the complexity O(WlogWlogk) (FFT with complex numbers), the second solution has the same complexity, but uses NTT and the third solution has the improved complexity (but it was already hacked by halyavin).
С++ solution, complex FFT by me.
С++ solution, NTT by me.
С++ solution, improved NTT by me.
P.S.: To get faster solution you should each time multiply the polynomials of the required degree, but not of the degree 220.
Complexity: O(WlogWlogk) or O(W(logW + logk)), depending the bravery of the coder :-)
UPD: It turns out that the first approach also has complexity O(W(logW + logk)). See below the comment of halyavin.
The problem was suggested by Lewin Gan lewin. The solution and proof also belongs to him.
Consider the undirected complete graph with n nodes, with an edge between nodes i, j with cost a ij. Let B ij denote the minimum possible value of the max edge of a path from i to j. We know that a ij ≥ B ij by definition.
If the matrix is magic, we can choose arbitrary k 1, k 2, ..., k m such that a ij ≤ max(a i, k 1, a k 1, k 2, ..., a k m, j) by repeating invocations of the inequality given. Also, you can show that if this inequality is satisfied, then the matrix is magic (by choosing an m = 1 and k 1 arbitrary).
So, this shows that the matrix is magic if and only if a ij ≤ B ij. Thus, combining with a ij ≥ B ij, we have a ij = B ij.
We need a fast way to compute B ij for all pairs i, j. This can be computed as the MST, as the path in the MST minimizes the max edge between all pairs of nodes. So, the algorithm works as follows. First, find the MST on the complete graph. Then, the matrix is magic if and only if the max edge on the path between i, j in the MST is exactly equal to a i, j. Also you shouldn't forget to check symmetry of the matrix and diagonal for zeros.
P.S.: Unfortunately we couldn't increase the value n in this problem: the tests already had the size about 67MB and they couldn't be given with generator. So most of the users who solved this problem uses bitset-s. The complexity of their solution is , where b = 32 or b = 64.
C++ solution, binary lifts by me.
Complexity: O(n 2 logn) or O(n 2).