### awoo's blog

By awoo, history, 3 years ago,

1117A - Best Subsegment

Tutorial

1117B - Emotes

Tutorial
Solution (Vovuh)

1117C - Magic Ship

Tutorial
Solution (Roms)

1117D - Magic Gems

Tutorial
Solution (Reziba)

1117E - Decypher the String

Tutorial
Solution (BledDest)

1117F - Crisp String

Tutorial
Solution (PikMike)

1117G - Recursive Queries

Tutorial

• +54

 » 3 years ago, # |   +82 I've solved problem E using Chinese Remainder Theorem (CRT), by asking: s1 = 'abcd...wxyzabcd...' (string of length n with period 26) s2 = 'abcd...wxyabcd...' (string of length n with period 25) s3 = 'abcd...wabcd...' (string of length n with period 23) you can see more details in 5012664
•  » » 3 years ago, # ^ |   +1 such an impressive solution.
•  » » » 3 years ago, # ^ |   0 adventure Can u please explain ur solution? I don't get why u are doing CRT?
•  » » » » 3 years ago, # ^ |   +8 Suppose that r[i] be the answer of the testing system to string s[i], then for each position i in given string t, you are looking for its original position, say pos, and we know that from defintion of s[i], pos = r[1][i] - 'a' (mod 26), pos = r[2][i] - 'a' (mod 25) and pos = r[3][i] - 'a' (mod 23). note that gcd(23, 25) = gcd(25, 26) = gcd(26, 23) = 1.
•  » » » » » 3 years ago, # ^ | ← Rev. 2 →   +8 Ok well I understand what you trying to do. But I don't understand why would just 3 equations be enough for a position?I mean since, |s| can range upto 10000, each character's occurance can be upto ~400 times.Can you please give the math behind it?Thanks in advance :)
•  » » » » » » » 3 years ago, # ^ |   +8 Wow, Thanks a lot.Unfortunate it is for a user can only give a single upvote.
•  » » » » » » » » 3 years ago, # ^ | ← Rev. 2 →   +3 Don't worry. I like getting likes (oh well, who doesn't :) ), but way more important is that you understood and it turned out that I can be helpful :)
•  » » » » » » 3 years ago, # ^ |   0 suppose there are more than 1 answer — x1,x2,x3 ... so x1=x2=x3=... (mod N) (as of Chinese Remainder theorem) here N=23*25*26 >10000 ; so only 1 of x1,x2,x3 will be less than N . others will be >N . But given string will never concede N . So an unique answer will be found.
•  » » 3 years ago, # ^ |   0 I do not understand why we cannot use 2,3 and 5 instead of 26,25 and 23? Is it because the lcm should be greater than size of the string so that we have unique solutions ?
•  » » » 3 years ago, # ^ |   0 "Is it because the lcm should be greater than size of the string so that we have unique solutions ?"yes, exactly.
 » 3 years ago, # |   0 Can anyone help me with this solution for Problem D? It gives TLE, although I think the complexity should be O(m3 * logn).
•  » » 3 years ago, # ^ |   +4 Dont use unnecessary modulo operations.
•  » » » 3 years ago, # ^ |   0 Thanks a lot!I just noticed that too. Here is the solution that passed.
•  » » » » 3 years ago, # ^ |   +5 I advise you to change c[r][j]%=mod; to if( c[r][j] >= mod ) c[r][j] -= mod;( in void mult(vvlli& a, vvlli& b) ), in order to run faster. 50233106That's because mod is much more slower than minus.sorry for my poor English...
•  » » » » » 3 years ago, # ^ |   +1 The mult(ll, ll) function he calls in his matrix mult function is super slow, it got 3 modulo operations in it! Just some small modifications to his matrix multiplication function makes his solution go from 2823 ms to 327 ms. I did three changes to the matrix mult. Used c+=a*b instead of c+=mult(a,b), this removes 3 modulo operations Changed c%=mod to if (c>=mod*mod) c -= mod*mod, removing one more mod Changed the order of the for loops, see here.
•  » » » » » » 3 years ago, # ^ |   +8 Thanks a lot. And I read your comment @pajenegod of changing the order of the loops, and the difference was about 200ms (keeping everything else the same), which is quite good. But bringing it down to 327ms is quite brilliant for a solution which started with around 3300ms, with clever modifications.And I didn't know that mod was this slow than minus. Thanks @interestingLSY as well.
•  » » » » » » 2 years ago, # ^ |   0 How is c%=mod equivalent to if (c>=mod*mod) c-=mod*mod?Take c = mod+mod-1 for example.
•  » » » » » » 2 years ago, # ^ |   0 The trick of changing loop order does help a little bit.Good to know, thanks.
 » 3 years ago, # | ← Rev. 2 →   -8 About problem D，why we just split the last gem but not the other？Can anyone explain？
