Vovuh's blog

By Vovuh, history, 4 months ago, In English,

1183A - Nearest Interesting Number

Idea: MikeMirzayanov

Tutorial
Solution

1183B - Equalize Prices

Idea: MikeMirzayanov

Tutorial
Solution

1183C - Computer Game

Idea: MikeMirzayanov and Vovuh and BledDest

Tutorial
Solution

1183D - Candy Box (easy version)

Idea: MikeMirzayanov

Tutorial
Solution

1183E - Subsequences (easy version)

Idea: MikeMirzayanov

Tutorial
Solution

1183F - Topforces Strikes Back

Idea: Vovuh

Tutorial
Solution

1183G - Candy Box (hard version)

Idea: MikeMirzayanov

Tutorial
Solution

1183H - Subsequences (hard version)

Idea: MikeMirzayanov

Tutorial
Solution
 
 
 
 
  • Vote: I like it
  • +64
  • Vote: I do not like it

»
4 months ago, # |
Rev. 2   Vote: I like it +9 Vote: I do not like it

Can somebody explain how we solve B with Binary Search?

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

      i don't understand the idea bs, could you explain

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

        See our search space is (maxElement-k to maxElement+k). Our answer lies between this interval. Now we binary search using the condition (minElement-mid)<=k) If our minElement satisfies this condition then all elements will surely do so we check for only minElement. and then we take max of all possible values.

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

    Set low = min(A) + k and high = max(A) + k, and use binary search to find out the result in this range.

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

For problem B, initially I wrote binary search solution for answer. But, it seems it is giving $$$-1$$$ for some valid case too.

Can someone please help me identify the mistake in my submission ?

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

    I wrote almost the same solution but I got WA on test 3 :(

    https://codeforces.com/contest/1183/submission/56084413

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

    Ok, I get it. Even if for a certain value, we cannot satisfy the conditions, It does not mean that the answer should be less than the value, the answer can be more than the current value too.

    For eg, Your array is 2 2 5 10 and let K be 4, the lower bound is 0 and the upper bound cannot be greater than (min+K). So you first check for (0+6)/2 = 3 (as mid). You see that condition is not satisfied as (10-3)>4, so you conclude that answer should be less than 3, which is not true, since the value 6 satisfies all the condtions.

    So, I think that Binary Searching the Answer cannot work for this problem.

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

    you should initialize lo to the least valid value, but you're initializing it wrong (0 isn't a valid answer). you should rather initialize to : max(a[n — 1] — k, 1) but only after checking if that value is indeed a valid candidate for an answer. (which is easy you can iterate through the array and check that) my solution

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

1183C - Компьютерная игра Can somebody explain why we need to do (-c+1)/(a-b) instead of (-c)/(a-b)? Thanks

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

    Because you have to add some differences to obtain the charge at least $$$1$$$. See the following example: $$$-c$$$ = 4, $$$diff = 2$$$. Then your formula will say $$$2$$$ and after two additions we will obtain $$$c=0$$$ and this is bad for us.

»
4 months ago, # |
  Vote: I like it +14 Vote: I do not like it

Vovuh's contests : Always fast and high quality

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

In problem H, I wonder if we don't get min of 10^12, what will happen then ?

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

    Theoretically, the number of subsequences of the string of length $$$n$$$ is $$$2^n$$$. This value is the upper bound but there can be strings that contain more than $$$2^{63}-1$$$ subsequences and if you not take the minimum with $$$10^{12}$$$ then your dynamic programming will overflow the 64-bit datatype. I takes $$$10^{12}$$$ because you never need more subsequences than $$$10^{12}$$$.

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

      Thanks

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

      what is the time complexity of solution of problem E ?

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

        $$$O(n^2k)$$$ since it's a BFS that visits $$$k$$$ vertices, and processing each vertex takes $$$O(n^2)$$$ time (generating/checking $$$n$$$ strings, taking $$$O(n)$$$ time each).

        (Edited to fix runtime, thanks Roach00.)

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

          but what about unnecesary generated subsequences apart from visiting k vertices and O(n) in deleting character from string for each vertex string

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

            You're right, it's $$$O(n^2 k)$$$ in the worst case where there are lots of duplicates and we only get one new string of each length.

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

              we could reduce the time complexity of search,insertion and deletion to log(n),if we use suitable data structure.but that could be hell of implementation.

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

          wc, Thanks to you too :)

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

      Can anyone explain how to come up with the upper_bound(1e12) for any given string?

      • »
        »
        »
        »
        7 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        it's in the problem. ~~~~ 1<= k <= 10e12 ~~~~

»
4 months ago, # |
  Vote: I like it +5 Vote: I do not like it

It's great to see these problems which have two versions! It helps me have a deeper understanding of the problem~

