Блог пользователя FelixArg

Автор FelixArg, история, 22 месяца назад, По-русски

Спасибо за участие! Надеюсь задачи вам понравились.

Один из тестировщиков (redpanda) сделал прекрасный видео разбор задач A-C, очень рекомендую посмотреть.

1982A - Футбол

Разбор
Решение (74TrAkToR)

1982B - Гипотеза Коллатца

Разбор
Решение (FelixArg)

1982C - Скучный день

Подсказка 1
Подсказка 2
Разбор
Решение 1 (FelixArg)
Решение 2 (74TrAkToR)

1982D - Красота гор

Подсказкa 1
Подсказкa 2
Разбор
Решение (FelixArg)

1982E - Количество k-хороших подотрезков

Подсказкa
Разбор
Решение (FelixArg)

1982F - И снова задача о сортировке

Подсказкa
Разбор
Решение (FelixArg)
  • Проголосовать: нравится
  • +113
  • Проголосовать: не нравится

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится -75 Проголосовать: не нравится

First. good problems I think.

»
22 месяца назад, скрыть # |
Rev. 3  
Проголосовать: нравится -14 Проголосовать: не нравится

...

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится +2 Проголосовать: не нравится

C. Most of the people have done this using dp. I didn't get the dp logic, but I tried it another way. For each position i, check the nearest position(let's say idx) on the right side which gives sum>=l (this can be done with lower bound and prefix sum, and also check if sum<=r). So you will have several i,idx intervals. Put them in a vector. Now all you need to do is find maximum non overlapping intervals, which is a standard leetcode problem.

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Well done! Very fun contest and good problem set.

»
22 месяца назад, скрыть # |
Rev. 2  
Проголосовать: нравится +28 Проголосовать: не нравится

Why would you set $$$n=5\cdot 10^5$$$ to a $$$O(n\log n)$$$ data structure problem?

»
22 месяца назад, скрыть # |
Rev. 2  
Проголосовать: нравится -17 Проголосовать: не нравится

Great mathforces round! + fast editorial

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone explain (?) me, In B,

Why k%(y-1) and not k%y ?

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

Under what category does problems like 1982B - Collatz Conjecture come under?

I always fail at making proper observations to such problems.

Can anyone please suggest me a list of such past problems?

Btw, I solved 1982C - Boring Day using sliding window technique. Code is quite simple and easily understandable.

Code
»
22 месяца назад, скрыть # |
Rev. 8  
Проголосовать: нравится +35 Проголосовать: не нравится

In problem D, the generalized version of Bézout's identity is used. If you haven't already heard about Bézout's identity, it states that the equation $$$\begin{equation} \ ax + by = d \ \end{equation}$$$ has integer solutions if and only if $$$\displaystyle gcd(a,b)\mid d$$$.

