Thank you for participating in our contest! We hope you enjoyed it. UPD: Implementations have been added.
Solution
Implementation (C++)
Video Solution
Solution
Implementation (C++)
Video Solution
Solution (Observation)
Implementation (C++)
Video Solution
Solution (DP)
Implementation (C++)
1567D  Expression Evaluation Error
Solution
Implementation (C++)
Video Solution
1567E  NonDecreasing Dilemma
Solution
Implementation (C++)
Video Solution
Solution
Implementation (C++)
Video Solution
Lightning Fast Editorial.
Thanks for the Video Solutions.
Liked problem E and really disappointed about not solving it :(
This contest had a great difficulty curve. Kudos!
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).
but one can clearly see that it will get tle if xor value is calculated every time.
Hey, can you please tell me why do we get TLE if we precalculate the xor using for loop? Won't it be O(a)?
We won't get TLE if we precalculate the XOR values. I saw your submission of B, You are getting TLE because you are calculating XOR values for every test case. The time complexity of calculating XOR will be $$$O(n)$$$, and you are doing this for every test case, so your time complexity is actually $$$O(t*n)$$$ and that's why the TLE.
Oh okay I see now. But please tell me how can we precalculate the xor without getting the TLE?
You can make an array, where $i$th element will store the value, 0^1^2^....^i. You should do this before you start processing test cases.
I didn't note that need to precalculate too. But because of pragmas I get AC.
Even with pragmas you wouldn't get AC in the worst case which will be around 1e10
believe me, many got AC
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.
Which Roughly Translates to This:
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
Didnt got an approach to solve C. Were you able to solve C problem