»
4 months ago, # |
  Vote: I like it +1 Vote: I do not like it

what is the mistake in my idea in problem D my code here

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

tnx for fast tutorial and good explanation but lots of questions was mikemirzayanov's

»
4 months ago, # |
  Vote: I like it +5 Vote: I do not like it

Really liked the trick you applied to F . Nice contest and especially problem F.

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

Hello, Could you help me with H (hard version)? It says wrong answer on test 43. I have no idea what is wrong with it. Thanks in advance. http://codeforces.com/contest/1183/submission/56165101

Edit: Anyways, I solved it after long hours of hard work.

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

I solved H with slightly different approach where dp[i][j]=#of subsequences of length j on prefix [1..i]. Let character on position i be A, then the transition looks like: dp[i][j]=dp[i-1][j]+dp[i-1][j-1]-dp[last[A]-1][j-1]. It is correct, however it may overflow sometimes. What I did was first adding dp[i][j]+=dp[i-1][j], and if now dp[i][j]>1e18 i skip the second part of addition. It passed the tests, but im almost sure that this is not correct. If we substract this value of dp somewhere later it is not preceise, so later dp states may be not exactly correct. So here come my questions: 1. Am i right that it is not correct? 2. Is this really hard to construct counter-test?

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

In problem C, why should we round up (-c+1/(a-b))? Why can't we use the value directly?

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

Can someone pls explain to me the solution of Problem H

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

Can anyone explains the 3. Computer tutorial formula for turns i.e. turns = ceil(-c+1/(diff)). How this formula is derived?

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

    Alright. Let the number of just play turns be $$$x$$$, and let the number of play and charge turns be $$$y$$$.

    The problem statement suggests that total number of turns should be $$$n$$$, hence $$$x+y=n$$$.

    It also suggests that the battery that these moves consume altogether should be strictly less than the initial charge. Just play moves consume $$$a*x$$$ amount of battery, whereas play and charge moves consume $$$b*y$$$ amount of battery, hence $$$a*x+b*y<k$$$.

    Substituting $$$y$$$ from equation 1, you can obtain an upper bound on $$$x$$$, and hence you'll arrive at the aforementioned equation.

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

      Thanks for your explaination. I got expression as y<(-c)/(a-b) as per your explaination above. But in code why are we taking ceil operation and adding 1 int he numerator. How its giving the correct answer

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

Can someone please explain why my submission won't work for Candy hard where submission id is https://codeforces.com/contest/1183/submission/56124846?

»
4 months ago, # |
  Vote: I like it +3 Vote: I do not like it

Can someone please explain the validity of the solution given here for F?

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

    My solution to F is: Sort the array (arr). Remove duplicate elements. Iterate for all i find maximum index j < i such that arr[i]%arr[j]!=0 and then find maximum index k < j such that arr[j]%arr[k]!=0 && arr[i]%arr[k]!=0, maximize the answer by arr[i]+arr[j]+arr[k].

    Let prove this solution by contradiction: Suppose, for index i there are indexs j0, k0 (k < j0 < k0 < i) can be selected such that arr[j0]+arr[k0] > arr[j]+arr[k] and i, j0, k0 satisfies all the conditions, if this is possible then our soluton is wrong. But if we look carefully this is not possible. j0 was not selected as k so we are sure arr[j]%arr[j0] = 0, same k0 was not selected as k because arr[j]%arr[k0] == 0, So arr[j0] and arr[k0] are divisors of arr[j] and obviously smaller than arr[j]. And we know than some smaller divisor of a number n is <= n/2 so arr[j0] + arr[k0] <= arr[j]. So above condition arr[j0]+arr[k0] > arr[j]+arr[k] is not possible.

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

someone explain where my solution fails for Problem C? MY Solution

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

    You are using ints, rather than longs. This overflows when you multiply n and b. I replaced your declarations of variables k, n, a, b with longs and it passes the first case you failed.

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

      thanks,I got it and I should check K<n*b too.

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

How to solve F by iterating over all possible triplets?

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

Can anyone help me with the problem C why are we adding diff to k (k + diff — 1) / diff

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

can someone explain the 2nd testcase of problem D i didnt quite understand how they took the maximum possible size thanks.

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

Vovuh

Content copied from my previous comment..(no one replied). {May be I am wrong}...

I tried D using max-heap but at the 19th case the response was TLE .. but as you guys have used sort() method [complexity will be nlogn]. So, as Mine...

https://codeforces.com/contest/1183/submission/56128383

