PikMike's blog

By PikMike, history, 8 months ago, In English,

1117A - Best Subsegment

Tutorial
Solution (adedalic)

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
Solution (adedalic)
 
 
 
 
  • Vote: I like it
  • +54
  • Vote: I do not like it

»
8 months ago, # |
  Vote: I like it +82 Vote: I do not like it

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

  • »
    »
    8 months ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    such an impressive solution.

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      saeed_odak Can u please explain ur solution? I don't get why u are doing CRT?

      • »
        »
        »
        »
        8 months ago, # ^ |
          Vote: I like it +8 Vote: I do not like it

        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.

        • »
          »
          »
          »
          »
          8 months ago, # ^ |
          Rev. 2   Vote: I like it +8 Vote: I do not like it

          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 :)

          • »
            »
            »
            »
            »
            »
            8 months ago, # ^ |
            Rev. 2   Vote: I like it +16 Vote: I do not like it

            Short answer:

            26, 25, 23 are coprime, so there are 26 * 25 * 23 = 129950 possible remainder combinations. 129950 > 10^4, so it's enough.

            Long answer:

            Let's forget about strings for a moment. Say you have two numbers 2 and 3. What are remainders of dividing numer 0 by those? It's 0 % 2 = 0 and 0 % 3 = 0. We can write it as pair (0, 0). Ok, let's do that for numbers from 0 to 5 and write results:

            (0, 0), (1, 1), (0, 2), (1, 0), (0, 1), (1, 2)

            You may have already noticed that we have something nice here — all pairs are unique. Why? I will skip this, take this for granted now. If you are interested read more about CRT and ask me if you don't understand, but I think talking about this will diverge too much from the actual problem. What do that gives us? If somebody gives you only those remainders you will be able to determine a numer between 0 and 5 that gives those reminders.

            Now it's all about asking string questions in such way that we will get this representation of a numer in remainders. Let's say that n = 5. Your question may look like this:

            ababab (we have a cycle of length 2)

            abcabc (we have a cycle of length 3)

            Ok, so now let's look what you may get at position 0 from first qeustion. If you get an 'a' it had to came from either position 0, 2, or 4. All of those give 0 modulo 2. If you get 'b' then it's either from 1, 3 or 5 — so it's 1 modulo 2. Similar case for the second question, but now you get "second part" of the represantion — that is remainder modulo 3. Let's assume that at position 0 you got 'b' and 'c' respectively. That is (1, 2) in our representation. And that is equal to 5. So the letter had to come from position 5.

            You can think of it as successively narrowing set of possible position where the letter came from. In the beginning you know nothing — i.e. possible "sources" of letter are positions {0, 1, 2, 3, 4, 5}. After first question for position 0 you get 'b' so now set becomes {1, 3, 5}. And now, after second question you can say it's 5.

            Now to be able to cover strings of length 10^4 you need to pick numbers bigger than just 2 and 3 to be able to identify wheter someting came from position 0 or position 6. If you only ask for remainder modulo 2 and 3 you will get (0, 0) for both 0 and 6 :( But, thankfully 26, 25 and 23 are enough. 26 has, well, 26 possible reaminders, 25 has 25, 23 has 23 so you get 26 * 25 * 23 = 129950 possible triplets. So you can determine numbers from 0 to 129949.

            What's also important here is that all those numbers are coprime. Had we used 2 and 4 instead of 2 and 3 we would get:

            (0, 0), (1, 1), (0, 2), (1, 3), (0, 0)

            So we have our first repetition not after 2 * 4 = 8, but actually after lcm(2, 4) = 4 numbers.

            • »
              »
              »
              »
              »
              »
              »
              8 months ago, # ^ |
                Vote: I like it +8 Vote: I do not like it

              Wow, Thanks a lot.

              Unfortunate it is for a user can only give a single upvote.

              • »
                »
                »
                »
                »
                »
                »
                »
                8 months ago, # ^ |
                Rev. 2   Vote: I like it +3 Vote: I do not like it

                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 :)

          • »
            »
            »
            »
            »
            »
            8 months ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            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.

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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 ?

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      "Is it because the lcm should be greater than size of the string so that we have unique solutions ?"

      yes, exactly.

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone help me with this solution for Problem D? It gives TLE, although I think the complexity should be O(m3 * logn).

  • »
    »
    8 months ago, # ^ |
      Vote: I like it +4 Vote: I do not like it

    Dont use unnecessary modulo operations.

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks a lot!

      I just noticed that too. Here is the solution that passed.

      • »
        »
        »
        »
        8 months ago, # ^ |
          Vote: I like it +5 Vote: I do not like it

        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. 50233106

        That's because mod is much more slower than minus.

        sorry for my poor English...

        • »
          »
          »
          »
          »
          8 months ago, # ^ |
            Vote: I like it +1 Vote: I do not like it

          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.

          1. Used c+=a*b instead of c+=mult(a,b), this removes 3 modulo operations
          2. Changed c%=mod to if (c>=mod*mod) c -= mod*mod, removing one more mod
          3. Changed the order of the for loops, see here.
          • »
            »
            »
            »
            »
            »
            8 months ago, # ^ |
              Vote: I like it +8 Vote: I do not like it

            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.