•  » » 3 years ago, # ^ |   0 If you want to split front gems , it has been done when calculating front dp values .
•  » » » 3 years ago, # ^ |   0 Yes,because the last gem has only two states，so it can be classified according to this. Thanks a lot.
 » 3 years ago, # |   0 Can anyone explain solution for D in more details? How does matrix exponentiation become solution?
•  » » 3 years ago, # ^ |   0
•  » » » 3 years ago, # ^ |   +1 Bro pls made a video in English
•  » » 3 years ago, # ^ | ← Rev. 2 →   +12 http://fusharblog.com/solving-linear-recurrence-for-programming-contest/ A good blog for matrix expo
•  » » » 3 years ago, # ^ |   0 Thanks a lot. I got stuck after finding out the formula.
 » 3 years ago, # |   +40 About matrix multiplication (Problem D). A while back I found a great trick for speeding up matrix multiplication by a fair bit. When doing matrix multiplication, just change the order of the for-loops (from the usual i,j,k to i,k,j) and you will have much better cache locality. See this.For example the editorial currently runs in 1014 ms but by just reordering the for loops in the matrix multiplication it takes 826 ms. The same trick also works in for example pypy. It changed the running time from 3.6 s to 2.9 s which made it pass the time limit.
•  » » 3 years ago, # ^ |   0 For me it just provided a slow down. 1466ms 1637ms Why did this happen?
•  » » » » 3 years ago, # ^ |   0 Thank you, this is an interesting topic. Do you know any other source on internet about this?
•  » » » » 3 years ago, # ^ |   0 Thank you ! I love this .
•  » » » » » 3 years ago, # ^ | ← Rev. 2 →   0 Glad you liked itI've played around further with matrix multiplication mod 109 + 7 in C++, and the best I've been able to get is around 109 ms. My conclusions are the following:The for loop order is really important, and even more with larger sized matrices. With unsigned long long you can do 18 additions of numbers of size (109 + 7)2 before having to take a modulo. This pretty much cuts down the modulo cost by a factor of 18.Store matrices as unsigned int. (32 bit is better than 64 bit because of fewer cache misses. Unsigned because it's much quicker to cast unsigned int to unsigned long long than int to unsigned long long)During multiplication you only temporarily need to store the result in an unsigned long long array, acting as a buffer. See my implementation for details.These tricks will make a huge difference, especially for larger sized matrices. Try for example running my code with input "1000000000000000000 300" and compare it to other solutions. My code does it in 2.4 s on cf.
•  » » » » » » 2 years ago, # ^ |   0 Just defer the mod of mul result to the end helps a lot, decreasing from 1200ms(81093813) to 800ms(81094265).Thanks a again for the tip.
 » 3 years ago, # |   0 If I learned matrix exponentiation!!! got stuck after finding dp[n] = dp[n — 1] + dp[n — m]
 » 3 years ago, # |   0 For D, I found a combinatorics equation which I am sure many of you might have arrived at.The relation was (summation of (n-k(m-1))C(k)). We need to go about putting different values of k till k <= n-k(m-1). As k increases (n-k(m-1)) would decrease and k would increase. Thus, termination of this process is assured. Now the question is how to solve this summation? I don't know how to proceed further. Can someone please help me with it. Thanks in advance.
