### pikmike's blog

By pikmike, history, 22 months ago, translation,

1107A - Digits Sequence Dividing

Tutorial
Solution (Vovuh)

1107B - Digital root

Tutorial
Solution (Ne0n25)

1107C - Brutality

Tutorial
Solution (Vovuh)

1107D - Compression

Tutorial
Solution (Vovuh)

1107E - Vasya and Binary String

Tutorial
Solution (Roms)

1107F - Vasya and Endless Credits

Tutorial
Solution (Roms)

1107G - Vasya and Maximum Profit

Tutorial
Solution (Roms)

• +55

 » 22 months ago, # | ← Rev. 2 →   -8 Thanks for spending your time to write an editorial, but i think it would be better if you include some hint !
 » 22 months ago, # |   0 Someone help with the solution for problem-B. I can't "easily" get it :( How did it go from the formula to (k−1)⋅9+x
•  » » 22 months ago, # ^ | ← Rev. 2 →   0 Any number's one digit sum equivalence is equal to number modulo 9
•  » » 22 months ago, # ^ | ← Rev. 4 →   0 Assume you understand the given formula.Then, you will easily find that S(x) repeats every 9 integers.So, you can write it as my + n, y should be related to k, n should be related to x, m should be a constant.The numbers 1 to 9 are the first cycle. Its digital roots are itself.The numbers 10 to 18 are the second cycle. Randomly pick two integers from the first and the second cycle, throw it into the my + n, you should then get the formula.
•  » » 22 months ago, # ^ |   +3 Just write a small function to print all results from 1 to 100, you can easily see the cycle pattern exist there. hope this can help.
•  » » 22 months ago, # ^ |   0 Its just a pattern, for 1 <= x <= 9 the sum of digits is x itself, for x >= 10 the pattern repeats x = 10, sum = 1 x = 11, sum = 2 x = 12,
 » 22 months ago, # | ← Rev. 2 →   +14 Problem G: For every diff[i], I calculated the range in which it is maximum and took maximum suffix from left side and maximum prefix from the right side to calculate the answer. Before constructing the segment tree,just to verify the logic I submitted the code with O(n) function to calculate max prefix and suffix expecting a TLE, but surprisingly it got ACed. Submission: 49068594 Are the test cases weak or is the time complexity of this solution lesser than O(n^2) EDIT: neal answered it here
 » 22 months ago, # |   +6 For problem D, the brute force solution apparently runs fast enough.
•  » » 22 months ago, # ^ |   +3 Yes,my brute force solution runs for 800+ ms.So I don't think this problem is very good and I don't know why this problem's difficulty is 1800. The time complexity just O(n²*c) ,c is the number of divisors of n
•  » » » 22 months ago, # ^ |   -9 c can be treated as a constant.
•  » » » » 22 months ago, # ^ | ← Rev. 2 →   +1 I think c can't be treated as a constant...c changes when n changes...but certainly , c is always less than ......
 » 22 months ago, # | ← Rev. 2 →   0 On the one hand, if some segment of kind [1+lx , 1 + (l+1)x], where l is the value from the range [0;n/x−1], contains different digits then we cannot compress the given matrix.Can someone explain me this with an example ??
•  » » 22 months ago, # ^ |   0 Please.. Someone explain me this with an example too.
•  » » 18 months ago, # ^ |   0 for example:- let us assume that the size of given matrix A is 8 and we are using x = 2, then values of Ceil(8/2) = 4,Ceil(7/2) = 4 which means B[4][4] is equal to A[8][8], A[7][8], A[8][7],A[7][7]. which will not be possible if these values are different.
 » 22 months ago, # |   +1 Problem G: Solved it using segment trees(max segment tree) and lazy updates. Start iterating i from 1 to n. For a fixed i, a node j(<=i) will contain the answer for the segment j to i. So at each iteration, make a query from 1 to i. For the update part, when going from i to i+1, add (a — c[i+1]) to the range (1 to i) and maintain a stack to store the gap values for the range. For the stack update, if the new value i.e. (d[i+1] — d[i])^2 is smaller than the top value of stack, just add the negative value of it to the stack with it's range and also update it's value in the segment tree. If the new value if larger than the top value of stack, keep popping the top value(these values also need to be updated in the segment tree) until the new value is smaller than the stack's top value. Submission: 49103066
 » 22 months ago, # |   -8 Why additional time is not given for Java programmers? Even the solution given in editorial for problem D is giving TLE in Java.
 » 22 months ago, # |   +3 In task F we don't have to pick every credit, so shouldn't the formula be min(0, rest of formula)? Every Hungarian algorithm (that I found) assign all of n elements. Or just b[i]*k[i]<=a[i], but the statement doesn't include it?
 » 22 months ago, # | ← Rev. 2 →   +8 Regarding the tutorial for problem E.I think it should be l ≤ mid ≤ r instead of 1 ≤ mid ≤ r here.
•  » » 22 months ago, # ^ |   +3 also mid<=r only if cnt=1
•  » » » 22 months ago, # ^ |   +1 dpdig, l, r, cnt = max{ansl, mid - 1 + dpdig, mid + 1, r, cnt - 1}what is the difference between these two? dpdig, l, r, cnt = max{ansl, mid - 1 + dpdig, mid, r, cnt}i mean is there something wrong with the second one?
 » 22 months ago, # |   0 Someone please explain the solution for problem-E, I didn't understand it from the tutorial.
