### pikmike's blog

By pikmike, history, 19 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)  Comments (41)
 » 19 months ago, # | ← Rev. 2 →   Thanks for spending your time to write an editorial, but i think it would be better if you include some hint !
 » 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
•  » » 19 months ago, # ^ | ← Rev. 2 →   Any number's one digit sum equivalence is equal to number modulo 9
•  » » 19 months ago, # ^ | ← Rev. 4 →   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.
•  » » 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.
•  » » 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,
 » 19 months ago, # | ← Rev. 2 →   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
 » For problem D, the brute force solution apparently runs fast enough.
•  » » 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
•  » » » c can be treated as a constant.
•  » » » » 18 months ago, # ^ | ← Rev. 2 →   I think c can't be treated as a constant... c changes when n changes...but certainly , c is always less than ......
 » 19 months ago, # | ← Rev. 2 →   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 ??
•  » » Please.. Someone explain me this with an example too.
•  » » 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 is equal to A, A, A,A. which will not be possible if these values are different.
 » 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
 » Why additional time is not given for Java programmers? Even the solution given in editorial for problem D is giving TLE in Java.
 » 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?
 » 19 months ago, # | ← Rev. 2 →   Regarding the tutorial for problem E. I think it should be l ≤ mid ≤ r instead of 1 ≤ mid ≤ r here.
•  » » also mid<=r only if cnt=1
•  » » » dp dig, l, r, cnt = max{ans l, mid - 1 + dp dig, mid + 1, r, cnt - 1}what is the difference between these two? dp dig, l, r, cnt = max{ans l, mid - 1 + dp dig, mid, r, cnt}i mean is there something wrong with the second one?
 » Someone please explain the solution for problem-E, I didn't understand it from the tutorial.
•  » » Made a solution just doing recursion on strings to show the idea (solution). I think this is a bit easier to follow.
•  » » » 18 months ago, # ^ | ← Rev. 2 →   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
•  » » » » 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.
•  » » » » » 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!
•  » » » » » » 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 n 3 possible different strings.
•  » » » » » » » Yep, sorry. So it runs in N^3 right?
•  » » » » » » » » The algorithm will ever only visit n 3 strings but for each string it will do at most n 2 work. This is because for each string it might need to lookup n strings in a hashtable. So it runs in n 5 with a very good constant, this could be lowered to n 4 by playing around with indices instead of strings.
 » Could anyone elaborate on the solution of Problem D ? Please !
 » 18 months ago, # | ← Rev. 3 →   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.
 » 18 months ago, # | ← Rev. 2 →   I will give a proof in this comment on why the formula for 1107B - Digital root 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 x j substracting 1 means that the trailing 0 s become 9 s so the disappear modulo 9 and if k is the first non-zero digit of x j 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 S b(x) which can be characterized like: The proof is the same as the above just replace 10 with b and 9 with b - 1.
 » 18 months ago, # | ← Rev. 3 →   good
 » Can someone please explain the editorial of E in more detailed way ?
 » 15 months ago, # | ← Rev. 2 →   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.
 » vovuh In D why can't we use binary search across the divisors of n to find x?
•  » » 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 
•  » » » Thanks
 » 5 months ago, # | ← Rev. 2 →   A different approach for problem D hereUsing gcd
•  » » interesting approach.
 » Whosoever solves problem E.Please let me know how the complexity of the solution is O(n^4).
 » 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.