•  » » 3 years ago, # ^ |   0 bro,i came up this method too,but i think the complexity of this method is too large.N/M=10^16. my english is poor.
 » 3 years ago, # |   0 In problem C, how is the function on which we are applying binary search a increasing function.In this part |x2-x3| + |y2-y3| <= k , it cannot be a increasing function because it can increase or decrease for different values of x3 & y3. x2 and y2 are destination points. For eg: let x2 = 7 and y2=7 Now initially x3 is 0 and y3 is also 0. x3=0,y3=0; Suppose string is "RLRL" After 1st step, x3 = 1(because first character is R) and y3=0, function value is abs(x2-x3) + abs(y2-y3) = abs(7-1) + abs(7-0) = 6+7 = 13 After 2nd step, x3 = 0(because second character is L) and y3=0, function value is= abs(7-0)+abs(7-0)=14 After 3rd step, x3 = 1(L is third character) and y3=0. function value becomes = abs(7-1)+abs(7-0)=13. After 4th step, x4 = 0, function value will be abs(7-0) + abs(7-0) = 14.The function is not increasing so how can we apply binary search for this. I have this doubt.
•  » » 3 years ago, # ^ | ← Rev. 2 →   +12 The proper function we binary search on is not |x2 - x3| + |y2 - y3| but f(k) = |x2 - x3(k)| + |y2 - y3(k)| - k, and I propose that f is non increasing, since |x2 - x3(k)| + |y2 - y3(k)| will increase no more than by 1 when increasing k by 1. So f(k + 1) ≤ f(k) and we searching minimal k that f(k) ≤ 0.
•  » » » 3 years ago, # ^ |   0 Ok I understood but whats your function for calculating x3(k) and y3(k)?
 » 3 years ago, # |   +2 I am confused by problem C statement and its editorial. It says we can move in the direction we want and transpoisition add up, but that fact is not used in editorial. What does it mean by if we dont move then only direction of wind counts? Also if all characters of wind direction are same then how can we reach all blocks within Manhattan distance?
•  » » 3 years ago, # ^ | ← Rev. 2 →   +12 Let's consider the wind move is wind_mv and the ship move is ship_mv. After k step the ship will move to (wind_mv_1 + ship_mv_1) + (wind_mv_2 + ship_mv_2) + ... + (wind_mv_k + ship_mv_k) which is equivalent to (wind_mv_1 + wind_mv_2 + ... + wind_mv_k) + (ship_mv_1 + ship_mv_2 + ... + ship_mv_k) Therefore, the ship is moved by the wind to (wind_mv_1 + wind_mv_2 + ... + wind_mv_k), and it can reach to any points that is within k moves from this point (wind_mv_1 + wind_mv_2 + ... + wind_mv_k).
•  » » » 3 years ago, # ^ |   0 Great explanation. @mttb90
 » 3 years ago, # |   +2 unable to understand how binary search is apply in problem C?
•  » » 3 years ago, # ^ |   0 generally binary search is applied when you want to find the minimum or maximum of something in a problem, and for this to work you need to make a function called "bool can(int x)" which gets x and tells you if in x days this ship either is on (x2,y2) or not, you should understand that the moment the ship arrives at the destination it can stay there because if the wind is Up then the ship can go down..... so basically the function has a 'x' which all the days smaller than x are false and all the days greater than x are true, so this is a basic binary search problem in which you want to find the minimum x.And in function "can(int x)" you can check after x days what position the ship will be in if it starts from (x1,y1) and how far it still is from (x2,y2) if using x more moves you could reach your destination then "can" should return true else false, I hope this helps :)
•  » » » 2 years ago, # ^ |   0 well explained
 » 3 years ago, # |   0 In the author's solution for E, how can we assure that using 3 strings with the mentioned approach will lead to finding the solution?
•  » » 3 years ago, # ^ | ← Rev. 2 →   0 Think when only n = 26, what's approach do you follow? Just sent dustinct characters. Here n> 26, so we sent n distinct pairs. 262 is not good enough to sent 10^4 distinct pairs. Think again.
•  » » » 3 years ago, # ^ |   0 What do you mean by n distinct pairs? We can send same character multiple times in a string unless the entire triplet for the 3 strings don't match. This is what author means. Please explain if you understand.
 » 3 years ago, # | ← Rev. 2 →   0 Hi, I am sorry I cannot understand the solution to problem E. HE has created new functions fl and fr. Can anyone please explain how has he defined new functions?Is there any reference material available for the same?Thanks
 » 3 years ago, # |   0 div 2 C test 5 ?