Thanks it get me a better understandimg
seen/good
Good development.
I knew how to solve E the moment I saw it, too bad I suck at implementation. Back to practice.
I think C is more difficult than D. Tutorial of problem D is much more difficult than mine. My solution works O(n)
C is simple if u dont approach the dp way.Just divide the input string into even and odd parts.If u analyse the problem its actually simple addition on adjacent values.This observation is enough to solve the problem .
Yeah i tried dp (couldn't solve), its very easy to make error, or think of wrong transitions equation.
Problem E is great but didn't understand the 11th base for problem D :(.
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)
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.
Yeah!thinking in that direction is hard! I tried one hour for it:)
I actually came up relatively quickly with a solution, but it was a bit unelegant, in my opinion: using backtracking, I fixed the digits that would have a carry, then I calculated how many pairs of numbers satisfy the fixed digits' carries.
Yeah i also did the same backtracking thing considering two cases for each digit whether to give carry to next to next number or not. you can check my code here https://codeforces.com/contest/1567/submission/127981331
Wow! The solution for C is really cool! I got a kinda bruteforce 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 !
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 countertestcase 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.
I suppose there is a mistake in explanation for problem C as $$$9 + 13 = 22$$$
I don't know how to do addition, apparently. It will be fixed soon.
can anybody explain why we are doing (a + 1) * (b + 1) — 2 after spliting it to 'a' and 'b' ? in problem C
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"
Damn it, I forgot that we are supposed to have only positive integers for A and B.
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+(a1)+...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.
I did D by simply printing the largest possible power of 10 (say x) such that (sx) ≥ (n1) in a loop while n is greater than 1. Finally printed whatever remained of s. This works in O(n).
have you got any proofs why is this works? I will be very grateful if u explain it:D
It's actually pretty simple. If the largest power of 10 we can put is x, then we will add 11^log10(x) to the answer. If we choose power of 10, we can get at most y*(11^log10(x/y)) in the sum, y being power of 10 lower or equal to x. Now we just need to prove that 11^log10(x) >= y*(11^log10(x/y)).
11^log10(x) >= y*(11^log10(x/y))
11^log10(x)/11^log10(x/y) >= y
11^(log10(x)log10(x/y)) >= y
11^(log10(x)log10(x) + log10(y)) >= y
11^log10(y) >= 10^log10(y)
11 >= 10
Thanks a lot!!!!
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.
Lightningfast editorial with video solution. Nice Job
I know how to solve F, but I didn't read it during this round. What a pity. MySolution
Editorial of problem C is really good.
Yeah, this time editorial were pretty good and damn fast
Wow, solution to C is incredible!!
Did anyone solve C using brute force in o(2^n) like either consider the carry or don't consider the carry?
I used that approach, check my submission here
When I try to access C Submission link CF says: "You are not allowed to view the requested page"
Fixed!
why Alice and Bob do weird things :(
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;
}
in first else block declare a tmp variable of int type store the value of "b^all" and then compare with a i had the same problem lmao
like else{ ll tmpvar = b^all; if(tmpvar != a){ cout << a+1 << endl; return; } cout << a+2 << endl; }
why is it like this? , means what is the problem , can you explain little bit.
It something related to buffer Sometimes without storing and directly using some function can cause error like
uhh, i dont think it is a buffer or whatever error,
in the first code, when you do double b = a/2; a is considered int and a/2 is done in int division where we just cut everything after decimal.
in second code b = b/2; is double division, so that gives correct output
just enclose ^(xor) in a bracket, ^ has a higher precedence that = operator, that may be the error
yes i think that was the issue (b^all == a) is treated as (b^(all == a)). so in my code it should be ((b^all) == a)
I can't see the implementations. Even after solving the problem it doesnt let me check them.
Fixed!
problem C was irritating to be honest
C question was really good and tricky and thank you for the contest and very fast tutorial
Question D was just amazing and liked the logic behind C and D. It was very good contest
I can't view the codes in the tutorial. It says'You are not allowed to view the respected page'.
Oops, we are fixing it right now. UPD: It is fixed.
In Problem D there is an ambiguity in the statement the decimal number may not be uniquely interpreted in the 11based system, for example, 100_{10} > 100_{11} = 0 + 10*11 = 110_{10} or 100_{10} >100_{11} = 0 + 0*11 + 1*121 = 121_{10}
$$$100_{11}$$$ is never interpreted as $$$10 \cdot 11 + 0 \cdot 1$$$; the digit $$$10$$$ is typically represented by $$$A$$$.
Why it is always optimal to divide the sum into a power of 10.
Could you please explain why for the test case "999999 3" the answer "1 1 999997" is better then "800000 100000 99999"? It seems to me that they have the same sum in 11based system
They have the same sum, but your submission gets WA on test case 4: $$$10_{11}+90_{11}=A0_{11}$$$, but $$$1_{11} + 99_{11}= 9A_{11}$$$.
Oh, I see. Thank you!
For Problem D, regarding the idea of splitting the least power of 10: "we should split the smallest power 10". I can't seem to make a correct algorithm for input, say: 100 3.
I would end up with the 3 following terms: 90, 9, 1.
Starting with [100], splitting into [90, 10] and then, take the smallest power of 10, 10, ending up with [90, 9, 1].
Have I misinterpreted your words?
You should only split powers of $$$10$$$ when we have no other ways to split any of the numbers: $$$90_{11}$$$ can be split as $$$10_{11}$$$ and $$$80_{11}$$$ still, and so a better answer is $$$[80, 10, 10]$$$.
When we add them:
$$$80_{11} + 10_{11} + 10_{11} = A0_{11}$$$.
$$$90_{11} + 9_{11} + 1_{11} = 9A_{11}$$$.
As you can see, we should continue to split all numbers into powers of ten, and as soon as all numbers are powers of ten, then we can take the smallest one.
they are same
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)??
Someone please link memoisation based Dp solution for problem C. Mine is giving wrong answer at test 5.
127946227
^ here is my submission. Hope it helps!
a great exercise would be to make a dp based soln. for finding number of ways when we normally add 2 numbers. I know it is n + 1, but if you can make that dp then you can do that alternatively in this problem.
You can calculate XOR of first $$$n$$$ natural numbers in $$$O(1)$$$. You can learn more about that here.
We were aware of this, but we didn't want to force people to google the solution. The editorial mentions this as well.
It will be better if you add it in editorial. I didn't know about it before seeing this comment.
Added.
flame, please lmk how to be as orzosity as you
thanq
The video is really great
[submission:https://codeforces.com/contest/1567/submission/127980698] O(n) solution for problem D
127980698 here
I think F can be transformed into a 2SAT 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.
Is this your assumption or can you prove that?
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.
After writing that I thought 2SAT are just redundant. You can simply fill two diagonally adjacent empty cells and do a bfs/dfs to spread the coloring.
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: "gridaligned" and "diagonal". Notice that "gridaligned" 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 "gridaligned" 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 "gridaligned" 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 doublecounting, 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.
there is a simpler proof to the 2coloring 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
Was so close to solving B but missed the case when xor of a1 and b is equal to a :(
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 2digit carry operation can be expressed in terms of the decimal digits and the carry bit using at most 10 linear equations.
$$$a_0 + b_0 = n_0 + 10~c_0$$$
$$$a_1 + b_1 = n_1 + 10~c_1$$$
$$$a_2 + b_2 + c_0 = n_2 + 10~c_2$$$
$$$a_3 + b_3 + c_1 = n_3 + 10~c_3$$$
$$$a_4 + b_4 + c_2 = n_4 + 10~c_4$$$
$$$a_5 + b_5 + c_3 = n_5 + 10~c_5$$$
$$$a_6 + b_6 + c_4 = n_6 + 10~c_6$$$
$$$a_7 + b_7 + c_5 = n_7 + 10~c_7$$$
$$$a_8 + b_8 + c_6 = n_8$$$
$$$a_9 + b_9 + c_7 = n_9$$$
where $$$0 \leq a_i, b_i, n_i \leq 9$$$ and $$$0 \leq c_i \leq 1$$$.
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 carryin bit. Similarly, the equations relating the most significant two digits $$$n_{k2}$$$ and $$$n_{k1}$$$ do not have carryout bit. Therefore, there are at most $$$2^{k2}$$$ possible distinct states for the binary vector of $$$k2$$$ carryout 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 1digit carry, where the answer can be computed as $$$(a+1)(b+1)2$$$.
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.
In problem B, Should the elements of the be distinct?
no
D can be done in $$$O(n*log_{10}(s))$$$ as well with simple recursion. https://codeforces.com/contest/1567/submission/127976931
In problem C(Carrying Conundrum), Could anyone please tell Why we are subtracting 2?
The two cases of one of the number in pair being 0
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 $$$>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 $$$>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:
Special Thanks to flamestorm for problemC !!!
Thank you for that round, the problems were really interesting. BUT!!! Maybe C and D should be replaced)
E is the hardest to me.
E was pretty standard. You should practice some good segment tree problems that require custom merge operations. A good one is here : 739C  Alyona and towers.
Why are we doing 2 in (a+1)(b+1) in the editorial of problem C.
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 :)
can anyone explain query part of problem E
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.
color[u] = (color[v] ^ 3);
What is the purpose of this code?I also don't understand the aim of the below part of the code in the solution for F. Can someone explain further. Thanks.
For problem E, the editorial's implementation is very suck.
I am very thank.
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.
Yes, you are right; I updated the editorial some time ago, but for some reason, it didn't seem to update here. I am not entirely sure what's going on ¯\_(ツ)_/¯
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 nondecreasing prefix, longest nondecreasing 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
can someone tell me where this submission might be failing ?
I am in a trouble with the last note of Problem C, "Note that pairs (a,b)and(b,a) are considered different if a≠b." Could anyone give me a proper explanation, please?