There are two ways to prove this: you can either use the extended Euclidean algorithm, or prove it via an existence proof that can be extended for $$$n$$$ variables instead of just $$$2$$$ (see the generalized version's statement here)

Here's the proof for three variables (heavily inspired by the proof for two variables from MONT) which can be easily extended for an arbitrary number of variables:

Let $$$S = { ax + by + cz \mid a, b, c \in \mathbb{Z} }$$$. Consider the smallest positive element $$$d \in S$$$, $$$d=ax_0+by_0+cz_0$$$. We want to show that $$$d=gcd(a,b,c)$$$. It is sufficient to show that $$$d\mid gcd(a,b,c)$$$ and $$$gcd(a,b,c) \mid d$$$. The second part is true since all of $$$\displaystyle \frac{a}{gcd(a,b,c)}, \frac{b}{gcd(a,b,c)}, \frac{c}{gcd(a,b,c)} \in \mathbb{Z}$$$. For the first part, we need to show that $$$d \mid a,b,c$$$.

Let us do the following for each of $$$(a,b,c)$$$ (I will only do it for $$$a$$$): Assume that $$$d \nmid a$$$. Then $$$a=qd+r, 0 \lt r \lt d$$$, and $$$r=a-qd$$$. $$$a$$$ is a linear combination of $$$(a,b,c)$$$ and so is $$$d$$$, which implies that $$$a-qd$$$ is too. Thus $$$r \in S$$$, and $$$r \gt 0,r \lt d$$$. This contradicts our assumption that $$$d$$$ is the smallest positive element in the set. So $$$d \mid a$$$. Repeating a similar process for $$$b,c$$$ shows that $$$d \mid b, d \mid c$$$. Combining these, we get $$$d\mid gcd(a,b,c)$$$.

$$$\blacksquare$$$

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Question C : Detailed Video Editorial https://youtu.be/zAMyp0dXCKk

»
22 месяца назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Problem C : Detailed Video Explanation (Greedy + Two Pointer) https://youtu.be/zAMyp0dXCKk

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

D is using something called Diophantine equation which I am very much unaware of
From where should I learn these things and is it as scary as it sounds like

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can anyone tell me the test case where my code fails in prblm 3??

267372328

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

didn't aware the fact that gcd of set of integer S divides x iff x $$$\in$$$ span(S) 😭 cost me problem D

how people solve this without knowing this fact?

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I have been trying problem D for quite a while now, I am using a 2D sliding window approach to calculate the number of "ones" that each submatrix would have. I observed that the difference changes by (k*k — 2*numberOfOnes) for each submatrix and can be either a positive or a negative change.

However I do not understand where I am going wrong. Here is my submission https://mirror.codeforces.com/contest/1982/submission/267424514

Can someone please help point out the mistake or even a testcase where my solution would fail.

  • »
    »
    21 месяц назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    If I read your code correctly, it seems like you are translating the problem into the coin change problem. i.e. you have coins of denomination $$$a_1, a_2,\ldots$$$, check if there is a way to produce change of value $$$x$$$.

    Firstly the translation is not right. For example if you have $$$x=1$$$ and coin values $$$\{2, 3\}$$$ the coin change problem says no, but the answer should be YES as you can go from $$$1\Rightarrow -2 \Rightarrow 0$$$.

    But even if the translation is correct it's well known such greedy method as you use will not work for all kinds of coin systems, it only works for a subset of coin system called "canonical" (I do not know much about it but it seems interesting). Here's an example that is not canonical (fails greedy): $$$a=\{3, 5\}, x=8$$$. So greedily adding $$$3$$$ gets you to $$$6$$$ which fails, but adding $$$3$$$ once then adding to $$$5$$$ gives $$$8$$$.

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I did not understand the D solution with GCD. I came out with the solution of finding the difference of ones and zeros for each submatrix using prefix sums (not fast enough to code it in the contest time though).

What I thought is that if I have d1, d2, d3... dn, and I can find c1d1 + c2d2 + ... + cndn that is equal to D, than I can satisfy the conditions.

Anyone can give some link to the proof of the last equation?

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone explain why my submission for problem D (267400465) gives runtime error? It works correctly on my local machine.

  • »
    »
    22 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    It does not work on cf but works on your machine due to undefined behaviour, in the end you have done *(s.begin()) assuming there are elements in s but that may not be true, in this case it's undefined behaviour as far as I am aware since whatever value is there may be picked up or a seg fault could occur. In your particular case it could have randomly turned up correct on your machine but on cf it could have given a seg fault or the random value could have been 0 giving a mod by 0 error.

»
22 месяца назад, скрыть # |
Rev. 2  
Проголосовать: нравится -19 Проголосовать: не нравится

I am sorry, i got my mistake

  • »
    »
    22 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится +2 Проголосовать: не нравится

    No the error is indeed correct. You have added an if statement in your loop with a break the lack of which made the error happen in the first place and is the reason your after contest submission is correct while during contest is not.

»
22 месяца назад, скрыть # |
Rev. 4  
Проголосовать: нравится 0 Проголосовать: не нравится

After the contest I have seen the GCD approach to solve problem D. However I don't understand what's wrong with the following approach:

I have used the following to check whether or not it will be possible to make the difference zero.

vector<int>v;
for(auto &it: reduceBy) {
    v.push_back(it);
}
while(diff > 0 && !v.empty()) {
    if(v.back() != 0) diff = diff % v.back();
    v.pop_back();
}
if(diff == 0) cout << "YES" << endl;
else cout << "NO" << endl;

here the reduceBy is a set which contains the different impacts we can have on the difference. Hence v is a sorted vector in increasing order.

This however gives me a wrong answer and I don't understand why.

  • »
    »
    22 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    v = [3,2] diff = 5 5%2 = 1 1%3 = 1 but it is possible

    • »
      »
      »
      22 месяца назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      I apologize I forgot to mention that reduceBy is sorted in increasing order and as we are traversing from the last index, it would pass as v = [2,3] diff = 5%3 = 2 2%2 = 0

      I’ll update the original comment that reduceBy is a set and v would be sorted in ascending order.

      • »
        »
        »
        »
        22 месяца назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        well increase v to be [2,3,4] and diff=5. Now it does not pass.

        What you are doing is greedily trying to reach 0, however in this problem the greedy approach is not valid since firstly you are not accounting for negative values of c: For example let v=[4,7] and diff=3 you would assume this is impossible but you can do 3+4-7 to reach 0

        Secondly it isn't necessary that the biggest number will indeed be reduced from diff to reach 0, think coin dp problem.

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In E there exists a solution with time complexity $$$O(T\log^2 n)$$$,using only DP. The main idea is trying to find every starting point of consecutive ones ($$$a_x=1$$$ if x is valid). You can see the implementation here.

»
22 месяца назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone please let me know me where my submission to problem C might be failing ?

267403795

edit: I got it..

»
22 месяца назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Hi everyone!

Can someone please point out why my submission 267373128 might be failing on the 475th test case of Test 2 of problem C. The following is the approach:

  1. I used a greedy approach for the problem. I declared two variables named sum and cnt (both initially set to 0).
  2. Then I started iterating over the array and check if sum + a[i] < l, then increased sum by a[i] and moved on to the next element.
  3. Else if sum + a[i] >=l && sum + a[i]<=r, that means this is the case when Egor would win. So I incremented cnt, set sum = 0 and move onto the next element.
  4. Else, which means sum + a[i] > r, then I have used a nested if else. If a[i] > r, then Egor cannot win if this element is included, so I set sum= 0 and moved onto the next element. Else if a[i]<=r, set sum = 0, but this time did not move to the next element as this element can contribute to forming a sum which is in the range.

Any help will be appreciated!

  • »
    »
    22 месяца назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    In your 4th point, when a[i]<=r you can still win with that element if you move your left pointer to the right by some amount (possibly). That is to say, if I'm taking the sum from index 0 to index i and it's larger than r, I could take from index 1 to i and that could maybe work.

    Edit: a[i]<=r instead of >

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

in the first problem, the score if x1 > y1 and x2 > y2 and y2 > x1 in that case there is a possiblity that at some point the yt would have been equal to xt ??

  • »
    »
    22 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    if x1 > y1 and x2 > y2 you could always increment x all the way and than y , remember we have to reach x2 and y2 eventually not that y2 is already there to hinder with x

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

hey , there are many telegram groups where solutions got leaked during contest please take some action. below is one groups where solutions and code was sharing during contest : https://t.me/codeforces_cp

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Ayo can someone help my code debug for Problem D. It says Wrong Answer on 4th Test Case. 242nd Token differ. 267451989

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can someone please give the 475th test case for problem C with n l and r?

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone please check what is wrong with this submission for D ?

267454158

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Could someone explain the editorial of E for me. It is still unclear for me, and what is the main point of the approach?

  • »
    »
    22 месяца назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится +16 Проголосовать: не нравится

    Note that "Bit sequence" can be generated by following infinite process:

    void step(vector<int>& a) {
        int sz = (int) a.size();
        for (int i = 0; i < sz; i++) {
            a.push_back(a[i] + 1);
        }
    }
    
    vector<int> a = {0};
    while (true) {
        step(a)
    }
    
    

    i.e $$$[0] \to [0, 1] \to [0, 1, 1, 2] \to [0, 1, 1, 2, 1, 2, 2, 3]$$$ and so on.

    It's not hard to see that it's actually a bit sequence: on the $$$i$$$-th step, we record all integers from $$$[2^i, 2^{i+1})$$$. These integers have one bit for $$$2^i$$$, and the rest of the bits correspond to the number of bits in $$$[0, 2^i)$$$, which is indeed the current sequence. Therefore, on the $$$i$$$-th step, we clone our sequence with an increment to the end of the sequence.

    From this point of view, the periodic nature of this sequence is clearly visible. Imagine $$$x$$$ is a maximum integer, such that $$$2^x \lt n$$$. Then we can split the sequence $$$0...n-1$$$ into $$$0..2^x-1$$$ and $$$2^x...n-1$$$. As we can see, the second part is also a prefix of the sequence but with a +1 increment. Therefore, if $$$x \gt k$$$, we can just solve for the two parts independently and then sum up the answers (since no good segment crosses $$$2^x-1$$$). And if $$$x \leq k$$$, the answer is simply $$$\frac{n(n+1)}{2}$$$, except for the case when $$$x = k$$$ and $$$n = 2^{x+1}$$$, then it is $$$\frac{n(n-1)}{2}$$$. With these formulas, you can write a simple memoization solution. It's not hard to see that it works fast because there are $$$O(\log^2 n)$$$ special states, where $$$n$$$ is a power of two, and we are always recalculating from a special state and a state that lowers $$$n$$$ at least twice.

    Code: 267468663

    • »
      »
      »
      22 месяца назад, скрыть # ^ |
      Rev. 2  
      Проголосовать: нравится 0 Проголосовать: не нравится

      Nice Approach. But could you explain why you did k — 1 for the second array that is from [2^x to n — 1] ?

      Like I get it that for the array [2^x to n — 1], there is an 2^xth extra bit and just this set bit is added to the sequence [0 to 2^x — 1] and it becomes the same sequence. But why k — 1 ? Should it not be k + 1 as we increased one bit to the original problem ?

      • »
        »
        »
        »
        22 месяца назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        Yes we increased one bit. Therefore condition turned to $$$bit(a_i)+1 \leq k \to bit(a_i) \leq k-1$$$.

        Like, after reduction to prefix we will actually have one bit more than on prefix. And in our world we want to count $$$\leq k$$$ therefore after reduction to prefix we need to count $$$\leq k-1$$$, so that with this extra bit it will be $$$\leq k$$$.

        • »
          »
          »
          »
          »
          22 месяца назад, скрыть # ^ |
          Rev. 2  
          Проголосовать: нравится 0 Проголосовать: не нравится

          Ohh, now got it. Thanks a lot.

          Also the TC will be o(log^3n) right ?

          • »
            »
            »
            »
            »
            »
            22 месяца назад, скрыть # ^ |
             
            Проголосовать: нравится 0 Проголосовать: не нравится

            It's $$$O(M \cdot (\log^2n + T\log n))$$$, where $$$M$$$ denotes $$$map$$$ runtime. But note that it can be easily rewritten without map to be clear $$$O(\log^2n + T\log n)$$$. Here $$$O(\log^2 n)$$$ to calculate answers for all cases where $$$n$$$ is a power of two, and after this single testcase will take $$$O(\log n)$$$. The upside of using a $$$map$$$ is that you don't need to think about those special states, you can just write all transitions as it is, and $$$map$$$ will optimize it for you, for the cost of $$$map$$$ runtime. In this problem constraints and TL are pretty generous luckily, therefore $$$map$$$ is safe to use.

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

This round helps a lot with my rating :)