•  » » 3 years ago, # ^ |   0 Yes.This problem http://codeforces.com/contest/1117/problem/G. Thanks
 » 3 years ago, # |   0 Hi, can someone explain why this solution written in Go failed on 10th test? I already almost copied C++ solution, but it didn't pass anyway. I think that I missed some underlying Go mechanics, but don't even know there to start debugging.
•  » » 3 years ago, # ^ |   0 For anyone wondering: there is byte overflow on 50th line. (122 — 97) * 26 > 256
 » 3 years ago, # |   0 Can someone please point the problem with MyCode
 » 3 years ago, # |   0 The solution to G seems very vague . Can someone give a simpler explanation or at least an insight?
 » 3 years ago, # | ← Rev. 3 →   0 For D what's wrong with this submission: 50816541 ? It gives the good answer for all the 37 Codeforces test cases when I run it from my computer but gives WA1 when I run it here.Up1: It also runs well at Ideone. For example, for the last codeforces' test case it gives good answer as can be seen here: IdeoneUp2: I just change the matrix representation from pointers to vector> and it passed as can be seen here: 50882908 . But I'm still curious to know why first code didn't pass. There is some issue in compiling here that I'm not getting?
 » 3 years ago, # |   0 Problem C : I am not able to understand how the upper limit for binary search. Can anyone please clarify on that ?
 » 3 years ago, # | ← Rev. 2 →   0 deleted
 » 2 years ago, # |   0 Why Problem G give 256mb memory limitation but million input. Is it meaning we can only solve it with such array implementated data structure. I see only one java solution passed test which consumed 240+mb memory, it was nearly failed.
 » 2 years ago, # |   0 Hi awoo, this link doesn't appear when you look at the contest or any of the problems in it. It would be useful for people using it in the future if you could attach it to the contest :)
 » 2 years ago, # |   0 can someone please explain problem D
 » 21 month(s) ago, # |   0 We can also solve Problem E by the following way:First, we send a string $s1= abcdefgh...xyzabcd...xyz..$ of length $n$ and accept a string $t1$ from the judge.For each position $j$, we get a character, which could have originated from at-most $n/26$ positions. For each j, we fill all positions cyclically in the same manner as we constructed $s1$ and send string $s2$. Then we accept string $t2$.Now, similarly, we have narrowed down the possibilites to $n/26^2$. We repeat this process again and get string $s3$, and finally $s$.
 » 19 months ago, # |   0 Divide and conquer approach for D: Divide input N into two roughly equal halves L and R (L + R = N). Compute the answer recursively on L and R and multiply the numbers together. This accounts for ways of configuring where you don't have any splits that "cross the middle." For configurations that "cross the middle" (only happens when M >= 2) and you iterate over how many from the middle split are in the left half, say i (1 <= i <= M-1). Then there are j = M-i in the right half. For each split, compute recursively on (L-i) and (R-j) and add their product to the sum.https://codeforces.com/contest/1117/submission/95704249
 » 18 months ago, # |   0 can anyone explain B in simple words ?
 » 10 months ago, # |   0 Could anyone please tell me how cycle length is k+1 in problem B?
 » 10 months ago, # |   0 I'm having trouble understanding matrix exponentiation transitions in the provided code of problem D. Could somebody explain why we take bin[0][0], bin[0][m — 1] and bin[i][i — 1] as transitions? Shouldn't it be bin[0][0], bin[0][m — 1] and bin[i][i + 1]? (I've watched Errichto's video on matrix exponentiation and perhaps I didn't get the idea right)Also why do we sum up fin[0][i], for each i as a result? I'm new to matrix exponentiation and would appreciate the help, thanks.
 » 10 months ago, # |   0 what the approach of problem B?
 » 3 weeks ago, # |   0 Problem — D: Link — This blog is quite helpful for the ones who are stuck on this dp[n] = dp[n-1]+dp[n-m] equation!
•  » » 3 weeks ago, # ^ |   0 Thank you!