Hi Codeforces!

We are glad to invite you to our first Codeforces Round Codeforces Round #738 (Div. 2) which will be held on Aug/15/2021 17:35 (Moscow time). This round will be **rated for participants with rating less than 2100**. We will be glad to see participants from the first division to join out of competition as well!

In this round, as the best friend of Mocha's, you are going to help her to solve the problems she meets.

The problems are prepared by 2sozx, JJLeo, Serval, Oshwiciqwqq and me. We hope you will enjoy the round. :P

We would like to thank:

Monogon for awesome coordination of this round and helping us improve our problems.

Toxel for excellent testing and discussion of this round.

cppascalinux, PurpleCrayon, ijxjdjd, wxhtzdy, qxforever, fixed_lxy, HKvv, jxm2001, GGN_2015, I-AK-ACM, wzy2001wzy, xrbxbn, insere, zhtjtcz, lgwza, qwertyczx, godel_bach, withinlover, Saisyc, Matrix53, rancy, asrinivasan007, QuangBuiCP, dream_love_atcoder, Atlantis592, shakugan_no_shanatan_, DovahkiinGA and 5095187020216 for testing our rounds

MikeMirzayanov for the amazing Codeforces and Polygon platforms.

You will be given **5 problems** to solve in **2 hours**.

Scoring distribution: **500—750—1000—(1500—1500)—3000**.

We recommend you to read the statements of all problems. Good luck & Have fun! :D

**UPD:** Great thanks to KAN and isaf27 for helping with Russian language translation and clarifications.

**UPD2:** Sorry for the long queue and my mistake in estimating the difficulty of problem D2 and E.

**UPD3:** Editorial is available now.

**UPD4:** Thank you for your participation in this round! Congratulations to the winners!

**Div. 2**

**Div. 1 + Div. 2**

Darn, my VIP tester streak ended.

As a tester, Monogon orz.

If you don't upvote Monogon, you won't get positive delta. Good luck!

But, unfortunately, the inverse is not true. :(

PurpleCrayon using old cheap tricks to get upvotes for Monogon!

PurpleCrayon should understand that even without these cheap tricks everyone will upvote Monogon for sure!

He's doing it to get upvotes for himself

Let's get him downvotes!

But You got Downvote Lol!!

Oh my GOD!

1-gon,just remember one thing ,with great power comes great responsibility. Orz 1-gon Orz

You can still be VIP coordinator...

Yet another blog where Monogon has more upvotes than the blog itselfAnnouncement blog 3 days before round -> bad round

why

hii rotavirus

1-gon is god

Tourist is god

Errichto is god

Are you a polytheist?

And you are a dog!

call it a bad round 3days before the round begins -> bad comment

As a tester, have fun!

ah, a man of culture indeed

exactly..

As a tester.

You must be careful with your codeand try to solve $$$x$$$ ($$$1 \leq x \leq 5$$$) problems!Good luck!

-QuangBuiYT

Can you provide a link to such problem?

Hoping to regain specialist after this round!

As a tester, oooops I find myself a problem setter! lol

As a coauther & tester for the first time, I hope you enjoy this round!

orz

As a tester, hope you enjoy those excellent problems and wish you all high rating!

Hope to become pupil again this time!

The scoring distribution looks noice!!!

hoping for nice round and thanks author for spending your time for us

Hope for a great round,and thanks for the early score distribution!

OrzOrz

As a tester,I'm sure that it is a fabulous round!

As a Participant, I Would like to remind my Fellow Participants, not to get wooed by the Easy Score Distribution, as the Problems are gonna be hell a lot Tricky & many of us will be getting...

Can You Guess ?`WA on Pretest 2`

:DHope to become expert for the first time, that feeling when you get something you dreamt 1 year ago :)))

hope this time i can reach pupil.(●'◡'●)

You will reach pupil in 7 contests.

speedforces ABC?

Maybe not speedforces, but balanced like 1300 A, 1500 B, 1700 C or something like that but harder than normal round ABCs.

why he write 6problems as e is the hard version of d only

Thank you for your reminding, I have updated the announcement :)

Score Distribution looks great! :)

Behold The Chinese Round! Orz

Good luck to everyone!

Auto comment: topic has been updated by Bazoka13 (previous revision, new revision, compare).If we are given 5 problems so why the scoring distribution is saying we're ganna have 6 problems ?

Spoiler1500 appeared twice inside the bracket denotes easy version and hard version of the same problem, and the only difference between them is the data constraints.