The problem D is pretty interesting I think.

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

please explain how I can withdraw my earnings Nears?

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Just wondering, what's the point of having multiple test cases in F if $$$t$$$ is so small ($$$t \leq 10$$$) and there are only three tests (#1, #2, #3) where $$$t \gt 1$$$? Personally, I prefer problems with only one test case, as I don't have to care about re-initializing variables.

»
22 месяца назад, скрыть # |
Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

FelixArg , My solution for D shouldn't have passed the Final system test cases, but it did.

  • I knew the current difference between type1Sum, and type0Sum. ( let's call it RequiredDifference )

  • I knew for each of the sub-rectangle of size k*k, what are achievable differences. Lets call them (d1, d2, d3 ... dk).

I knew that I had to do a1 * d1 + a2 * d2 + a3 * d3 + ... ak * dk = RequiredDifference

I didn't know how that gcd ( d1 , d2 ... dk ) | requiredDiff .

But I knew, for ax + by = z , then gcd(x, y) | z. So I used this two variable equation property and passed test cases. Refer to my solution.

The test cases were weak for D. Ideally my solution shouldn't pass. Thanks to djm03178, it was later hacked.

Also, I honestly felt helpless during the contest because I didn't know this property gcd ( d1 , d2 , d3 ... dk ) | requiredDiff.

  • »
    »
    22 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится +11 Проголосовать: не нравится

    I wonder, does that solution have an actual counterexample? I'm stress testing a bunch of small cases but none are found yet. Anyways, to explain the TL hack, it looks something like this:

    $$$n=m=8$$$, $$$k=4$$$

    00000000
    00000000
    00000000
    00001111
    11110000
    11110000
    11110000
    11110000
    

    Expanding this pattern to $$$n=m=500$$$, $$$k=250$$$ makes 15281 distinct possible differences, so it's not feasible to perform gcd on every pair in 2 seconds. Since $$$k$$$ is even, the differences can only be even as well, so having an odd sum of $$$a_{ij}$$$ makes it impossible to find a possible way and therefore it can only realize that the answer is NO after running the whole loop.

    • »
      »
      »
      22 месяца назад, скрыть # ^ |
       
      Проголосовать: нравится +3 Проголосовать: не нравится

      I got a counterexample, a=42 b=70,c=30, pairwise gcd are 14, 10,6 leaving out say 8 but gcd of all is 2 so all even numbers work.

      • »
        »
        »
        »
        22 месяца назад, скрыть # ^ |
        Rev. 5  
        Проголосовать: нравится +3 Проголосовать: не нравится

        I mean, finding three numbers for the counterexample is easy, but making an actual test for that is another story, because of how hard it is to obtain only the desired values and not undesired ones with the 'windows'.

        I think I have a brief idea for this though. We can have large enough $$$k = n \lt m$$$, and control the difference with each column, so only each pair of columns of distance $$$k$$$ can affect the values and nothing else.

      • »
        »
        »
        »
        22 месяца назад, скрыть # ^ |
         
        Проголосовать: нравится +3 Проголосовать: не нравится

        Ohhh, I think it works but the other hack seems to be already doing the same thing. I didn't realize because the hacked solution has a slightly different approach, but I guess it essentially has the same condition.

        Here's the one I made:

        1
        14 16 14
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2
        0011110001111111
        0011100001111111
        0011100001111111
        0011100001111111
        0011100001111111
        0011100001111111
        0011100001111101
        0011100001111101
        0011100001111101
        0011100001111101
        0011100001111101
        0011100001111101
        0011100001111101
        0011100001111101
        
        • »
          »
          »
          »
          »
          22 месяца назад, скрыть # ^ |
           
          Проголосовать: нравится 0 Проголосовать: не нравится

          Damn I'll come back to this 6 months later when I am hopefully a little better equipped to learn this stuff, haven't dipped into hacking yet and coming up with test cases rather than numbers immediately feels much harder. Either way cool that you were able to come up with this.

»
22 месяца назад, скрыть # |
Rev. 6  
Проголосовать: нравится 0 Проголосовать: не нравится

Anyone Can please figure out which test case my code isn't passing on 3rd.Here is the submission id. 267473714

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In problem C my code 267502094 is showing TLE on 17th test case. My logic is simply to iterate till the segment sum is >= l. If its becomes greater than r then i'll just take nly the first card of that segment and chose to lose the round... starting the next round from the next card thus. Help me in finding my mistake.

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Hey, For problem C I was trying to write a dp solution in the contest but I was getting WA on test 2, eventually had to go without dp. Can anyone please help me in finding the error? I have wasted a lot of time finding a bug :(

Please help, my submission: 267511316

»
22 месяца назад, скрыть # |
Rev. 3  
Проголосовать: нравится +7 Проголосовать: не нравится

An alternative way to calcute the DP in E goes like this. P. S. the length of the text might seem daunting but it is because I tried to explain the logic behind every step.

Again, $$$dp(i, j)$$$ represents the number of subarrays for the first $$$2^i$$$ numbers if we can have at most $$$j$$$ bits. It is easy to notice that this formulation is equivalent to the number of subarrays formed by the numbers for which only the first $$$i$$$ bits can be non-zero. This might be useful for some for later intution. To make things a bit easier to write down, let's denote $$$g(x)$$$ as the number of subarrays contained in an array of length $$$x$$$, so $$$g(x) = \frac{x(x + 1)}{2}$$$.

If $$$j \geq i$$$, then the answer is simply $$$g(2^i)$$$, because no number smaller than $$$2^i$$$ has more than $$$i$$$ bits.

if $$$j=0$$$ then the answer is 1 because there is only one possible subarray (the one containing just a zero).

Now for the general case, $$$ 1 \leq j \lt i$$$, we need to notice something. The smallest number with more then $$$j$$$ bits will be one that looks like $$$1...1$$$ in binary, where there are a total of $$$j + 1$$$ bits (equal to $$$2^{j+1} - 1$$$). In this case all the subarrays formed by these numbers will be included in the dp. The number of these subarrays is $$$g(2^{j + 1} - 1)$$$. It is also important to note that these numbers form a sequence of continous good numbers which don't neighbour the other good numbers because of $$$2^{j+1} - 1$$$.

The number after $$$2^{j+1} - 1$$$ is $$$10..0$$$ or $$$2^{j + 1}$$$ where there are $$$j + 1$$$ zeros. We can notice that the number of subarrays formed by numbers where this lone bit is the highest one is just $$$dp(j + 1, j - 1)$$$, $$$j + 1$$$ because that is the number of zeros we have to work with and $$$j - 1$$$ because we need to account for the extra bit. In a similar fashion we include the numbers for which the $$$(j + 2)rd$$$ (0-indexed) bit is the highest one, the answer for which will be $$$dp(j + 2, j - 1)$$$. If we continue this pattern for every bit, we will have $$$dp(i, j) = g(2^{j + 1} - 1) + dp(j + 1, j - 1) + dp(j + 2, j - 1) + ... dp(i - 1, j - 1)$$$. The reason why we can sum up these states is because the sequence of numbers considered by each state are seperated by at least one bad number. E.g. $$$2^{j+1} - 1$$$ seperates the first sequence of numbers from the others. Similarly we can be sure that the numbers considered in the second term are seperated by the number $$$2^{j+2} - 1$$$ (the binary representation of this number is just $$$j + 1$$$ ones so this number is bad). For the third term the guaranteed bad number is $$$2^{j+3} - 1$$$, for the fourth its $$$2^{j+4} - 1$$$ and so on. This was also my intution for this dp. To somehow formulate the formula for dp such that all the terms are seperated by bad numbers. This dp yields a complexity of $$$O(log(n)^3)$$$ and it can be optimized to $$$O(log(n)^2)$$$ with prefix sums but it is not necessary to pass the TL.

You can use this DP to solve the rest of the problem in the same way as the authors did but my approach is a bit simpler in my opinion (though I think they are fundementally the same).

Step one: check if the smallest number with more than $$$k$$$ bits ($$$2^{k+1} - 1$$$) is greater than $$$n$$$, if that is true, then add add $$$g(n)$$$ to the answer and terminate.

Otherwise we go to the highest bit of $$$n$$$. Let's say that is the $$$b$$$-th bit. We add the subbarays not containing the bit to the answer, the number of which is $$$dp(b, k)$$$. Then, we reduce $$$k$$$ by 1 and $$$n$$$ by $$$2^b$$$ since every other solution will include the $$$b$$$-th bit. Now we go back to step one and repeat until $$$n$$$ is zero or code is terminated in step one. We can be sure that the numbers included in the subbarays for each step are not neighbouring because of similar reasoning to the DP. The biggest number considered in the range (which is just a sequence of $$$1$$$-s in binary), seperates them. If there aren't more than $$$k$$$ bits the code terminates at step one, so we can be sure that everything is nice and seperated.

Here is my code. code

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

In problem D, it is mentioned that, checking D mod gcd(d1,d2,…,dq)=0 is sufficient to test the existence of the solution for the equation c1⋅d1+c2⋅d2+⋯+cq⋅dq=D. Is it any proved theorem or intuition? Can any one explain this?

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Please someone explain what the below lines do in code of solution E?

if (s1 == e1 && s1 != 0){
	s_cur = (s1 + s2);
}
if (s2 == e2 && s2 != 0){
	e_cur = (e1 + e2);
}
»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Am i the only one who found F very easy for its position?

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone share the greedy solution of problem C

Or you can show the mistake of my solution

Link to submission

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For E problem - In the given test case 576460752303423268 59 (The last one)

int mod = 1e9+7; The answer is just (C(n, 2)%mod + n%mod)%mod But I think my code is doing something wrong while calculating C(n, 2)%mod

And the final answer that I'm getting is 777901708. If anyone has solved it, can they tell me if there is some catch or a different method to calculate this?

PS: I took help from stack overflow for getting the (nCr % p) value for big values of n.

Thank You

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Good round

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone tell me how to do problem E using technique meet in the middle?

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can somebody explain in 1st problem why there can't be a moment in 3rd testcase where the scores can't be equal at all mean reaching from (1,2) to (4,5) 1 can first go to 2 so there will be a moment right??

  • »
    »
    21 месяц назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    There can be a moment, you are right. The question is whether it's certain that there was such a moment. If there 100% was a moment, where the scores were equal, print NO, otherwise(like in 3rd case) if it isn't certain, print YES.

    • »
      »
      »
      21 месяц назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      okay okay so how does you make it certain ??

      I actually don't understand it still I can't understand why the editorial solutions works still

      • »
        »
        »
        »
        21 месяц назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        For it to be certain, one of the teams had to have made a comeback. For example, if the score is 1:2, and when he returns it's 3:2. There is only one possible scenario and that is for the first team to score a goal and make it 2:2, and then score another goal and make it 3:2. In that scenario(which is the only possible one), there was a time where the scores were equal. If you write down a few more examples where a team made a comeback, you will see that in any scenario you come up with, at some point, their scores had to be equal. And in the opposite example where the team that was in the lead, stayed in the lead you can always find a scenario where they weren't equal at any point.

»
4 недели назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In problem D why don't we manually check for some values and directly see if gcd divides ?

according to chicken nugget if ax+by=c, then c needs to be calculated manually upto lcm(a,b)-a-b.

after this value onwards its a multiple of gcd(a,b).