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).
Firstly I was also calculating XOR(n) for every test case and got TLE :/
Yep. I did the same mistake :(
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.
Oh, yeah I get it now. Thank you so much :)
ll xorr; ll rem = (a — 1) % 4; if (rem == 0) xorr = a — 1; else if (rem == 1) xorr = 1; else if (rem == 2) xorr = a ; else if (rem == 3) xorr = 0;
We can calculate xor upto a by this method also that taking modulo of a1 with 4 and if remainder is o then xor will be a1 else other condition. Time complexity =O(1)
Precalulate zor values before inputting test cases then time complexity will be O(t)
To calculate the xor value of all the numbers from 1 to n in O(1) time :
Find the remainder rem = n % 4
If rem = 0, then xor = n
If rem = 1, then xor = 1
If rem = 2, then xor = n+1
If rem = 3 ,then xor = 0
This works because we get 0 as the XOR value just before a multiple of 4. This keeps repeating before every multiple of 4.
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
Mine too accepted with pragmas + bruetforce
I got tle :'( , then I had to use the properties of XOR. It was good tho
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
There is another way of computing xor in O(1) time complexity because xor of numbers from 1 to n follows a pattern and pattern repeats itself in the following manner
lets say rem = n%4
If rem = 0 xor = n
If rem = 1, xor = 1
If rem = 2, xor = n+1
If rem = 3, xor = 0
and with slight modification we can get the required results.
well even looping through n and using pragma optimization gives AC:):)
Didnt got an approach to solve C. Were you able to solve C problem

Thanks it get me a better understandimg
yes
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 .
But can you tell me why are we subtracting 2?
yes as in question both numbers should be positive....hence if s=given sum,then we have to ignore two possibilities (s,0) and (0,s).
Yeah i tried dp (couldn't solve), its very easy to make error, or think of wrong transitions equation.
Yes .
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! we do try to impliment dp for some problems and will get surprised looking at easy solution using some logic
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.cc/contest/1567/submission/127981331
yes if someone approaches the DP way it gets complicated ,but if one thinks greedily its easy. Somehow i got the greedy idea on time:)
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 !
Video tutorials are nice.
Very fast and amazing editorials with video solutions too.
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.
The editorial came earlier than expected thank you.
For solution C: "Note that in every other column, the addition Alice performs is correct."
This doesnt seem to be correct in the example in the question?
c was nice
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"
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.
because the answer ask the pair of positive integer,answer can appear tow pairs (0,a+b)and(a+b,0),so you need to minus 2.
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).
Wouldn't the time complexity be O(nlog10(s)) because of computing the power of 10 for each iteration?
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
Can you explain your approach?
Yes,I did using backtracking all the possible cases.Link to my submission 128014364
Can you explain your approach?
firstly i precalculated no.of ways to represent n(n<20) as a sum of two single digits. Now,start from the leftmost digit (let's say x) . It can be formed by two ways a) simply as a sum of two single digits which is ways[x] b) can be formed by a carry(which is 1) ,in this case x becomes x1 and the digit next to the next to the current digit say y becomes 10+y; In each case the no.of ways the remaining digits can be formed can be calculated recursively.
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 :(
C was damn awesome after looking at editorial! Felt happy after looking at the editorial although I was unable to think it during contest
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
thank you
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
I miss read the problem statement of B and got stuck and tried to find xor of every a1 elements ;_;
Really fun contest. Unfortunately I was too dumb to realize the miniscule errors I made in B and D :'(
But my rating increased by 69, so I guess that's nice.
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.
I got you! Thank you for the time you took to reply.
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)??
XD
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!
Great solution . Thanks
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.
https://codeforces.cc/contest/1567/submission/127954668
How does this solution for D split the 10s into 9s and 1s if there isnt enough digits?
Why does this get WA on test 3 in problem B https://codeforces.cc/contest/1567/submission/127978046
Thank you so much
flame, please lmk how to be as orzosity as you
thanq
The video is really great
[submission:https://codeforces.cc/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.
check in GFG
In problem B, Should the elements of the be distinct?
no
Recursive Solution for problem C.
Can someone explain the DP solution for problem C in a detailed manner? Thanks!
In B, I never knew such formula existed for xor and I got TLE for using the naive approach. But then in the end, I formulated my own formula for xor and achieved O(1) time. I am so noob in coding, but to know that I constructed a formula that I never knew existed gives me some motivation. Its my dream to at least solve 3 problems during one contest
D can be done in $$$O(n*log_{10}(s))$$$ as well with simple recursion. https://codeforces.cc/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
Got it. Thank You.
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:
How 3+4+100+4=110 in base 11 in the third test case explanation? when 70+27=97 in base 11 in the first test case explanation? Am i missing any knowledge? Please share it! Thanks!
I got it now. Think like how we do normal base 10 addition
9 + 1 = 10 where 1 is carry and 0 is the sum at that position. Similarly, In base 11, 4+4 = 8 + 3 = 9 + 2 = 9 + 1 + 1 = A + 1 = 10 where 1 is carry and 0 is sum at that position. Note: 9 + 1= A because in base 11, digits are {0, 1, 2,...9, A}
See this https://math.tools/table/addition/base/11 Similarly think for binary addition (Base 2 addition). You will definitely understand it.
Special Thanks to flamestorm for problemC !!!
I really loved problem E. I have written an implementation of it by using two extra pieces of information at each node l and r, where l and r are the range that this node covers. I did this because I felt that the implementation of the query function in the editorial was hard for me to understand and hence I wrote this code which only changes the merge function and the query function is not changed: 128038196
Thank you for that round, the problems were really interesting. BUT!!! Maybe C and D should be replaced)
In Problem B, the facts that we use are for precalculating XOR from 1 to n are mismatching with the facts that I have.
https://www.geeksforgeeks.org/calculatexor1n/
Can somebody confirm if it's wrong in the above link?
I had messed up with a bracket in my code, giving me an erroneous result. rather than the above could you check how the 2 statements below are calculated differently?
(d^b)!=a
d^b!=a
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 :)
got it thanx!!
I guess sol(obs) for C is wrong somewhere... because 15 * 90 = 1350 instead of 1950.
Please correct me if I'm taking the solution in the wrong way!
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.
Problem C Submission with complete Newbie Implementation https://codeforces.cc/contest/1567/submission/128091033
Hey, Can u please help in one sample test case of problem c the last one (10000) how its 99 ,I am getting only 81 cases with 2 case only Refer this image if u need According to me The blue ones underlined will have no contribution, and C means it will generate carry and NC means it will not generate carry Any help will be appreciated
Check the cases they should be 10X9+9X1=99
Thanks man, appreciated
Nice round bro . I think E is easier than C
who can explain the C by using dp,the translate as if some trouble
Can someone explain D in simple words? I have looked at both the editorial and the video but don't seem to grasp the idea.
why the video solution can not connect
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.
Problem E:
My Submission
Giving wrong answer on test case 3 128573992
what's wrong in my code?