codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

360.md (11657B)


      1 Source: https://codeforces.com/blog/entry/9529
      2 
      3 #### [361A - Levko and Table](/contest/361/problem/A "Codeforces Round 210 (Div. 2)")
      4 
      5 Matrix,in which all diagonal elements equal *k* and other elements equal 0, satisfied all conditions.
      6 
      7 For example, if n = 4 and k = 7, our matrix will be
      8 
      9 7 0 0 0
     10 
     11 0 7 0 0
     12 
     13 0 0 7 0
     14 
     15 0 0 0 7
     16 
     17 #### [361B - Levko and Permutation](/contest/361/problem/B "Codeforces Round 210 (Div. 2)")
     18 
     19 *gcd*(1, *m*) = 1, so if *n* = *k*, there is no suitable permutation.
     20 
     21 It is well known that *gcd*(*m*, *m* - 1) = 1. Lets construct following permutation. It has exactly *k* good elements.
     22 
     23 *n* - *k*  1  2  3  ...   *n* - *k* - 1  *n* - *k* + 1  *n* - *k* + 2  ...   *n*
     24 
     25 #### [360A - Levko and Array Recovery](/contest/360/problem/A "Codeforces Round 210 (Div. 1)")
     26 
     27 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.
     28 
     29 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:
     30 
     31 1. ![](https://espresso.codeforces.com/43276438da99cc4c8df6b9745e9d8949f38b7a6a.png) — it's impossible due to construction of array *b*.
     32 2. ![](https://espresso.codeforces.com/f478ddcce8a7d508190dae64199388530205df49.png) — *b*[*i*] is a maximal possible value of *a*[*i*], so ![](https://espresso.codeforces.com/2664dadd317791ef8b53564bae147664f12e9d5d.png) can't be bigger.
     33 
     34 #### [360B - Levko and Array](/contest/360/problem/B "Codeforces Round 210 (Div. 1)")
     35 
     36 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
     37 
     38 |*a**j* - *a**i*| ≤ (*j* - *i*)·*x*
     39 
     40 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.
     41 
     42 #### [360C - Levko and Strings](/contest/360/problem/C "Codeforces Round 210 (Div. 1)")
     43 
     44 Let's count amount of such substrings of *t* that are bigger than corresponding substring of *s* and begin at the position *i*.
     45 
     46 1. If *t*[*i*] < *s*[*i*], this amount equals 0.
     47 2. If *t*[*i*] > *s*[*i*], this amount equals *n* - *i*.
     48 3. 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.
     49 
     50 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.
     51 
     52 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*.
     53 
     54 If *t*[*i*] < *s*[*i*], *dp*[*i*][*sum*] +  = *dp*[*i* - *pref* - 1][*sum*]·(*s*[*i*] - '*a*') — we can count this value using partial sums.
     55 
     56 If *t*[*i*] > *s*[*i*], *dp*[*i*][*sum*] +  = *dp*[*i* - *pref* - 1][*sum* - (1 + *pref*)·(*n* - *i*)]·('*z*' - *s*[*i*]). Let's iterate *pref*.
     57 
     58 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:
     59 
     60 ![](https://espresso.codeforces.com/ad02e25fb96c56042a7157d68abadc3406ffcc16.png)  =  ![](https://espresso.codeforces.com/4806c3f0958cedb861a2d57c54a061393ff89a15.png)  <  *k*·(*n* + *k*·*log*  *k*).
     61 
     62 #### [360D - Levko and Sets](/contest/360/problem/D "Codeforces Round 210 (Div. 1)")
     63 
     64 *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 ![](https://espresso.codeforces.com/0067a073d6b62ecb4bc6515e3cb6691133ccc5fb.png), where *c**j* ≥ 0, or we can write it as ![](https://espresso.codeforces.com/6a2c92cc709f5ce68b35fbaab027177c14707c59.png).
     65 
     66 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 ![](https://espresso.codeforces.com/672cd850738c6d81a3ee508f32a19c5fa46a35ae.png) 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.
     67 
     68 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.
     69 
     70 Also we need to find *q**i*. Let's find volume of the i-th set. It is equal to ![](https://espresso.codeforces.com/c67797901173e5a7e9618b7436151f8cb575d341.png). 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 ![](https://espresso.codeforces.com/ac8c33fdbb35d89596b0120e5ec54737acd96c45.png).
     71 
     72 #### [360E - Levko and Game](/contest/360/problem/E "Codeforces Round 210 (Div. 1)")
     73 
     74 **Algorithm:**
     75 
     76 Firstly we will solve problem if first player can win.
     77 
     78 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 *d*1[*i*] be the distance from *s*1 to *i*, *d*2[*i*] be the distance from *s*2 to *i*. Consider a road, that we can change, from *a* to *b*. If *d*1[*a*] < *d*2[*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.
     79 
     80 If *d*1[*f*] < *d*2[*f*] after all changes then first player wins.
     81 
     82 If we replace condition *d*1[*a*] < *d*2[*a*] by *d*1[*a*] ≤ *d*2[*a*], we can check if Levko can end this game with a draw.
     83 
     84 **Proof:**
     85 
     86 Let's call "edges" only roads which Levko can change. When we do Dijkstra's algorithm we use all roads, not only edges.
     87 
     88 1. 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.  
     89    If only first player goes on edge from *a* to *b*, we can set its value *l*[*i*]. Proof: there must hold *d*1[*a*] < *d*2[*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 *d*1[*f*] ≤ *d*1[*a*] + *d*(*a*, *b*) + *d*(*b*, *f*) < *d*2[*a*] + *d*(*a*, *b*) + *d*(*b*, *F*) = *d*2[*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*).  
     90    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.  
     91    If no player go on edge, we can set its value *r*[*i*]. Proof: Shortest pathes of both players doesn't change.  
     92    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*]).  
     93    After every such operation first player wins again and all edges become either *l*[*i*] or *r*[*i*].
     94 2. 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*].  
     95    (a) Let's prove that after performing all operations we will have *d*1[*a*] < *d*2[*a*] for all good edges (*a*, *b*). If we have *d*1[*a*1] < *d*2[*a*1] for edge (*a*1, *b*1) and after changing value of (*a*2, *b*2) this condition doesn't hold. We have *d*1[*a*1] >  = *d*2[*a*1], *d*1[*a*2] < *d*2[*a*2]. We change only one edge and shortest path from *s*2 to *f* become shorter so edge (*a*2, *b*2) lies on this path.  
     96    *d*2[*a*1] = *d*2[*a*2] + *d*(*a*2, *b*2) + *d*(*b*2, *a*1) > *d*1[*a*2] + *d*(*a*2, *b*2) + *d*(*b*2, *a*1) ≥ *d*1[*a*1]. Contradiction. (b) Let's prove that after performing all operations we will have *d*1[*a*] ≥ *d*2[*a*] for all bad edges. We can continue our procces otherwise.  
     97    (c) Let's prove that if condition *d*1[*a*] < *d*2[*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).  
     98    (d) Let's prove that if any subset of good edges is equal to *l*[*i*] and *d*1[*a*] < *d*2[*a*], ift also holds when we make all good edges equal *l*[*i*]. Let's simulate all procces and use (c).
     99 3. Lets prove that for all edges values(not necessary only *l*[*i*] or *r*[*i*]), *d*1[*a*] ≥ *d*2[*a*] for all bad edges (*a*, *b*).  
    100    Assume that we have such edge. Consider shortest path of first player to its beginning. If there exist bad edges (*a*1, *b*1) on this path, there must holds inequality *d*1[*a*1] < *d*2[*a*1]. 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 *d*1[*a*1] < *d*2[*a*1]. By (d) we have that we can set all good edges *l*[*i*] and condition *d*1[*a*1] < *d*2[*a*1] will be satisfied. So we have contradiction thst this edge is bad.
    101 4. 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 *d*1[*f*] < *d*2[*f*] for some subset of good edges, this condition will be true if we set all good edges *l*[*i*].
    102 5. Note that proof will be same if we want to check whether Levko can end a game with a draw.