»
8 months ago, # |
Rev. 2   Vote: I like it -8 Vote: I do not like it

About problem D,why we just split the last gem but not the other?Can anyone explain?

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    If you want to split front gems , it has been done when calculating front dp values .

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Yes,because the last gem has only two states,so it can be classified according to this. Thanks a lot.

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone explain solution for D in more details? How does matrix exponentiation become solution?

»
8 months ago, # |
  Vote: I like it +40 Vote: I do not like it

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.

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    For me it just provided a slow down. 1466ms 1637ms Why did this happen?

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it +11 Vote: I do not like it

      Great question, this is the first time I've seen a counter example. The reason why it is different for you seems to be because of the way you do the multiplication and addition. You are effectively using a += b*c%mod; if (a>=mod) a-=mod;, but if you change it to a += b*c; a%=mod; it runs quicker and the changed order runs significantly quicker.

      Order (i,j,k) with your mult and add: 1466 ms

      Order (i,k,j) with your mult and add: 1606 ms

      Order (i,j,k) with new mult and add: 1404 ms

      Order (i,k,j) with new mult and add: 780 ms

      I have no clue why (i,k,j) with your mult and add is so slow (or rather why the other mult add is so much quicker). Maybe someone else knows why this is. There is probably more to this than just cache misses.

      Note: One important thing. A 100*100 matrix is pretty small, much of it will probably fit in the L1 cache and all of it in L2. So the real gain will be much more apparent for larger matrix sizes. Take for example 230*230 with your mult/add, that takes around 9.5 s for (i,j,k) and 8.9 s for (i,k,j). Or take this improved version, for 300*300 (i,j,k) takes 5.9 s and (i,k,j) takes 4.5 s.

      • »
        »
        »
        »
        8 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Thank you, this is an interesting topic. Do you know any other source on internet about this?

      • »
        »
        »
        »
        8 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Thank you !

        I love this .

        • »
          »
          »
          »
          »
          8 months ago, # ^ |
          Rev. 2   Vote: I like it 0 Vote: I do not like it

          Glad you liked it

          I'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.

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

If I learned matrix exponentiation!!! got stuck after finding dp[n] = dp[n — 1] + dp[n — m]

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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.

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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.

  • »
    »
    8 months ago, # ^ |
    Rev. 2   Vote: I like it +12 Vote: I do not like it

    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.

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Ok I understood but whats your function for calculating x3(k) and y3(k)?

»
8 months ago, # |
  Vote: I like it +2 Vote: I do not like it

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?

  • »
    »
    8 months ago, # ^ |
    Rev. 2   Vote: I like it +12 Vote: I do not like it

    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).

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Great explanation. @mttb90

»
8 months ago, # |
  Vote: I like it +2 Vote: I do not like it

unable to understand how binary search is apply in problem C?

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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 :)

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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?

  • »
    »
    8 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    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.

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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.

»
8 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

div 2 C test 5 ?

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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.

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    For anyone wondering: there is byte overflow on 50th line. (122 — 97) * 26 > 256

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone please point the problem with MyCode

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

The solution to G seems very vague . Can someone give a simpler explanation or at least an insight?

»
8 months ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

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: Ideone

Up2: I just change the matrix representation from pointers to vector<vector<long long>> 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?

»
7 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Problem C : I am not able to understand how the upper limit for binary search. Can anyone please clarify on that ?

»
6 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it
deleted