•  » » 22 months ago, # ^ |   +1 Made a solution just doing recursion on strings to show the idea (solution). I think this is a bit easier to follow.
•  » » » 22 months ago, # ^ | ← Rev. 2 →   0 Nice Approach, I tried this and got AC, but before that I had tried to recursively remove each block of consecutive digits(same 1s or Os). Algorithm is right but I got TLE, I don't understand why it is. Can you explain what happen? HERE IS MY CODE: https://codeforces.com/contest/1107/submission/49218582
•  » » » » 22 months ago, # ^ |   0 The difficult part of this problem is not really to calculate the answer but to do it quickly. The reason why my code doesn't get TLE is that I practically only modify the beginning of the string, and if you think about it all the strings that is created will basically be a substring of the original string (except for the beginning part). So the number of strings I create are very limited.In your code you are not doing it nearly as neatly because you are deleting parts of the string everywhere (not just in the beginning). So you will create a ton of different strings, probably exponentially many, and because of that get TLE.
•  » » » » » 22 months ago, # ^ |   0 Yeah I understand your code, that is your state either is substring of original string or combination of two substrings of original string. The number of different substring is nC2 ~ N^2 and the number of combination of 2 substrings is (N^2)C2 ~ N^4, so your complexity is O(N^4). And my code may be up to N! So it gets TLE.Thanks for your help!
•  » » » » » » 22 months ago, # ^ |   +1 To be technical, its not either a substring of the original or a combination of two substrings. It is always a string on the form '0...0' + S[a:b] or '1...1' + S[a:b] so there are n3 possible different strings.
•  » » » » » » » 22 months ago, # ^ |   0 Yep, sorry. So it runs in N^3 right?
•  » » » » » » » » 22 months ago, # ^ |   0 The algorithm will ever only visit n3 strings but for each string it will do at most n2 work. This is because for each string it might need to lookup n strings in a hashtable. So it runs in n5 with a very good constant, this could be lowered to n4 by playing around with indices instead of strings.
 » 22 months ago, # |   0 Could anyone elaborate on the solution of Problem D ? Please !
 » 22 months ago, # | ← Rev. 3 →   0 I think your solution for problem A has a mistake. It doesn't work for "300". Am I right?UPD. I am sorry, I forget that zero is not used here.
 » 21 month(s) ago, # | ← Rev. 2 →   0 I will give a proof in this comment on why the formula for 1107B - Цифровой корень works.To see that the formula is true you can prove that and gives the same value or it is invariant under the formula. In the case of xj substracting 1 means that the trailing 0s become 9s so the disappear modulo 9 and if k is the first non-zero digit of xj then its value will decrease by 1. Then we can do something like this: It's not hard to see that given a b-ary number system where b is a positive integer if we define a similar process then the number x will end up in Sb(x) which can be characterized like: The proof is the same as the above just replace 10 with b and 9 with b - 1.
 » 21 month(s) ago, # | ← Rev. 3 →   0 good
 » 21 month(s) ago, # |   0 Can someone please explain the editorial of E in more detailed way ?
 » 18 months ago, # | ← Rev. 2 →   0 Can someone explain why DP calculation in E is correct i.e. why there exist 'mid', why optimal solution for DP doesn't contain more than one segment which will lead to 'cnt' number of 'dig' instead of just a segment [mid, r]?Upd: Got it.
 » 18 months ago, # |   0 vovuh In D why can't we use binary search across the divisors of n to find x?
•  » » 18 months ago, # ^ |   +1 Because the function isn't monotonic. For $n=12$ if the answer for $2$ and $4$ is true then it is not necessary that for $3$ the answer also will be true.Example (uncompressed): Test010101010101 010101010101 010101010101 010101010101 010101010101 010101010101 010101010101 010101010101 010101010101 010101010101 010101010101 010101010101 
•  » » » 18 months ago, # ^ |   0 Thanks
 » 8 months ago, # | ← Rev. 2 →   0 A different approach for problem D hereUsing gcd
•  » » 8 months ago, # ^ |   +3 interesting approach.
 » 8 months ago, # |   0 Whosoever solves problem E.Please let me know how the complexity of the solution is O(n^4).
 » 4 months ago, # |   0 For Problem G: I used Kadane's algorithm but it gives wrong answer on test 7 and test 36 only. Could anyone point out the issue with this approach. Code with some comments.
 » 2 months ago, # | ← Rev. 8 →   0 For problem E, if we use the strategy as follow:$f[l][r][i]$ is the answer if we clear $l$ to $r$ and another $i$ characters $s[l]$ on the left $([s[l],s[l],..,s[l],s[l+1],..,s[r]])$. And $f[l][r][i]=\min(f[l+1][r][0]+a[i+1],f[l+1][p-1]+f[p][r][i+1])$.Why can it still pass the tests if we don't do dp for $a[i]$ to make $a[i]\geq a[i-k]+a[k]$ ？