As a participant,Orz yourself

I have a mock round for my ICPC regionals and it ends just 5 minutes before the start of this codeforces round. Challenge accepted.

5 problems again -_- Another speedforces maybe! but this time ABC :/

As a tester up vote me ,problems are very nice

The problem is that you are not in the tester's list...

hi covid

Are you the main account of Aman2?

Funniest thing ever LOL!

Another one hoping to speed to reach -100 contribution lmfao

Sparky_Master_WCH1226 achieved it in quite a hilarious way though.

As a tester, hope you enjoy those excellent problems and wish you all high rating!

Glad to see partial Scoring :)

i hope i can get top 10 in this contest

Top 10 before-contest commenting rituals

Score distribution looks good:)

Hope I can become Candidate Master again.

Wow, this round was prepared by students from Beihang, which my ideal university is!

good luck all :)

Won't be able to attend this round for some personal reason. Wish all the participants good luck. Hopefully, you will have a great time brainstorming.

i wanna be specialist again, wish me good luck qwq

Wish all the best to you. And I hope I'll be able to get the lost rating in my last contest.

thx, and wish u good luck qwq

Rating is temporary, class is permanent

pls not bitwise again

How to bitwise?

wdym

Looks like they bitwised again

I always wanted such an amazing score distribution lol.

I hope

PUPILthis timeSpoilercoz Hope is a good thing

Please don't start hopeforces

why u being so negative dude?

I missed the part where that's your problem.

See my contributions if you want an answer for that

but i know that i won't make it , coz i am un prepared

Finally there is a CHINESE Round!!!

Just enjoy the round and test your skills.

i just wanted to ask author , are covid cases still coming up in china?

Have they forgot to send an email? I didn't receive any!

Hoping for Pupil :)

I have searched the whole post for these few words "The statements will be short and clean." But unfortunately, I couldn't find it and get disappointed :[ :P .

Hi bros!!!!!!!!

Need your contribution dude

Yeah!!!!!!

Dont be mad , do code .

Ok. I'm ready to help my new friend Mocha, if can :D

speedforces!

hello guys

The downside of giving an easy A is you get a long queue...

queue not ending!

queue forces

The round should be unrated , such a long queue .

Queue is taking too long,if we get a wrong ans it tells us after minutes,and because of that we can't focus on ongoing quuestion, and time is also lost

queueforces ???

Ya too much long queue!

