Matrix,in which all diagonal elements equal k and other elements equal 0, satisfied all conditions.
For example, if n = 4 and k = 7, our matrix will be
7 0 0 0
0 7 0 0
0 0 7 0
0 0 0 7
gcd(1, m) = 1, so if n = k, there is no suitable permutation.
It is well known that gcd(m, m - 1) = 1. Lets construct following permutation. It has exactly k good elements.
n - k 1 2 3 ... n - k - 1 n - k + 1 n - k + 2 ... n
Let's find such value b[i] that a[i] ≤ b[i] for all indeces i. Let's simulate all operations and diff[i] will be the difference between current value of i-th element and its initial value. If we have operation of first type, we change values of diff[i]. If we have operation of second type, we know that a[i] + diff[i] ≤ m[i], so a[i] ≤ m[i] - diff[i]. We will get array b when we union all this inequalities.
Let's prove that either b satisfied all conditions or there is no such array. It can be two cases, why b does not suit:
— it's impossible due to construction of array b.
— b[i] is a maximal possible value of a[i], so can't be bigger.
Let's solve this problem using binary search. We need to check whether we can achieve an array, when c(a) will be at most x. Lets make dp. dp[i] means minimal number of elements with indeces less than i, which we need to change, but we don't change i-th element. Let's iterate next element j, which we don't change. Then we know that we can change all elements between i and j. It is equivalent to such condition
|a j - a i| ≤ (j - i)·x
Difference between neighboring elements can be at most x. The maximal possible difference increases by x exactly j - i times between elements i and j, so this inequality is correct.
Let's count amount of such substrings of t that are bigger than corresponding substring of s and begin at the position i.
If t[i] < s[i], this amount equals 0.
If t[i] > s[i], this amount equals n - i.
If t[i] = s[i], then let's find such nearest position j, j > i , that t[j] ≠ s[j]. If t[j] > s[j], needed amount of substrings will be n - j. If t[j] < s[j], needed amount of substrings will be 0.
We can rephrase this: If t[i] > s[i], it will be (1 + pref)·(n - i) new substrings, where pref means how many last elements in s and t is equal.
Let's make dp. dp[i][sum] means that we viewed i positions, have sum needed substrings and s[i] ≠ t[i]. Lets iterate their common prefix pref.
If t[i] < s[i], dp[i][sum] + = dp[i - pref - 1][sum]·(s[i] - 'a') — we can count this value using partial sums.
If t[i] > s[i], dp[i][sum] + = dp[i - pref - 1][sum - (1 + pref)·(n - i)]·('z' - s[i]). Let's iterate pref.
Let's note that sum - pref·(n - i) ≥ 0, so pref ≤ sum / (n - i) and pref ≤ k / (n - i). This means that third cycle will make at most k / (n - i) iterations when we find value of dp[i][sum]. Let's count total number of iterations:
= < k·(n + k·log k).
p is prime, so there exist primitive root g modulo p(We don't need to find it, but we know that it exists). We can write a i = g r i. Note, that i-th set consists of all numbers , where c j ≥ 0, or we can write it as .
By Fermat's little theorem a p - 1 = 1 mod p we have that a k mod p = a k mod (p - 1) mod p. So can be equal to all values k·t modulo p - 1, where t = gcd(b 1, b 2, ... , b m, p - 1). Note that t doesn't depend on r i, so we can assign g = g t. Then all elements of i-th set will be g r i·k, where k ≥ 0. Now we can replace b i by q i, where q i = gcd(r i, p - 1), as we do with b i at the beginning. Then all elements of i-th set will be g q i·k, where k ≥ 0. This means that if we write all values g 0, g 1, ..., g p - 2 in a line, i-th set will contain every q i-th element.
Now we need to find union of this sets.Let's do it using inclusion-exclusion principle. All our numbers are divisors of p - 1. Let's dp i be the coefficient near i in inclusion-exclusion principle( i is a divisor of p - 1) and we can find this values, adding all q i alternatively.
Also we need to find q i. Let's find volume of the i-th set. It is equal to . From the other side it is equal to such minimal number d i, that a i d i = 1 mod p ( d i is a cycle). From a i p - 1 = 1 mod p we have that p - 1 is divisible by d i. So we can find d i as a divisor of p - 1. And .
Firstly we will solve problem if first player can win.
Let's make all roads that we can change equal to r[i] and do two Dijkstra's algorithms from vertices s 1 and s 2. Let's d1[i] be the distance from s 1 to i, d2[i] be the distance from s 2 to i. Consider a road, that we can change, from a to b. If d1[a] < d2[a], we will set length of such road equal to l[i] and do two Dijkstra's algorithms again. We run such process until any road changes its length.
If d1[f] < d2[f] after all changes then first player wins.
If we replace condition d1[a] < d2[a] by d1[a] ≤ d2[a], we can check if Levko can end this game with a draw.
Let's call "edges" only roads which Levko can change. When we do Dijkstra's algorithm we use all roads, not only edges.
Let's prove that if there exist such edges values that first player wins, there exist values of edges such that first player wins and all this values equal either l[i] or r[i]. Consider shortest pathes of both players.
If only first player goes on edge from a to b, we can set its value l[i]. Proof: there must hold d1[a] < d2[a] because first player goes on it and wins. This condition holds after change of value of this edge. If second player goes on this edge, he loses because d1[f] ≤ d1[a] + d(a, b) + d(b, f) < d2[a] + d(a, b) + d(b, F) = d2[f]. If second player doesn't go on this edge, he loses because shortest path of first player became smaller( d(x, y) — shortest path from x to y).
If only second player goes on edge from a to b, we can set its value r[i]. Proof: Shortest path of the first player doesn't change and shortest path of second player can only become larger.
If no player go on edge, we can set its value r[i]. Proof: Shortest pathes of both players doesn't change.
If both players go on edge from a to b, we can set its value l[i]. Proof: Shortest pathes of both players decrease by (initial value of this edge — l[i]).
After every such operation first player wins again and all edges become either l[i] or r[i].
Consider result of our algorithm. Let's call edge "good" if its value is equal to l[i] and "bad" if its value equals r[i].
(a) Let's prove that after performing all operations we will have d1[a] < d2[a] for all good edges (a, b). If we have d1[a1] < d2[a1] for edge (a1, b1) and after changing value of (a2, b2) this condition doesn't hold. We have d1[a1] > = d2[a1], d1[a2] < d2[a2]. We change only one edge and shortest path from s2 to f become shorter so edge (a2, b2) lies on this path.
d2[a1] = d2[a2] + d(a2, b2) + d(b2, a1) > d1[a2] + d(a2, b2) + d(b2, a1) ≥ d1[a1]. Contradiction. (b) Let's prove that after performing all operations we will have d1[a] ≥ d2[a] for all bad edges. We can continue our procces otherwise.
(c) Let's prove that if condition d1[a] < d2[a] holds for some edge but we doesn't change it on this iteration, this continion holds after this iteration. Proof is same as in (a).
(d) Let's prove that if any subset of good edges is equal to l[i] and d1[a] < d2[a], ift also holds when we make all good edges equal l[i]. Let's simulate all procces and use (c).
Lets prove that for all edges values(not necessary only l[i] or r[i]), d1[a] ≥ d2[a] for all bad edges (a, b).
Assume that we have such edge. Consider shortest path of first player to its beginning. If there exist bad edges (a1, b1) on this path, there must holds inequality d1[a1] < d2[a1]. Consider first of this bad edges (a, b). Then shortest path of first player to a doesn't consist any bad edge. Consider problem,m which is equivalent to our problem but finish is in vertex a. good and bad edges will be same. Let's change all value of edges as we do in item 1. Note? that all bad edges will be equal to r[i]. So only subset of good edges can be equal to l[i] and d1[a1] < d2[a1]. By (d) we have that we can set all good edges l[i] and condition d1[a1] < d2[a1] will be satisfied. So we have contradiction thst this edge is bad.
This means that if first player goes on any bad edge, he loses. So we can set r[i] to all this edges. So we can set l[i] to some subset of good edges. By (d) we have that if we have d1[f] < d2[f] for some subset of good edges, this condition will be true if we set all good edges l[i].
Note that proof will be same if we want to check whether Levko can end a game with a draw.