any suggestion where am I wrong???

  • »
    »
    4 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    .. whether max heap is a good way to solve it ..
    
    pseudocode:
    
    
    heap=maxheap(contains the frequency of distinct nos)
    start=maxheap[0] //max value
    while(heap not empty):
          val=heap(remove max value from heap)
          if((val-start)>=0):
                  if(start>=0):
                        count=count+start
    		    start=start-1
    	      else:
    		    break
          else:
    	      if(start>=0):
    		   start=min(val,start)
    		   count=count+start
    		   start=start-1
    	      else:
    		   break
    
    So, for me its O(nlogn) ... n for removing the elements and log(n) for maintaining the heap...??? am I correct?? ...
    
    
»
4 months ago, # |
  Vote: I like it +5 Vote: I do not like it

For 1183F - Возвращение Topforces, I tried all of the possible options for 30 biggest unique elements and failed in system testing (56093272). After the contest, I changed 30 to 40 and got accepted (56189352). Can anybody come with a counter test for my solution? The main idea is if $$$x \neq y \land x | y \rightarrow x \times 2 \leq y$$$.

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

    This solution will fail at the test case where a lot of divisors of maximum should be omitted in greedy solution. So we can take a number $$$x$$$ with maximum number of divisors, omit one from $$$\{x/2, x/3, x/5\}$$$ and add two minimum numbers that are not divisors of $$$x$$$. This gives us a test with $$$n = 149$$$, where the least number is used in optimal answer.

    Test
    Generator

    Probably this or something similar should be added to this problem testset, Vovuh

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

      Thank you, I will add this test now!

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

Can someone explain me the Problem D ?

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

    In this problem, you are provided with total different types of candies (n)

    and then the type of a particular candy in a series

    for eg- 8 1 4 8 4 5 6 3 8

    8 is the total number of different types of candies

    and the sequence in which they are present is

    firstly a candy of type 1 then a candy of type 4 then a candy of type 8 again a candy of type 4

    total number of candy of type 1 is = 1 total number of candy of type 2 is = 0 total number of candy of type 3 is = 1 total number of candy of type 1 is = 1 total number of candy of type 4 is = 2 total number of candy of type 5 is = 1 and soon

    You have to find out the gift

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

    Have a look at this, https://codeforces.com/contest/1183/submission/56165836 In this we try to first see the frequencies of all candies, and sort then in descending order. We all candies with highest frequency(say f for now) to our gift and the next optimal step would be to add f-1 candies of the next candy type(if available, else add all candies if its freq is less than f-1). We always try to add the number next candy type, based on how many candies of the previous type we added. Optimally we always try to add (prev_added_candies — 1) candies if available, int current step.

»
4 months ago, # |
  Vote: I like it +3 Vote: I do not like it

can anyone please explain tutorial for H. I don't understand how does it work.

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

    I too am unable to understand the very first few lines.

    "Initially all values are zeros except $$$dp_{i,1}=1$$$ for each $$$i$$$ from $$$0$$$ to $$$n−1$$$."

    Why is this the case? Won't it count two different occurrences of the same character as two instead of one ( we need distinct subsequences right? )

    UPD: (Adding example)

    If the given string is "abca", then $$$dp[0][0]=1$$$ and $$$dp[3][0]=1$$$, but I thought we must count it as 1 times "a" is there not twice. How is that being taken care of?

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

      You will not consider the first 'a' when you'll calculate the number of subsequences of length one. The same with other lengths, two dynamic programming states $$$dp_{i_1, j}$$$ and $$$dp_{i_2, j}$$$ can be non-zero and $$$s_{i_1} = s_{i_2}$$$ but you will not consider the first state in the answer.

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

        Oh okay. So correct me if I am wrong, we are making dp such that each subsequence $$$t$$$ that ends with say some character $$$X$$$, will be counted at the maximum position $$$i$$$ such that $$$i \ge len(t)$$$ and $$$s[i]$$$ is $$$X$$$. ( $$$s$$$ is original string ).

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

          Yes, this is true.

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

            Thanks a lot! Finally understood and implemented.

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

              Hey, can u please explain the solution in a detailed manner, I am not able to understand why are we doing the steps mentioned. Thanks in advance!

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

                Maybe you should try to explain what exactly you did not understand, instead of asking someone to tell you the whole thing, especially when there is editorial where someone has tried to do what you exactly want.

                Now, I will try to explain a simple example to you, to make sense of the dp ( not go through the whole process ). Maybe that will clear everything.

                Consider given string $$$s = aba$$$. I will just tell you how to understand the $$$dp$$$.

                Firstly, let's note ourselves, what are the subsequences. We have

                {$$$aba,ba,aa,ab,a,b$$$}

                Out of these,

                1. $$$a$$$ is counted at index $$$0$$$ and $$$2$$$,
                2. $$$b$$$ is counted at index $$$1$$$,
                3. $$$ab$$$ is counted at index $$$1$$$,
                4. $$$aa$$$ is counted at index $$$2$$$,
                5. $$$ba$$$ is counted at index $$$2$$$,
                6. $$$aba$$$ is counted at index $$$2$$$.

                Since $$$a$$$ is counted twice, I was confused, but finally while calculating total counts, we are only considering $$$last[n-1][ch]$$$ for each character $$$ch$$$ = [ $$$a$$$ to $$$z$$$ ], thus in case of $$$a$$$ only the one at index $$$2$$$ is counted at the end. The one at index $$$0$$$, however, is also important, because only using it you can count the strings $$$aba$$$ or $$$aa$$$.

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