Speedforces + long queue = RIP ratings:(

I think so many cheaters around there , solution are leaked in telegram, youtube

Dude, Why are u searching that in contest time. xD

Dude cheating doesn't make sense , I suspected submissios for B and C

Especially B , See how less submission difference for A and B

Oke. but u should use your brain more without wasting time in thinking about cheating at least in contest time. (:

I also against cheating may be most of them ^_^

Don't worry , they will never have their rating depicting their true skills. They may get increased ratings now for which they cheat, but they're for sure gonna fail in long run. Since 2 years , one can observe the increased amount of cheating in these contests. I can understand ,it really hurts when you work genuinely and you don't get what you deserve because of these mfs.

I want to see that shittiest pretest 2 for B

`??`

found that

Actually I am the shit

Hope I can become Candidate Master.

The same answer for how I can become you.

Too long queue and Speeforces round , Unfair contest

div2.5

first 4 problems were like 4 problems of div3.

s

one of the most standard rounds I have seen in my life

Hope to becume ♂

~~Dungeon~~master♂ after this roundAh ♂ That's ♂ Good

Easy contest!!

is the solution to D2 some form of optimization of D1 using bitset in C++?

I'm not sure what was intended, but I used a randomized approach until there existed a connected component of large size, then I just tried all edges that have an endpoint not in that large component.

Nice problems!

Here's an extended version of $$$C$$$ which is cool:

You won't be given the array $$$a_i$$$ itself, instead you will be given $$$n$$$ and you can interact and ask for particular values of $$$a_i$$$. Ask at most $$$20$$$ queries and output the answer as required in the original problem. Assume $$$n < 10^5$$$.

Okay here is a solution as some people are asking:

SolutionBasically you want to find an $$$i$$$ such that $$$a_i = 1$$$ and $$$a_{i-1} = 0$$$. You can just assume $$$a_0 = 0$$$ and $$$a_{n+1} = 1$$$. Let's say $$$\text{solve}(l, r)$$$ finds such $$$i$$$ in $$$[l, r]$$$ given $$$a_{l-1} = 0$$$ and $$$a_r = 1$$$.

For $$$\text{solve}(l, r)$$$: If $$$l = r$$$, we found the required index. Otherwise, check $$$m = (l+r)/2$$$, if $$$a_m = 1$$$, call $$$\text{solve(l, m)}$$$ otherwise call $$$\text{solve}(m+1, r)$$$. Now, answer is just $$$\text{solve}(1, n+1)$$$.

How would this version be solved?

I have thought of a way in which we can find if the first element is 1 (to check for chains like n+1,1,2,3,4,...n) and to ask if a[n]==0 to check for chains like 1,2,3,4,5,...n,n+1.

However how to check for other cases where the answer is possible.

you have to find place i where a[i] == 0 and a[i + 1] == 1.

you can do that using binary search.

after you find it (or if it doesn't even exist) you can solve it

How to find it using binary search?

tbh I'm not sure whether this works but I would do sth like that

I hope this works :D

But the number of queries will be greater than 20 ( log(10^5)==18 and then in each step of binary search we have to ask two values so 2*18==36>20 )

why 2?

Nvm saw your implementation above

Yea, nice one.

But I personally don't like it when problem consists of 10 easy subproblems, so I would make this as a standalone problem

What type of queries we can ask from interactor?

Particular values of $$$a_i$$$

Umm ok, we can do binary search. Really cool problem.

It exists on cf https://codeforces.com/problemset/problem/1019/B . It is one of my fav problem.

How to solve E?

E was just like you calculate total number of sequence or pairs with gcd == x, like go from end

pseudo code be like

for j = m to j = 1:

ans[j] = calculate(j)

for(int i = 2 * j ; i <= m; i+=j) ans[j] -= ans[i]

calculate(j) is of complexity m / j * n, using dynamic programming

in total its $$$\sum_{y=0}^{m} (n * (m / j)) = O(m n log m)$$$

Can u explain how to make calculate(j) with the constraints a1 + a2 + ... + an <= m

https://codeforces.com/blog/entry/93788?mobile=false#comment-829127

I have explained here

There is also knapsack dp method using prefix sum , instead of generating function, refer Neal submission

let f(i) = number of sequence of length n with gcd == i for each range [lj,rj] 1 <= j <= n , there will [ (lj+i-1)/i , rj/i] number of element which are of multiple of i and therefore number of choices as well.

Now, to compute f(i) we need to find a solution to the equation

a1 + a2 ... an = m/i where each aj : [(lj+i-1)/i,rj/i] we can use PIE to solve above equation.

Now, total number of sequence with gcd 1 will be sum of mu(i)*f(i) over 1 <= i <= m

Even though I am not the target participant for this problemset, I thought the authors might enjoy some feedback.

A: No comment.

B: Classic problem. It is ok to give a classic problem as easy problem.

C: The construction is clean, I liked it. The difficulty is appropriate. It is very similar to the construction of an Hamiltonian cycle in a tournament.

D: Very cool problem. One immediately guesses that it must be possible to add an edge until one of the two forests becomes a tree. Then, it is easy to obtain an $$$O(n^2)$$$ solution, which is enough for the easy subtask. The hard subtask, which was quite hard for me, requires to optimize the solution to something pseudo-linear (in my case to $$$O(n \log(n)^2)$$$, but I am pretty sure that it is possible to do better). I really enjoy problems about optimizing quadratic solutions to pseudo-linear, and this is a great example of such a problem. It was a very nice idea to split the problem in two subtasks since the easy version is considerably easier than the hard version but still interesting.

E: Standard problem. If one knows the right techniques (i.e., mobius mu function and backpack with copies of the same item) this is straightforward.

Overall, it was a well-prepared contest. I had fun thinking about problem D. Thanks to the authors.

can you share some resources to learn the topics u mentioned for problem e ?

In problem D, you can add all possible edges from $$$1$$$ to other vertices. Then, divide all vertices into two groups: the first group consists of vertices in the same CC with $$$1$$$ in the first graph and in different CC with $$$1$$$ in the second graph, the second group is vice versa (some vertices may not lie in any group). Now we can add an edge between any pair of vertices from different groups, so we just have to maintain two queues of these groups and delete a vertex from a queue if it becomes bad at some point. Overall, it works in $$$O(n \cdot DSU(n))$$$.

Neat solution. I would have never found this one during the contest.

My solution is more involved (and thus I will not describe it). It maintains a sufficient amount of helping structures so that it is always possible to find a good edge to add in $$$O(\log(n))$$$ amortized time per edge (and $$$O(\log(n)^2)$$$ amortized time is required to update all the structures when an edge is added).

Interesting, that the mu function makes it straightforward. But since I have no about what it is (:P), I ended up with a separate DP which counts arrangements for all GCDs efficiently, eventually ending up with GCD = 1 arrangements.

The important formula is (valid for any set $$$S$$$ of $$$n$$$-uples)

It is not hard to prove (once you look up the properties of the Mobius function $$$\mu$$$ on wikipedia) and it comes handy in a large number of problems.

Can you share some resources from where a beginner (in this topic) can practice them?

I learned a bit from this Codeforces tutorial Link.

Thanks a lot.

Very cool, thanks a lot! I'm probably going to spend tonight learning more about it :)

As for the DP solution I mentioned, the idea is to iterate over all values $$$g <= M/N$$$ in decreasing order, calculating the number of arrangements that follow the constraints on the sum and the individual value ranges, such that the GCD of the sequence is $$$k*g$$$ for some positive integer $$$k$$$. $$$dp[i][j]$$$ = number of valid suffixes starting at $$$i$$$ and with sum at max $$$j*g$$$ that follow the constraints I mentioned above. To transition, take the sum of all $$$dp[i][j-x]$$$ such that $$$l[i]/g <= x <= r[i]/g$$$. We can make transitions constant time by maintaining prefix sums.

Now, let's denote arrangements with GCD exactly $$$g$$$ as $$$count[g]$$$. $$$count[g] = dp[0][M/g] - \sum count[x]$$$, where $$$x$$$ are multiples of $$$g$$$. Our answer will lie in $$$count[1]$$$.

May you link on set of classic problems as B? It'd be useful for newbie as I.

Edit: I am talking about D2I also had the $$$(\log{n})^2$$$ idea initially (small to large merging with some multiset-like data structure). I optimized it by using the following simple idea to construct edges:

If any forest becomes connected, we are done. Otherwise, both forests have at least $$$2$$$ components. Consider any two nodes $$$x$$$, $$$y$$$ in distinct components of the first forest and any two nodes $$$p$$$, $$$q$$$ in distinct components of the second forest.

If $$$x$$$ and $$$y$$$ are in distinct components of the second forest, then add edge between $$$x$$$ and $$$y$$$. Otherwise, at least one among $$$u$$$ and $$$v$$$ will be in a different component than $$$x$$$ and $$$y$$$ in the second forest, say $$$u$$$. Now, at least one among $$$x$$$ and $$$y$$$ will be in different component than $$$u$$$ in the first forest, say $$$x$$$. So merge $$$u$$$ and $$$x$$$.

This construction in fact acts as a proof for the solution!

very cool solution

Solved A and C but with 9 wrong answers and very slow. My worst contest yet. Look forward to improving speed and accuracy.

One of the easiest div2D1 ever

i should have started with D i got idea + implementation in less than 10 min.

again unable to solve A,B,C

Thanks for the contest, it had nice problems especially C and E.

However I'm not much of a fan of D1. I saw constraints and just guessed that trying every edge in any order would work. I suspect that many other participants did the same without even trying to prove it.

Just my opinion, but I'm not much of a fan of problems where guessing the answer and getting proof by AC is significantly easier than proving why it works.

I think it was not guessing the answer.

If you have read/studied Krushkal algorithm it uses Dsu to ensure that the new vertices do not form cycle with previous edges. Same is the case with this problem.

That does not guarantee optimality of the construction.

I took all the unaccounted edges in a set and traversed through all and checked whether adding would lead to a cycle in any of the 2 graphs. However, I got time limit exceeded on test 6. Can you tell what extra optimization did you do? Here's my submission.

Going through all the unvisited edges is alredy n^2, checking for cycle makes it n^3 but i'm not sure if it's mathematically n^3 only or not. Rather, just check if it's part of different trees or not by mantaining array marking some sort of tree number for every node and mantain this array. Again, not exactly sure what the complexity will look like for this.

Thank you!

My explanation is that:

$$$[1]$$$ Let $$$u$$$ and $$$u'$$$ are any 2 nodes in Mocha graph that not in the same connected component

$$$[2]$$$ Let $$$v$$$ and $$$v'$$$ are any 2 nodes in Diana graph that not in the same connected component

There will be at least a way to connect $$$u-v, u-v', u'-v, u'-v'$$$ to form 2 forests without making a cycle to it.

If there is no such $$$u'$$$ or $$$v'$$$ then there will be no more edge since no matter how we add edge it will form a cycle else we can repeat this multiple times

No matter what is the order you choose for the edges to be add, it will always such edges that satisfy the statement — which is not making a new cycle into graph — such $$$u-v$$$ or $$$u-v'$$$ or $$$u'-v$$$ or $$$u'-v'$$$ satisfied $$$[1], [2]$$$

Therefor we will have the result of maximum possible edges can be added, by keep finding new $$$u, u', v, v'$$$ satisfied $$$[1], [2]$$$

By using brute-forces algorithm for each possible $$$i$$$ and $$$j$$$, we tested for all for cases ($$$i = u$$$ or $$$i = u'$$$) with ($$$j = v$$$ or $$$j = v'$$$) for any $$$u, u', v, v'$$$ satisfied, those who are not will not included in the result, those of which are needed will added to result

We don't need to redo the brute-forces every times we add a new edges, since those are which either already an edge or will make a new cycle, and will never satisfy again

Using Disjoint Set Union Data Structure, we can check for any 2 nodes that in the same connected component or not

Therefore we only need brute-forces for $$$O(n^2)$$$ with a little help of data structure $$$O(\alpha(n))$$$ amortized per query

Well some proofs for D1 did help in solving D2. For example my proof (and solution for D2) described here.

I have firstly met the error "wrong answer Token parameter [name=s] equals to "BRBRRBRBBR", doesn't correspond to pattern "[BR]{1}" (test case 4)" in task B. Funny, I just don't write 5 rows, but description turn me to deep frustration. ha-ha)

Video Tutorial C: https://www.youtube.com/watch?v=H8GDDIAOVdQ

Nice contest, kudos to the authors.

Problems were really nice. I would have expected a harder E and an easier D2 but it was good however. Congrats to the problemsetters.

Can someone please tell me when there will be -1 solution for problem C. And also Solution for C.

As per my intuition there will always be a answer.

Yes, there will always be an answer.

Case 1:jumping from n+1 to 1 Case 2:jumping from n to n+1 Case 3:jumping from i to n+1, then n+1 to i+1

How to see that there are no other cases?

you can only visit n+1 once

Consider the array A is a binary string. The following 3 cases cover all possible binary strings:

Answer exists for all 3

I made the same cases but got wrong answer on test case 3. Then I thought my intuition is wrong and gave up. Now I'm waiting system testing to be done to see where did my solution failed.

Did anyone solve E using Polynomial Multiplication and FFT ? I tried to implement it but it gave incorrect output for Sample Test Case 3 and then I could'nt debug it

Not good for div 2 contest

was it easy?

yes

WOW!! Score distribution was correct!! :p

Imagine being sponsored by Telegram and ITMO and still be laggy af

Does anyone have code for generating random forests so I can test my solution for D1 before the System Test ends?

SpoilerThanks.

can someone tell me how to do A XD?

u just use the & operation over all the elements...

bruh

It is simple problem hidden behind complecated words.

Since we can pair any element with any other element, we can remove any bit that is not set in all numbers. One simple way to find that bits is doing AND on all numbers.

Ha-ha, I used vector of vector of 31 bools (10^9 ~= 2^30) and then & each column to result vector. After that convert binary representation to long long.

Wow, it's really funny that it's equal to & over all the elements.

ans=a1&a2&a3&a4&...&an, since a&b is always not greater than a and b themselves.

I just apply operation on the whole array until i get an array with all element being equal.

First time in div2 I was able to solve till D (D1) . Though it was an easy contest overall i really enjoyed it after coming back from a long break.

D2 O(n * dsu(n)) solution from ♂Dungeon master♂

Maintain two dsu, one for each forest. Ass long ass there is a vertex that is not connected to vertex 1 in first graph and vertex 1 in second graph, connect it with vertex 1 in both forests. Then ass long ass there is a vertex X in first graph that is not connected to 1 and vertex Y in second graph that is not connected to 1, connect X and Y in both forests. Can be performed with two pointers, one find X, other find Y. Proof is on you, ♂slaves♂

man that is so easy yet so complicated :D

I'm surprised 5k people solved D1

Well, DSU is quite a common data structure nowadays most of the newbies and pupils know about them if they have gone through the kruskal's algorithm.

I expected the sulution to need some clever ordering of connecting the components. I still do not see how any order can allways be optimal.

So the problem is not to know DSU, it is to see (or just try) that brute force works.

Ya at first i tried to find any order to add the edges but then I just tried adding the edges manually and see what's the maximum number of edges i can add and after going through some random test cases i noticed that we have to add as many edges as we can until there is not any edge to add. of course by checking if adding that edge is not forming any cycle then i just implemented two dsu's and it worked.

D2 is truly interesting...Thanks for such problems

has anyone solved C using reverse edges and starting dfs from node n + 1? i just want to know if its a correct solution

it's not a dfs problem tbh, at least i didn't feel it, maybe there exist a solution but i think it makes this problem kinda harder in implementation

idea is: if u can't get from n+1 to 1 then u have to start from 1

if node n have path to node n+1 u can just go from 1 to n+1

if node n+1 have path to 1 then answer is: n+1, 1, 2 .. n

if it doesnt just find if there exist any point where node i has path to n+1 and n+1 has path to node i+1, then answer is 1, 2..i n+1 i+1 i+2 .. n

basically u just can't skip any node and can start either from n+1 or 1

oh well guess ill try this then thanks

Hey, I solved C using DFS with Toposort you can check it here 125991523.

Plz explain your approach

Also, was there a reason for $$$n \leq 10^4$$$ in problem C? Both the solution and the judge seem trivial to code in $$$O(n)$$$. Was the problem initially interactive somehow?

I will FST on C, I believe there was no pretest with all 1's... Why?

Edit: I failed indeed. Overall I enjoyed the contest. I think having some pretest with all 1's was needed. I was surprised because even if it is a useful test or not, it seems like if the input is an array where the values can only be 1 or 0, arrays with all 1's or all 0's naturally come to mind as tests. That's what I think, though I'm not a problemsetter. I hope someone finds this feedback useful. As I said, I really enjoyed the round anyway.

I think there is all 1's in 2nd pretest.

arcaea www

can we find number of solution of

a1 +a2 +a3 ... an = mwith upper bound on each variable in O(n) or O(n^2)I imagine using Multinomial Theorem and Polynomial multiplication we can do that in $$$O(N*MlogM)$$$.

I think I used the approach you are describing for problem E. I just could not think of anything so I came up with a kind of naive $$$O(nmlog^2(m))$$$ solution where we iterate through values from m to 1 and we calculate $$$f(i)$$$ which is the number of solutions that satisfies the first and second condition, and the gcd of all n numbers are a multiple of i. Then if we can efficiently find f(i), we can calculate $$$g(i)$$$ which is the number of combinations that satisfies the first and second condition, AND the gcd of all n numbers are exactly i, via the relationship $$$g(i) = f(i) - \sum_{j=2}^{m/i} g(i*j)$$$ which can be calculated in $$$O(mlog(m))$$$ due to the harmonic series. For calculating $$$f(i)$$$, you can use a knapsack dp that runs in $$$O(nmlog(m)/i)$$$ for each i using BIT. Then we obtain a solution $$$O(nmlog^2(m))$$$.

I think I solved it in $$$O(nm)$$$ using the formula given by inclusion-exclusion principle, as presented in this Stackexchange link.

Firstly, you precompute factorials to be able to compute binomial coeficient fast.

Secondly, you compute how many even-sized and odd-sized subsets give a certain sum $$$x$$$. We compute sets of different parity separatelly, to quickly compute $$$(-1)^{|S|}$$$. This is done by knapsack-like loop (remebember to increase upper bounds by one before running the loop):

Thirdly, now we know the

number of subsetsthat have given sum of $$$r_i$$$, so we iterate over those sums and change our`ans`

accordingly:`ans += (evens[x] - odds[x]) * binomial(n + m - x, n)`

(`+-1`

ommited for clarity).If you want to compute number of solutions to $$$a_1 + a_2 + ... + a_n \leq m'$$$ then it corresponds to $$$m$$$ going from $$$0$$$ to $$$m'$$$ in the formula from third step. Notice, that most of those binomial will be equal to $$$0$$$ if we process them in the reversed order (aka from $$$m'$$$ to $$$0$$$).

We introduce variable $$$help = 0$$$ and simply increment it by $$$binom(m' - x + n, n)$$$ and change our

`ans`

accordingly:`ans += help * binomial(n + m - x, n)`

.Unfortunatelly, I finished coding just after the round and now I'm impatiently waiting for system tests to finish and submit possiblity. #feelsbadman

Edit: It passed systests: Submission 126025440 :D

replying to find it later.

p.s -> how to find a particular comment easily later ?

There are those permalinks to comments (

`#`

symbol in the comment header, like https://codeforces.com/blog/entry/93788?#comment-829093). I guess you could save those in some file comments you find interesting.I, for one, have never needed to save a particular comment. With blogs / articles I intend to read later, in Chrome there is a list

`to read`

. You press the`favourite star`

symbol in the address bar and the option pops out.I figure the api design. But how will I know the ID is of your comment lol.

we could also use generating function to calculate the formula like this

basically we need to calculate

$$$[x^m] \prod_{i} \sum_{j = l_i}^{r_i}x^j$$$

$$$[x^m]\left( x^{\sum_{}l_i} \right) \times (1-x)^{-n} \times \left( \prod_{i}(1 - x^{r_i - l_i + 1}) \right) $$$

$$$[x^{m-\sum_{}l_i}] \times (1-x)^{-n} \times \left( \prod_{i}(1 - x^{r_i - l_i + 1}) \right) $$$

we can calculate coefficients of this polynomial $$$\prod_{i}(1 - x^{r_i - l_i + 1}) $$$

in total of $$$O(n * m)$$$ time using knapsack dp as we need atmost m powers.

as we need sum of all coefficient we need to multiply the equation by $$$(1-x)^{-1}$$$ again

so finally we need

$$$[x^{m-\sum_{}l_i}] ( (1-x)^{-(n+1)}) \times \left( \prod_{i}(1 - x^{r_i - l_i + 1}) \right) $$$

As power is fixed this becomes $$$O(m)$$$ to calculate, as negative binomial expansion coefficient for any power of $$$x$$$ can be represented in closed form

after using harmonic sum property for all calculating gcd = 1 total complexity becomes

$$$\sum_{i=1}^{m} calculate(x / i)$$$ = $$$ O(m.n.log(m))$$$

Yet I passed pretests printing -1 D:

https://codeforces.com/contest/1559/submission/125941230 — sent at minute 8, evaluated at minute 10 https://codeforces.com/contest/1559/submission/125944849 — sent at minute 13, evaluated at minute 17 https://codeforces.com/contest/1559/submission/125939758 -> https://codeforces.com/contest/1559/submission/125946723 !! — sent at minute 6, evaluated at minute 8

my submission: https://codeforces.com/contest/1559/submission/125937327 — sent at minute 4, evaluated at minute 20.

This weird late evaluation gave others a faster time to resubmit and eventually get accepted (and they did), but my submission was evaluated very late (although submitted very early!), and that gave me a great disadvantage).

Could anyone explain how this could've happened?

MikeMirzayanov sorry to tag you, but I don't want to lose rating over this. If my solution would've been evaluated instantly, it would've still taken me a minute to correct the problem, and now I am facing a 15 minute penalty (which normally should be 3, and now costs me a lot of rating) for very ambiguos reasons :(

I too had a queue of over 15 minutes for my C. Never seen a contest with this long queue ever being rated. I even had gotten up then thinking the contest would definitely get unrated. I don't mind having long queues for rated contests, just some standard should be followed. On one day a 15 minute queue makes unrated, on some other day it doesn't. RIP Ratings. I got quite nervous.

Problem C solution Problem D1 solution

Thanks for the great round! :) I like problem D2 very much, it's the most interesting problem I've ever seen :)

Can't I solve E with FFT or NTT?

I tried but it was still slow. I stress-tested on max constraints it took about 1 minute to execute on my PC.

Code: 126014845

Thanks.

Posted my solutions video: https://www.youtube.com/watch?v=U68R-0_6o9o

Now that's a contest (UPD: WOW FST on test 57)

In C. Mocha and Hiking, can't we just go from 1st village to final village through every village continuously? I mean, 1 2 3 4 ... n+1 Isn't this a solution? I know that if this is a stupid answer, but technically, it is satisfying the conditions, right?

Yes, if

`a[n] == 0`

you can just travel from 1 to n+1Shit. Sorry. I thought it included all the villages. My bad.

There is no first kind of edge between n and n+1.

Nice round. loved it.

what's this??? My solution for A and C was accepted and pretest(3) and pretest(15) passed for A and C respectively. But after system testing both become WA. If WA shows in the live contest then maybe I solve it with different approach.

it is because system testing is not yet finished.

But solutions which are in testing show in queue but my both solution is showing WA

It's typical of the system to show that, wait and it will turn back.

testing completed and I got one AC and one WA (even after getting pretest passed(3) )

Can anyone tell me why my submissions for D1 failed test6? This was the first submission 126009094

After solving A-D1, I got nothing to do.

Nice problems guys. Good team work!

lol. But it was easy. I just guessed the answer without even understanding why it works.

It's a good contest.

But I have a question:

Could anyone hack my code(D2): https://codeforces.ml/contest/1559/submission/126013197

Maybe it's easy to hack...

upd:hacked by peti1234

Unexpected verdict. I don't know what to do with this.

UPD: Hacked

i bricked this contest can i hvae some contrubtion points

It's a nice contest in general.But I should say that the difficulty between ABCD1 and D2E is like a cliff

Hoping to become pupil after this contest!

Tried hard to solve C, BTW nice round

When will rating be updated?

UPD: Ratings changed Thanks.

ranran(Diana) you take me away！

jiaxintang pi dou mei you yong !

wo men jia xin tang xiang yao zheng fa yi ge ding wan ren hai shi hen rong yi de!

heyheyhey，ranran(Diana)，my ranran，heyheyhey

Problem D1: This is the easy version of the problem. The only difference between the two versions is the constraint on n.

I have an AC code on problem D2 and the same code got WA on D1 ?!!

WA on D1: https://codeforces.com/contest/1559/submission/126029011 AC on D2: https://codeforces.com/contest/1559/submission/126029036

please note that your solution on D2 doesn't work on D1

Oops, My "Newbie" teammate is here for contributions LOL, I think I have to reach your rate to understand how you think before replying. (good luck in solving D1 & D2 Mr. Suliman)

editorial?

Is there any way to see the whole input of a test case. I really can't figure out what I did wrong here:

https://codeforces.com/contest/1559/submission/126029664

Edit: figured it out ;)

To not keep you waiting, the ratings are updated preliminarily. In a few hours/days, I will remove cheaters and update the ratings again!

Hi. Can you tell me why my rating decreased even after solving 2 questions of div 2 contest when I am in div 3. My rating decreased from 1078 -> 1060.

It doesn't matter how many problems you solved, what matters is how good you did compared with others and your current rating. You solved less problems or solved them slower than people with similar rating, therefore your rating went down.

So should I not give contests which are rated for div 1 & 2 for now? I am in div 3 right now and am still learning about competitive programming.

You should still give contests rated for Div. 2 and everyone, it's a great opportunity to practice your competitive programming skills.

Succeed！I am not a newbie now.

This contest was really an amazing experience. 1st time i was able to solve 3 problems of div 2

Great Round, thanks!

Thanks for the great contest. But unfortunately I got negative delta again :(

Problem D1 Video Editorial

Problem D2 Video Editorial

How a problem's difficulty is calculated?? like 1600 or 1700. I think this contest's D1 was 1600 but still 4500 plus participants solved it.

Received a mail saying my solution to B coincides with this, and I agree, it does. But I think it's a reasonable solution. Surprised there was only one such clash. Obviously don't have any evidence to "prove" that I didn't copy. Any ideas as to how I can refute this?

I think it's reasonable that two submissions is almost the same in a div2.B.

finaly experts had an oportunity of participating in a div3 round.

MikeMirzayanov I've got a message that the following solutions coincide: SunTakesTooLongToDie/125962649, Tony1234_/125963558

The code is obviously taken from this blog

I wonder why pypy3 slower than python3 when run a same code ? Isn't pypy an upgraded version of python? :O python3:https://codeforces.com/contest/1559/submission/126146836 pypy3:https://codeforces.com/contest/1559/submission/126146809

MikeMirzayanov, I've received a system message saying :"Your solution 125949787 for the problem 1559C significantly coincides with solutions spy20051623/125949787, ZeldaHuang/125971185."

The code is completely written by myself and I didn't send it to anyone during contest time. The problem is not difficult for Experts and Candidate Masters to solve, and it's possible to write similar codes.

In addition, I find many solutions similar to it, such as 125971285, 125972457 (I chose them just from common standing page 3).

I hope my "violation" to be checked again.

Thank you all staffs!

I used ideone.com to write my code in public mode and someone got the access and copied my code. This was a mistake that wont be repeated in future.

In the official standings how did rank 296 got 100+ delta while CMs above him with lower rating got lesser delta