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

Автор flamestorm, 5 лет назад, По-английски

Thank you for participating in our contest! We hope you enjoyed it. UPD: Implementations have been added.

1567A - Domino Disaster

Solution
Implementation (C++)
Video Solution

1567B - MEXor Mixup

Solution
Implementation (C++)
Video Solution

1567C - Carrying Conundrum

Solution (Observation)
Implementation (C++)
Video Solution
Solution (DP)
Implementation (C++)

1567D - Expression Evaluation Error

Solution
Implementation (C++)
Video Solution

1567E - Non-Decreasing Dilemma

Solution
Implementation (C++)
Video Solution

1567F - One-Four Overload

Solution
Implementation (C++)
Video Solution
Разбор задач Codeforces Round 742 (Div. 2)
  • Проголосовать: нравится
  • +237
  • Проголосовать: не нравится

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

Lightning Fast Editorial.

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

Thanks for the Video Solutions.

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

Liked problem E and really disappointed about not solving it :(

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

This contest had a great difficulty curve. Kudos!

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

Weak test cases in problem B. Under the given condtions no precomputation should give TLE as no condition was given to limit the sum of all values of a.

Edit: Obviously I mean TLE only for those who have calculated XOR everytime in O(a).

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

    but one can clearly see that it will get tle if xor value is calculated every time.

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

    I didn't note that need to precalculate too. But because of pragmas I get AC.

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

    There is a method that you can use, which can essentially answer each test case in O(1) without having to run any precomputation or use any pragma optimizations.

    It is based on the simple observation in the trend of the XOR value from 0 to a. It goes a little something like this.

    • If 'a' is a multiple of 4, the XOR of the sequence will be 'a' itself
    • If 'a' is a multiple of 2 but not a multiple of 4, the XOR of the sequence will be 'a' + 1
    • If 'a' is an odd number and the 'a' + 1 is both a multiple of 4 and 2, the XOR of the sequence will be 0
    • If 'a' is an odd number and the above constraints fail, the XOR of the sequence will be 1

    Which Roughly Translates to This:

    if(a % 2 == 0)
    {
        if(a % 4 == 0) limit = a;
        else limit = a + 1;
    }
    else
    {
        if((a + 1) % 2 == 0 and (a + 1) % 4 == 0) limit = 0;
        else limit = 1;
    }
    

    Then you can simply perform the usual checks to get to the final answer.

    Here is my submission if you need any further clarifications. 128021227

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

Didnt got an approach to solve C. Were you able to solve C problem

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

seen/good

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

I knew how to solve E the moment I saw it, too bad I suck at implementation. Back to practice.

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

I think C is more difficult than D. Tutorial of problem D is much more difficult than mine. My solution works O(n)

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

Problem E is great but didn't understand the 11th base for problem D :(.

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

    basically 11th base was for hinting to the fact that you need to make n numbers such that they can achieve highest decimal place. example — to make 1000 with 2 numbers consider two cases, 900 100 990 10

    now you can see that in first case you are multiplying higher decimal places with even higher power of 11, 9*((11)^3) + 1*((11)^3) and in second case it is 9*((11)^3) + 9*((11)^2) + 1*((11)^2)

    so basically 1*((11)^3) > 9*((11)^2) + 1*((11)^2)

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

For me at least, C is the type of problem that seems quite hard, then, when you see the editorial, you realise it was really easy and you were the one who complicated it.

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

Wow! The solution for C is really cool! I got a kinda brute-force solution in O(2^log10(n)). Fixing which digits are going to add in such way that, it would create a carry. Then just checking how many pairs satisfy that kind of fix carried addition. Here is my submission link Link !

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

I thought of an algorithm to F, 127979513, that came up to mind when simulating filling the grid by hand. However, this algorithm failed Pretest #6, and I don't know where it went wrong, nor am I able to come up with a counter-testcase for it. The algorithm is as follows.

  • Create an array $$$ans$$$, which is our answer. Initially, all elements of $$$ans$$$ are $$$0$$$.

  • First, check if a marked cell has an odd number of unmarked cells. If true, then there must be no grid that satisfies the condition. Else, for each marked cell $$$(x,y)$$$ with $$$n$$$ neighbors, $$$ans[x][y] = \frac{5n}{2}$$$.

  • Create an array $$$req$$$. $$$req[i][j]$$$ is initially $$$0$$$ for unmarked cells and $$$ans[i][j]$$$ for marked cells. Think of $$$req$$$ as the sum which we still need to 'distribute' to adjacent cells. We will subtract from $$$req$$$ when we update the neighbors of marked cells such that at the end, $$$req[i][j] = 0$$$ for all $$$i$$$ and $$$j$$$.

  • Iterate through each cell in $$$ans$$$. For each unmarked cell with $$$ans=0$$$, update its value with $$$1$$$.

Updating a cell goes as follows:

  • To update an unmarked cell by $$$a$$$, set its value to $$$a$$$. Then, update all adjacent marked cells with $$$a$$$.

  • To update a marked cell at $$$(x, y)$$$ by $$$a$$$, subtract $$$a$$$ from $$$req[x][y]$$$. Then, if $$$req[x][y] = 1$$$ or $$$2$$$, we know that all remaining unmarked neighbors of $$$(x, y)$$$ should contain $$$1$$$. Thus, update them by $$$1$$$. The same thing happens when $$$req[x][y] = 4$$$ or $$$8$$$, where all its remaining neighbors should be $$$4$$$.

Does anybody have any idea on where or why it breaks down? Any help would be appreciated.

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

I suppose there is a mistake in explanation for problem C as $$$9 + 13 = 22$$$

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

can anybody explain why we are doing (a + 1) * (b + 1) — 2 after spliting it to 'a' and 'b' ? in problem C

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

    There are a + 1 ways of getting the odd digits, there are b+1 ways of getting the even digits and they are independent. After that we have two solutions that dont work, (0,s) and (s,0), so we remove them with this "-2"

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

    if you're talking about problem C we can divide number n to 2 numbers a and b. by the position of their digits depending on odd or even. Then you have to make that number sum of 2 different numbers. we can 0+a,1+(a-1)+...a+1 in total a+1. it's the same for b. but the problem said positive integers which don't include 0. so we exclude the first number equals 0 and the second number equals zero which are 2. So the answer is (a+1) * (b+1) — 2. It might be different from some people's solution.

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

I did D by simply printing the largest possible power of 10 (say x) such that (s-x) ≥ (n-1) in a loop while n is greater than 1. Finally printed whatever remained of s. This works in O(n).

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

E is a very standard problem. I don't see it appropriate for a Div2 Round. I guess it would be better in a D of an Educational Round.

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

Lightning-fast editorial with video solution. Nice Job

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

I know how to solve F, but I didn't read it during this round. What a pity. MySolution

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

Editorial of problem C is really good.

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

Wow, solution to C is incredible!!

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

Did anyone solve C using brute force in o(2^n) like either consider the carry or don't consider the carry?

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

When I try to access C Submission link CF says: "You are not allowed to view the requested page"

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

why Alice and Bob do weird things :(

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

Can someone help me to debug this code Q(B) — MEX or Mixup

include <bits/stdc++.h>

using namespace std;

int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long int t = 1; cin >> t; while(t--) { long long a,b; cin >> a >> b;

long long all = 0;

    a--;

    if (a % 4 == 0)
     all = a;

    if (a % 4 == 1)
      all = 1;

    if (a % 4 == 2)
     all =  a+1;

    if (a % 4 == 3)
     all =  0;


    a++;

    // cout << all; 
    if(all == b)
    {
        cout << a << '\n';
    }
    else
    {
        if(b^all != a)
        { 
            cout << a+1 << '\n';
        }
        else{
            cout << a+2 << '\n';
        }

    }
}

}

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

I can't see the implementations. Even after solving the problem it doesnt let me check them.

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

problem C was irritating to be honest

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

C question was really good and tricky and thank you for the contest and very fast tutorial

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

Question D was just amazing and liked the logic behind C and D. It was very good contest

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

I can't view the codes in the tutorial. It says-'You are not allowed to view the respected page'.

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

In Problem D there is an ambiguity in the statement- the decimal number may not be uniquely interpreted in the 11-based system, for example, 100_{10} -> 100_{11} = 0 + 10*11 = 110_{10} or 100_{10} ->100_{11} = 0 + 0*11 + 1*121 = 121_{10}

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

After seeing solution to E, I started to wonder what Segment trees can't do.

Maybe it can even end world hunger in O(nlogn)??

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

Someone please link memoisation based Dp solution for problem C. Mine is giving wrong answer at test 5.

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

You can calculate XOR of first $$$n$$$ natural numbers in $$$O(1)$$$. You can learn more about that here.

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

flame, please lmk how to be as orzosity as you

thanq

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

The video is really great

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

[submission:https://mirror.codeforces.com/contest/1567/submission/127980698] O(n) solution for problem D

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

I think F can be transformed into a 2-SAT problem (maybe easier to think for some people): If one marked cell has an odd number of adjacent empty cells, obviously there's no answer.

Otherwise if it has 2 adjacent cells, one must be filled with true (representing 1) and the other must be filled with false (representing 4). Let the two cells be $$$a$$$ and $$$b$$$, we have the relation $$$(a \lor b) \land (\neg a \lor \neg b)$$$.

If it has 4 adjacent empty cells, it's always better to fill the opposite empty cells with the same number, and we can get a similar relation as the above case.

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

    If it has 4 adjacent empty cells, it's always better to fill the opposite empty cells with the same number, and we can get a similar relation as the above case.

    Is this your assumption or can you prove that?

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

      Can't say I proved that but this is my rough idea:

      Cells that share common empty cells form a "connected component" in which the way of filling two diagonally adjacent cells determines how to fill the whole component.

      1. If some marked cell has only two adjacent empty cells, we should make the two empty cells different => opposite empty cells should be the same.
      2. Otherwise all marked cells have four adjacent empty cells => opposite empty cells can be the same or different.
    • »
      »
      »
      5 лет назад, скрыть # ^ |
      Rev. 2  
      Проголосовать: нравится +33 Проголосовать: не нравится

      I also solved the problem with this method, and my proof went something like this:

      Build a graph whose edges are pairs of unmarked cells that we want to force to be different. For marked cells with 2 adjacent unmarked cells, draw an edge between them; for marked cells with 4 adjacent unmarked cells, draw edges between (top, left) and (bottom, right).

      Any bipartite colouring of this graph yields a valid solution. To prove such a colouring exists, it suffices to show that there are no odd simple cycles in the graph.

      Let's assume, for the sake of contradiction, that such a graph with an odd cycle exists.

      There are two types of edges in this graph: "grid-aligned" and "diagonal". Notice that "grid-aligned" edges do not change the parity of coordinates, but "diagonal" edges change the parity of both coordinates. Therefore there must be an even number of "diagonal" edges, and so, an odd number of "grid-aligned" edges.

      If the edges are straight lines, the cycle is a boundary of a section of the grid. Notice that the only marked cells on the boundary are on the "grid-aligned" edges, so there are an odd number of them.

      Count the number of ordered pairs of marked cells (P, Q) where P and Q are adjacent and both on or inside the boundary. By double-counting, this number must be even.

      However, for a marked cell inside the boundary, we know that the number of adjacent marked cells on or inside the boundary must be even, and for a marked cell on the boundary, this number must be odd. As there is an odd number of marked cells on the boundary, the number of ordered pairs is odd, which is a contradiction.

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

        there is a simpler proof to the 2-coloring if you instead connect (top,right) and (bottom,left) for the diagonal edges .

        the difference between the x and y coordinate remains invariant under the diagonal edges since it ±1 to both coordinates , and grid aligned edges changes the difference by ±2 since you need a net change of 0 to the difference thus you need an even number of grid aligned edges (parity argument still holds for even diagonal edges)

        UPD : this is wrong I missed that for connecting two forced unmarked cells you can get diagonally left edges

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

Was so close to solving B but missed the case when xor of a-1 and b is equal to a :(

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

I used a recursive algorithm for solving problem 1567C - Carrying Conundrum

128012196

The algorithm is based on the idea that as $$$2 \leq n \leq 10^9$$$, the 2-digit carry operation can be expressed in terms of the decimal digits and the carry bit using at most 10 linear equations.

Linear Equations

If the decimal representation of $$$n$$$ has $$$k$$$ digits, where $$$1 \leq k \leq 10$$$, then the equations relating the least signification two digits $$$n_0$$$ and $$$n_1$$$ do not have carry-in bit. Similarly, the equations relating the most significant two digits $$$n_{k-2}$$$ and $$$n_{k-1}$$$ do not have carry-out bit. Therefore, there are at most $$$2^{k-2}$$$ possible distinct states for the binary vector of $$$k-2$$$ carry-out bits.

It is noted that the $$$k$$$ equations are separable into two independent sets of linear equations according to the odd/even parity of the digit index. This leads to the same solution given in the editorial for partitioning $$$n$$$ into two numbers $$$a$$$ and $$$b$$$, with the conventional 1-digit carry, where the answer can be computed as $$$(a+1)(b+1)-2$$$.

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

I am an absolute noob :’(. I got the idea of problem B but could not be able to find anything on the Internet about the formula for xor of 1…n. So bad. Anyway I love the editorials and thank you all for an awesome contest.

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

In problem B, Should the elements of the be distinct?

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

D can be done in $$$O(n*log_{10}(s))$$$ as well with simple recursion. https://mirror.codeforces.com/contest/1567/submission/127976931

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

In problem C(Carrying Conundrum), Could anyone please tell Why we are subtracting 2?

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

I feel like the Editorial to Problem D doesn't carry all cases, so I did a visual explanation.

Take $$$s=113$$$ and $$$n=7$$$. First we split the decimals into powers of $$$10$$$:

We could still need more numbers to fully get $$$n=7$$$. So we look for the smallest number $$$ \gt 1$$$:

An we split it into $$$10$$$ equal parts. To achieve this you can just replace this number with a tenth and then push back this value 9 times:

We repeat this step, until we have at least $$$n$$$ values. The editorial proposes a $$$O(n \log n)$$$ solution for this step using a priority queue, but this can also be done in $$$O(n)$$$ if we keep 2 separate arrays, one for numbers $$$ \gt 1$$$ and one for numbers $$$=1$$$. The former one will be automatically sorted at all times following this procedure.

In the next step we could have more than $$$n$$$ numbers. We just collect the overflow and add it to the last value:

And we are done:

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

Special Thanks to flamestorm for problem-C !!!

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

Thank you for that round, the problems were really interesting. BUT!!! Maybe C and D should be replaced)

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

E is the hardest to me.

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

Why are we doing -2 in (a+1)(b+1) in the editorial of problem C.

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

    Because the pairs (0, n) and (n, 0) have also been included in (a+1)*(b+1). We need to remove them as we want both numbers to be greater than 0. Hope you got it. I also did not get it but I tried with n = 22 and I understood. Hope it helps you too :)

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

can anyone explain query part of problem E

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

1567F : As I can see, those who got AC in the contest had simpler solutions than the author. The editorial is quite general, I think. Just curious how this problem can be extended.

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

Problem C Submission with complete Newbie Implementation https://mirror.codeforces.com/contest/1567/submission/128091033

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

color[u] = (color[v] ^ 3); What is the purpose of this code?

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

I also don't understand the aim of the below part of the code in the solution for F. Can someone explain further. Thanks.

// flip each cell appropriately, column by column
    for (int j = 1; j <= m; j++) {
        int curr = (j % 2 ? 4 : 1);
        res[1][j] = curr;
        int prev = color[val[1][j]];
        for (int i = 2; i <= n; i++) {
            if (grid[i][j] == '.') {
                if (color[val[i][j]] != prev) {curr = flip(curr);}
                res[i][j] = curr;
                prev = color[val[i][j]];
            }
        }
    }
»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For problem E, the editorial's implementation is very suck.

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

I think there might be some mistakes according to tutorial of problem F. In the tutorial it said: "However, the tricky case is to deal with cells with 0 unmarked neighbors". But it is obvious that cells with 0 unmarked neighbors will get the value of 0 in the end. So what is really tricky to deal with is those cells with 4 unmarked neighbors. I think the writer might confuses the definitions of marked and unmarked cell.

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

E editorial seems a bit impl heavy. My solution is a lot shorter(50 lines).

Basically, each node stores the following: answer for the range, longest non-decreasing prefix, longest non-decreasing suffix, size of range, 1st and last elements in the range. I won't explain the combine part as it's easy to understand from the code. Also, notice you could just store l,r for range instead to take care of the last 3 parts but I decided not to store the elements in an array.

Solution