Problem B : Equalize Prices

i have just checked if (min+k >= max-k) then print min+k ; else print -1; still wrong answer on test case 3.Can somebody please explain.

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

thanks for all your help ....., now deleting this comment

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

    teshar roy on youtue for videos hacker earth for graphs and math and all the basics expect for dynamic programming learn it from geeks for geeks any one who did it did it alone so cheer up and go solve a lot of problems and participate in lots of contests that'a the most important thing hope to do well enjoy .

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

Simple solution for H:

Let $$$dp[n][k]$$$ be the number of distinct subsequences of length $$$k$$$ using only the first $$$n$$$ characters of $$$s$$$. Then

dp[n][k] = dp[n-1][k] + dp[n-1][k-1] - dp[l-1][k-1]

where $$$l = lst_{n-1,s[n]}$$$.

Now greedily add substrings to $$$S$$$. Note that two subsequences of different length must be different. (We first use the $$$dp[n][n]$$$ subsequences, then $$$dp[n][n-1]$$$, etc)

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

Can someone explain the Dynamic Programming approach for Problem E — Subsequences (easy version).

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

I solved problem D with O(N) solution. I like when you guys hide the solution of the problems in the way you did in this tutorial. So when i go to read the solution of the problem A i don't end up reading the solution of problem B and a lot of times i don't want to do it.

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

56141485 Can someone please tell what am I doing wrong. When I run the code on my system with the test case : 1 10 100 100 100 100 100 100 100 100 100 100

I get the answer as 10, but on running in custom invocation answer comes out as 27.

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

Vovuh Why do n/2, n/3, n/5 never divide each other completely? Can this be proved? Thanks :)

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

    cause n / 2 has more 3 factors than n / 2 and n / 3 has more 2 factors than n / 2. :)

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

In problem F editorial Let's imagine that the maximum element is divisible by 2, 3 and 5 and there are three following numbers in the array: maximum divided by 2, by 3 and by 5. Then their sum is greater than the maximum (and may be greater than the answer we have!) because 12+13+15>1. can someone give an example of such case... Thanks in advance

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

    Consider one of test cases in problem sample:

    4
    30 15 10 6
    

    Obviously, $$$n < n/2 + n/3 + n/5$$$ is $$$30 < 15 + 10 + 6$$$ here

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

Can anybody explain the complexity for Problem F (brute force) solution ,it would be really helpful?

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

For problem F,https://codeforces.com/contest/1183/submission/56308081
My submission is just a N^2 complexity solution, why can I get it passed?

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

For Problem D, Candy Box(easy version) can someone help me identify the error in my code?

https://codeforces.com/contest/1183/submission/56358474

Thank you!

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

1183(c) Can anyone tell me why its not working??

include<stdio.h>

int main() { long n,k,a,b,q,c,turns,diff; scanf("%ld",&q); while(q--) { scanf("%ld%ld%ld%ld",&k,&n,&a,&b); c=k-(a*n); diff=a-b; if(c>0) printf("%ld\n",n); else { turns=(-c+1)/diff; if(turns>n) printf("-1\n"); else printf("%ld\n",n-turns); } } }

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

pls explain why in C

16 7 5 2 is giving ans=0
nd
15 5 4 3 is giving ans=-1
also
20 5 7 3 is giving 1 instead of 2??pls reply
»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

In the problem C, i am not able to get where in the problem statement its written that when vola plays the turn "play and charge", the charge of the battery increases by diff=a-b. It's written in the editorial as well. Kindly help me with the problem statement of the problem. Thanks in advance

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

Could anyone tell me why am I getting a wrong answer with problem D on the 3rd test?

https://codeforces.com/contest/1183/submission/56678366

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

Could anybody explain me please, why I have TLE on test 3 in problem F ? https://codeforces.com/contest/1183/submission/56682439 I have no ideas :( I use C#.

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

For all those who can't understand solution for H can try this problem first https://www.spoj.com/problems/DSUBSEQ/

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

What is the time complexity for solution of E?? .... they used set count which is O(n) (even more with strings)...but using map is slower than this !!

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

Can someone explain why Problem H solution works ? I don't understand why we are constructing these matrices and what is the logic to using them.

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

For Question A. Max step should be 7. Number = 997 steps taken will be 7 But the editorial says it's 5.

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

Thanks for these Good Editorial !