Tutorial is loading...

Jury solution: 85699387

Tutorial is loading...

Jury solution: 85699884 Tutorial is loading...

Jury solution: 85699939 Tutorial is loading...

Jury solution: 85700178 Tutorial is loading...

Jury solution: 85700334 Tutorial is loading...

Jury solution (Solution 1): 85700515Jury solution (Solution 2): 85700518

Tutorial is loading...

Jury solution: 85700669Feel free to share your approach here!

My first submission for problem A in C++ was

ceil(1.0 * n / 2) and got WA. Does anyone have any idea what is wrong?

Why the downvote? Same is written here

Because here 1.0*n is evaluated first, the result of which is a float value and the divison of this floating value with 2 (integer type) results in a floating value. Hence, there is no precision loss due to divison.

You have to use (int)ceil(1.0 * n/2), because large values will be in Scientific floating-point notation

Try n = 1000000000, it outputs "5e+008"

Thanks

alternative of ceil is == >> **(n+1)/2 ** it will also give you a same answer

when n = 10^8 it will produce 50000000.0000 something like this but correct answer is 50000000

For some reason, a lot of people use the ceiling and floor function for libraries. There are many ways to mess up when doing this. I personally find it a lot easier to just use the fact that programming languages round down when dividing. Thus, to floor a/b you can do

`a/b`

and to ceiling a/b you can do`(a+b-1)/b`

Totally true. I used ceil once in Problem A and it didn't get accepted. I was not able to solve A in that contest. Since then I'm never using ceil.

At situations like these it's best to do (n + 1)/2

Here's a trick for calculating ceil of (a/b). Just do (a+b-1)/b. No, need to confuse yourself with ceil functions.

I also got wrong answer same way...you can use (n-1)/2+1 for avoid ceil()..

See the link for better understanding Click here...

int ceil2(int a, int b) { int c=a/b; if(a % b ! =0) c++; return }

bro, correct way to write ceil function is ceil((1.0 * n)) as this will convert our no. into floating variable and here the ceil function works properly hope you got my point :)

One should always point out that that's not the way one spells "Phalange" ;P

Dude, you lost the whole joke.

The whole point of the name is for fake identity theft. Spelling it right is not actually "right" (P.s if you are not friends fan, drop it)

You can use : double n; cin>>n; cout<<fixed<<setprecision(0)<<ceil(n/2);

Never trust ceil function of C++. It incurred me wrong submission many times.

just write n/2 and it will work without any problem. Also declare n as an int.

1

99999999

5e+07

I don't like the problems which can be done without using functions or loops

Try adding and subtracting using loops then and wrap these loops inside a function.

I think you understood the comment wrongly.

Problemsetting might not be the job for you.

You are green and you think you know a dime about problemsetting.

It shows the message that "You are not allowed to view the requested page" when trying to access the jury solutions.

Pathetic and Confusing problem statement for problem B. Shitty Contest

Problem B was pretty beautiful to my taste, even though I couldn't solve it,overall the questions were good.

For problem B I would have to agree the problem statement was pretty confusing, but others were fine.

ya the problem statement confused me too.. after getting the solution it was very easy

Yeah I had a hard time understanding how a shape spread in 2 rows in a calendar with x columns can be compared with the same shape spread in 2 rows in a y columns calendar

Link for the solution codes are not working right now ... please fix them.

Can someone elaborate problem D's editorial?

The idea is to fill out as many diagonals as possible, where each diagonal could wrap around the left/right sides. Filling in a diagonal increases each $$$R_i$$$ and $$$C_j$$$ by one. For a fully complete diagonal, $$$max(R) = min(R)$$$ and $$$max(C) = min(C)$$$, hence $$$f(A)=0$$$ if $$$k \% n = 0$$$. For an incomplete diagonal, you get $$$max(R) = min(R) + 1$$$ and $$$max(C) = min(C) + 1$$$, so $$$f(A)=2$$$.

what does this means "each diagonal could wrap around the left/right sides??

Wrapping around the left/right sides means that if the diagonal would go off the right side, it would continue on the left side. I think a visual example is the best explanation:

Step 0:

Step 1:

Step 2:

Step 3:

Thankyou so much.. got it now

i am still now getting...what if k%n is not zero

if k%n is not zero then the answer is always 2. e.g. in case if k=4 and n=3 using diagonal filling you will get:

110

010

001

you can try out the combination with different numbers to prove this inductively.

Thank you! This was very helpful than the editorial.

SO much better than the editorial. Thanks :)

Even hints will be better than the editorial given for the problem D

How did you get that intuition tho? Wrap around diagonals seems a notch above any intuition I got. The closest I got to is filling out the closest diagonal stripe in the grid —

Only later did I realise that it was wrong.

The diagonals aren't really the important part here, it's just easy to construct that way. These matrices are equivalent:

Think of it like filling a chessboard with rooks. If you place the first rook at $$$(r, c)$$$ , then you are left with $$$n-1$$$ rows and $$$n-1$$$ columns open (this is equivalent to 1 row and 1 column with a sum of 1 and $$$n-1$$$ rows and $$$n-1$$$ columns with a sum of 0). Since we're trying to minimize the difference in max/min sums, it's better to place the second rook on an open row and column. This will leave you with $$$n-2$$$ rows and $$$n-2$$$ columns. Keep following this pattern, and you'll end up with $$$n$$$ rooks on the board.

So after filling in n rooks (as per the finding open column open row method), if k is greater than n, how would the remaining rooks go into the board? Or they could just be filled randomly?

You would treat the $$$n$$$ spaces that the first rooks take up as unavailable spots, then place the next $$$n$$$ rooks in the same way as the first $$$n$$$ where only 1 new rook per row and 1 new rook per column. The important part is that you don't place 2 on the same row/column when they're part of the same "rook group".

The following is not the way written in the editorial, but I have done it in my solution. Firstly, lets observe that if $$$k$$$ is divisible by $$$n$$$, the answer is $$$0$$$, and when it is not, the answer is $$$2$$$.

ProofThere are $$$n$$$ rows and $$$n$$$ columns. ($$$k$$$ % $$$n$$$) is always less than $$$n$$$. Thus each extra integer, can be fitted in these $$$n$$$ rows and columns.

Now, how to fit the integers?

What I did was first insert $$$1$$$s in indexes $$$[1, 1], [2, 2], ...., [n, n]$$$.

In the next iteration, I inserted $$$1$$$s in indexes $$$[1, 2], [2, 3], ...., [n, 1]$$$

Like this, we have $$$(k / n)$$$ iterations.

I had put the extra $$$(k$$$ % $$$n)$$$ 1s simply as if it was the $$$(k / n) + 1$$$ th iteration, and only the first $$$(k$$$ % $$$n)$$$ 1s need to be inserted.

My submission for reference: 85648287

I didn't understand how you put remaining

`k%n`

one's please explainWe put the ones in $$$k / n$$$ iterations, and thus the remaining ones will be $$$k$$$ % $$$n$$$. Note, over here the answer is $$$2$$$. The remaining ones should be lesser than $$$n$$$, because if not, then we could have added another iteration to use the $$$n$$$ ones. As the remaining ones are lesser than $$$n$$$, it means that they are lesser than the number of rows and columns. Thus, we iterate for the first $$$k$$$ % $$$n$$$ rows and columns, in the next iteration, and initialise that point $$$a[row][col]$$$ to $$$1$$$.

There is no need to do seperately for

`k/n`

and`k%n`

we can fill the matrix in only one loop going from`[1,k]`

Yes, but don't you think the implementation will go for a toss? I am not telling you that my solution is the best, just telling you from my perspective, I code a solution which is the easiest to implement.

That formular is wrong, isn't it? $$$f(A)=2(1^2+1^2)$$$

I implemented AC that as

It says f(A)=2. Which is the result of (1^2+1^2). Correct me if I got it wrong.

It says f(A)=4. Which is result of 2*(1^2+1^2)

Or what is the 2 good for?

Not logical but grammatical mistake.

No, he means to say that f(A)=2, which comes from 1^2+1^2, that's why the brackets. The brackets are not indicating multiplication.

I think B and C should have been swapped. Thanks for fast editorial though.

I think D was little bit similar to 1360G - A/B Matrix and thank you for a nice contest.

I actually copied some code from that problem when the answer is 0

I actually copied almost the whole code. See this submission 81245465 for problem 1360G - A/B Matrix and my submission 85681253 for problem D.

That's why i was able to do this problem otherwise it would be hard to come up with such algorithm during contest.

Little bit? Dude for k%n!=0 you just add one for loop and you get AC.

I need a little help here. I do remember solving that problem A/B Matrix. I read that question again now and I cannot figure out how to solve it. I have no clue how I had up solved. It kind of made sense when I saw the explanations but now its just gone. This is my problem. I solve problems on various judges on various judges but I forget topics too soon. Can you tell me how you remember those ideas? Or do you make notes of these? Or that is what your mind tells you because you have practiced a lot of similar questions? Regards.

Wow man you have such a nice memory XD I think this is the third time I have seen you remembering similar old problems. No offence tho :)

pattern in questions!

One of the Best editorial for beginners like me.

I know it's too much to ask from anyone , but i will be highly obliged and grateful to the one who will look at my submissions of D to see why it fails to pass test cases . My submission link : https://codeforces.com/contest/1371/submission/85694260

testcase

`4 7`

your answer

correct answer

Thanks a lot man! i understand now . Have a great luck and wonderful day/night ahead ! :)

I up-vote this for your humbleness

Could E be solved, under the same constraints, if P was not prime ? I'm curious about it.

if $$$p$$$ is not prime, you should replace it with the minimum integer $$$k$$$ such that $$$k!$$$ is a multiple of $$$p$$$

upd: nvm, it's wrong

reasoning

the answers are such $$$x$$$ that some $$$P[i]$$$ can be chosen in $$$\geq k$$$ ways

you can prove that, if this condition is true, for $$$1 \leq y \leq k$$$, some $$$P[i]$$$ can be chosen in $$$y$$$ ways, so $$$f(x)$$$ is a multiple of $$$k!$$$

upd: nvm, it's wrong

though i suck at patterns and constructive algos , but still managed some positive delta ....xD P.S — thank you for the contest ! @physics0523

Div2 rounds are now tending towards Div3! who else feels same?

True

May be it is because it was physics0523's first round that's why..!

No Actually u r an expert!! thats whyIt's not about individual rating, just look at submissions on problems till E!, it is rare.

if u r finding these problem easy means u r improving

Question B should have been better explained

To be honest, felt like a div3. except the last problem. For others, no. of submissions is almost same as on a div3.

You can't just compare no. of submissions in a div3 contest and a div2 contest. In a div2 contest there are more high rated coders than in div3 contest. The expected rank of a 1500 rated coder in Codeforces Round #654 (Div. 2) was 4299, and his rank would be 2894 in the contest Codeforces Round #650 (Div. 3)

Problem is easy, coders are not very skilled, no of submissions are X. Problems are harder, coders are also better skilled, the problem might get a similar number of submissions.

I used this website to calculate expected ranks.

To compare the div3 rounds and div2 rounds, compare their problem difficulties, not no. of submissions.

In C's editorial shouldn't it be m<=min(a,b) rather than m<min(a,b)?

yeah, i am thinking about that. it should be m <= min(a, b)

You are correct for 8 3 4 3 answer should be "YES".

Why can't someone who knows English go over the problems and correct bad translations? It's so hard to read these problems with so many out of place commas, and completely messy sentences.

Editorial Problem E1 You can find the value of f(x) by the following simulation:

First, let v={the number of enemies they have strictly less than x candies}, ans=1. Then do the following steps for i=x,x+1,...,x+(n−1). v=v + {the number of enemies they have exactly i candies} ans=ans∗v v=v−1

Is it just me or was this part not really that intuitive?

Yeah,

I still haven't been able to fully understand the Editorial of E1.

I understood the statement by seeing some solutions but the reason it works is still a mystery to me

Found a good explanation https://codeforces.com/blog/entry/79624#comment-654544

for a give n x we are calculating all permutations v = all value less then x we can always put all v values in first place x could always defeat these values and will become x+1 now in second position e can select v -1 as out of v we have already put 1 value at first place but, we can also put those values which are equal to x+1 thats why for x+1 we added v += the num of enemy that has exactly x+1 candies. so we should multiply our ans with this num thatis ans = v*(v-1 + k) where k is num of enemies that have exactly same candies as x+1 and v is numbver of candies less then x, and so on.

I did the same thing that's mentioned in the editorial for E1. But I am getting WA for test 5. Submission.

problem B was little difficult to understand

Can someone explain C with examples? And what tags do these types of problems come under?

Well, check the tags in the problem!

Notice Second Type of guest always consume the one which is

smallin quantity. So if v > c then only chocolate will be consume by Type 2 guest or if c > v then only vanilla is consume and after consuming either the inequalityv > corc > vwhich one is there will still hold true. So if min(v, c) >= m then we can always allocate 1 cookie to each type 2 guests.Thank you, that helps!

hello

In C part I used :

if(Math.min(a,b) >= m && Math.max(a,b) >= n){

and got a wrong answer. Could anyone please tell me.

Firstly, a + b should be greater than or equal to m + n. So, check that first. The second condition inside the if statement is not needed. See the above comment by nicknamee12 for explanation.

Thanks I got it now.

Finally understood B, thanks!

At least provide some explanation for problem D. We can see the other's submission if we want to see code.

You have to note the pattern. I will explain this by an example.

For k <= n it is simple : We have to fill only along the diagonal.

For k > n :

One thing that is sure : Putting 1 along diagonal is always optimal choice to minimize the final ans.

Step 1. Fill the diagonal

Step2 .

Step 3. See the pattern

Step 4. We will repeat what we have done in step 2, and step 3 till we still some entry to fill.

Repeating pattern :

Repeating pattern:

And finally :

Trick that I use to print is r-c (or c-r) is constant for a particular diagonal.

Understood now. Thank you

Any reason for making problem C around the same difficulty as a typical Div2 A?

In D there should be either

(1^2 + 1^2)or2 * 1^2, not2 * (1^2 + 1^2).My poor brain still didn't understand the solution to problem B. Could anyone be kind enough to explain it a bit more clearly?

problem C was fantastic for me.I spended too much time for it.when I finded it's solution I just surprised.

The problems were great. I thought C's solution would be much tricky..But now i find that I should have thought about it for a little more time..lol

First 4 problems felt more like div3 contest only maths and implementation kind. Or may be I am improving.

You are improving...

Spoilerin

MathsHow can you judge a problem without taking part in actual contest.

Judging a problem without taking part in contestBy solving it after the contest ends.

solving a problem after the contest is much easier than during the contest. Have a look at problem C, it seemed difficult to most of the people appearing for the contest but after upsolving they found that very easy.

@aryan12 means solving after the contest but with the below constraintNOT LOOKING AT THE SOLUTIONS BEFOREI had registered for the contest. But due to some techincal errors I was unable to participate in the live contest. But I solved the problems later without watching the solutions. And I was giving my opinion about the contest and about the level Div2 actually is.

Felt like giving an Aptitude test!

Yes, At least for the first 4 problems Never thought pattern recognition and printing will help me solve my firsts Div2 D problem :D

https://codeforces.com/blog/entry/79541?#comment-654173 This single comment has a better explanation for A, B, C than this editorial

lol , how's that even an explanation?

Just the same way how the below code is an explanation ~~~~~ let p=0 , q=0

Total complexity: O(n2) ~~~~~

Can Any One Explain me Solution of

problem:E1? Because I am still not able to understand it through Editorial. Please Explain if You know .How we can find the value of f(x)???

My reasoning was: Sort $$$a$$$. Fix $$$x$$$ $$$(1 \leq x \leq 2000)$$$. Lets see how many elements we can pick to be $$$P_0$$$ in a valid permutation. Find the last $$$i$$$ such that $$$a_i \leq x$$$. Then we can let $$$P_0 = a_0 \text{ or } a_1 \text{ or } ... \text{ or } a_i$$$. Thus there are $$$i + 1$$$ ways to pick $$$P_0$$$. To pick $$$P_1$$$ we do the same thing, i.e., find the last $$$i$$$ such that $$$a_i \leq x + 1$$$. Then we can let $$$P_1 = a_0 \text{ or } a_1 \text{ or } ... \text{ or } a_i$$$ except that we used one of these elements in choosing $$$P_0$$$. Thus there are $$$i + 1 - 1$$$ ways to pick $$$P_1$$$. Repeat this process for each $$$j < n$$$. If you get to some $$$j$$$ such that the number of ways to pick $$$P_j$$$ is divisible by $$$p$$$, then $$$x$$$ is not valid. Otherwise it is valid. The time complexity is $$$O(\max(a)n\log(n))$$$. This definitely won't pass E2 though.

Edit: My contest code (disregard the freq array, it is unused): 85694840

I have done the same thing as you. Can you tell me what is wrong with my code?

https://codeforces.com/contest/1371/submission/85673827

probably overflow? You never modded your ans variable, but it can be as big as $$$n!$$$

Lol you're right...was so close to solving 5 for the first time :(

Thanks for sharing your approach mate...idk why but was struggling to understand the editorial...I found your approach more insightful. Thanks. Appreciate it! :)

You can find the minimum x such that f(x) % p != 0, then you can do binary search on your same approach for maximum x. And print from minimum to maximum.

Yep that's the editorial's first solution for E2. The editorials $$$C_i(x)$$$ function is the number of ways to choose $$$P_i$$$ if the starting amount is $$$x$$$. I implemented it and it works in $$$O(\log(1e9)n\log(n))$$$ (~100ms).

During solving this question on practice. I just guessed(weak guess) that maybe the answer would be a continuous segment. So, did a binary search. Still, not understanding why it worked?

Its because of these two simple observations that the editorial states:

$$$1.$$$ For each $$$x$$$, for each $$$i$$$ $$$(0 \leq i < n - 1)$$$, $$$C_i(x) \geq C_{i + 1}(x) - 1$$$, and $$$C_n(x) \leq 1$$$.

$$$2.$$$ For each $$$i$$$ ($$$0 \leq i < n$$$), for each $$$x$$$, $$$C_i(x) \leq C_i(x + 1)$$$.

From $$$(1)$$$, its clear that if $$$C_i(x) \geq p$$$ at some $$$i$$$, then there must be some $$$j \geq i$$$ such that $$$C_j(x) = p$$$.

Since $$$p$$$ is prime, $$$p \mid f(x)$$$ if and only if $$$p \mid C_i(x)$$$ for some $$$i$$$ $$$(0 \leq i < n)$$$. I think this is called Euclid's Lemma.

Thus from $$$(2)$$$ it is clear that $$$f(x) > 0$$$ and $$$p \mid f(x) \implies f(x + 1) > 0$$$ and $$$p \mid f(x + 1)$$$. Therefore there will be a smallest value $$$mn$$$ such that $$$f(mn) > 0$$$, and there will be a smallest value $$$mx \geq mn$$$ such that $$$f(mx) > 0$$$ and $$$p \mid mx$$$ and each $$$x \geq mx$$$ is not good. Thus binary search works. The answer is just the range $$$[mn, mx)$$$.

I can`t understand the first condition.

suppose x = 3 and i be 2, then power at i=2 will be 5, so C(i,x) will give how many values are there less than or equal to 5, suppose this value is 10, out of this 2 have already been used at index 0 and 1 so value should be 8.

Now at i+1 we will have value 6 and then we will ask how many values are there less than or equal to 6, this will incude all the values less than or equal to 5 and the values equal to 6, let it be 15 (10+5(freq. of 6)), as now 3 value have been used value value here will be 12

so we have C(i,x) = 8 and C(i+1,x) = 12, then how is the above condition satisfied ?

Thanks a lot, for some reason I wasn't able to understand this via the official editorial. You saved me from a headache :P

Ha, thank you! Good explanation.

I'm trying to understand the editorial implementation now. But I can't figure out what the

`bk`

array means. Can someone give me an intuition?Could you please explain more clearly?I didn't understand why did you take x in such a way?

Can someone please help me with this submission for E1. It gets WA on test 9.

EDIT :I found the mistake. I was usingsetdata structure earlier to get the count of elements less than or equal to $$$x+i$$$ which is wrong as it only stores unique elements and thus might give me wrong count values in case of duplicates.Can anyone figure out what's wrong with this solution? (Problem D). It's showing WA on Test 3 but I am not able to figure out why. https://codeforces.com/contest/1371/submission/85682293

how can one arrive to the approach used in problem D. I mean is it really too intuitive? If yes, then can anyone share similar problems for practice.

My idea was to flatten the board into a line of length $$$n^2$$$ and spread the ones "evenly" across the grid. That is, put a one at positions $$$0 \bmod n^2, (n + 1) \bmod n^2, 2(n + 1) \bmod n^2, ..., (k - 1)(n + 1) \bmod n^2$$$. Then you can easily convert the linear position to an $$$(r, c)$$$ coordinate by the formula $$$coord(x) = (x / n, x \bmod n)$$$. I didn't prove it rigorously, but it got AC: 85675022

I believe a similar technique can be used to solve this problem: https://codeforces.com/problemset/problem/1360/G

Thank you gentleman for this brilliant approach, now I am wondering how to prove and why this works. If possible can you please elaborate? Thanks a lot.

One. No square will be hit twice using this method, and all $$$n^2$$$ squares can be hit if needed (i.e. when $$$k = n^2$$$).

Proof: Since $$$\gcd(n + 1, n^2) = 1$$$ (easy to show, I hope you can figure it out), the method will hit exactly $$$\frac{n^2}{gcd(n + 1, n^2)} = n^2$$$ before returning to its initial position (0), so the claim holds. This formula follows from a theorem of group theory which states that for any positive integer $$$k$$$, for each $$$x \in \mathbb{Z}_k$$$, the smallest integer $$$d$$$ such that $$$d \cdot x \equiv 0 \pmod{k}$$$ is $$$d = \frac{n}{\gcd(x, n)}$$$. An equivalent way to say it is $$$d$$$ is equal to the number of distinct elements in $$$\mathbb{Z}_k$$$ you get by repeatedly adding $$$x$$$ to itself modulo $$$k$$$. This theorem appears surprisingly frequently on Code Forces for some reason (probably due to its relation to cycles and permutations).

Two. I'm still not sure how to rigorously prove that this method minimizes $$$f(A)$$$ ... Im guessing it has to do with the fact that also $$$\gcd(n + 1, n) = 1$$$.

Shouldn't it be $$$m \le min(a,b)$$$ for problem $$$C$$$?

In E2 for solution 1 :

"if C_i(x) exceeds p, then there will be some C_j(x) = p as C_i(x) decrements by at most 1".

Why is this necessary that some C_j(x) will be equal to p?

what if C_i(x) does not decrease but only increase? Can someone explain?

It has to decrease towards the end. If there was no constraints on choices at each turn and you had 5 options you end up with 5*4*3*2*1 choices. The constraints can never increase the number of choices for any turn but can reduce them so any 'good' x will ensure that you never have p or more choices on any turn.

Cn(x) has to be one bcoz total n choices. So,if some c_i > p and each c_j can atmost decrease by one then p+a....p.....1

The problems were wonderful. I loved them all.

However Ruby 2.7.1 (which was upgraded from 2.0.0 recently) is too slow on Codeforces. My O(1) (I mean, O(t)) solutions don't pass! https://codeforces.com/contest/1371/submission/85638755 Same code with same input passes in 150ms on Macbook Air and 75ms on AtCoder!

It seems that any Ruby2.7.1 submission takes 900ms at least. (Even if 1 line do-nothing code) So I guess Ruby2.7.1 on Codeforces has somewhat wrong settings.

And I found that previously passed code makes TLE now with Ruby2.7.1.

Please let me know if any better place to report bugs to Codeforces. Thanks!

Shouldn't it be m <= min(a,b) in place of m < min(a,b) as written in the editorial

Yes

For all those who want to understand how the solution for D is working, have a look at the comments of my code.

I can't understand the solution of C except the base condition n + m <= a + b. And how to think solution of this kind of problem, I tried different types of conditions but couldn't unite them.

Second type guests always choose fewer remaining type of cookies out of 2 types of cookies, so after they choose, the fewer number of remaining cookie is always reduced by 1. First type guests will not get angry if at least 1 cookie left, so the best thing to do is to put them on the back burner.

The contest was mostly logic based for question A to E1 and not much related to algorithms and data structures.

In problem E2 (Asterism), after we have calculated b_i, how are we able to find all the "good" x's in linear time?

Your algorithm for comprehending D is funny, Mr physics0523 ;P

For the whole round, I thought that in problem B, we have to count only one pattern for the row that completely divides the total number of days.The problem statement could have been written more clear.

still can't solve C :( if we call type 1 guest before type 2 guest then it won't decrease min(a,b) right? but in editorial

`if there is a type 1 guest before a type 2 guest, the type 1 guest have a possibility to decrease the value of min(a,b) unnecessarily`

If a = b and we call two guests of type 1, they will decrease the minimum of a and b.

I have a question, if you can't find some order using the greedy approach presented in the solution, how will you prove that you can't find any using a different approach ?

Yes,this statement is conflicting. You can understand it by realising that at any moment,type 1 guest tries to eat the cookie which is more in number and type 2 tries to eat cookie which is less in number.If number of cookies are same the type 1 eats chocolate cookie and type 2 eats vanilla.

got it, tysm! we invite type 2 guests first because they take minimum of vanilla and choclate and in the end we invite type 1 guests and check if we have required remaining dishes.

This statement is genius! Thanks

I'm not sure whether this was mentioned before, but there is a DP solution to E1 which generalizes to E2. I am going to present a handwavy explanation of it:

85709850 Here, dp[i] denotes the number of valid permutations of the i smallest elements. From there, we want to insert a new element in these permutations, which does not make any of the existing duels worse, as our candies are monotonically increasing, so we just find the number of positions, where we can put the new element. If we had x candies at start and our new duel is the k-th one, then we will have x + k — 1 candies, so this must be at least the new amount of candies.

Now moving on to E2:85708674

Knowing the recursion from the dp, we can now see that if we have too few candies to win a duel, f(x) is 0, and if we have at least as many candies at start than the p-th smallest enemy, we will also have trouble, because that multiplies our dp by p. So we can find the min and max candies easily, and now we just want to quickly decide whether any of our other products are zero. The parts in the dp product before the p-th smallest element cannot be divisible by p, as they are positive but smaller than p, so we just take the elements from the p-th to the n-th and see whether they give a zero residue with p, granted that we have less candies than them when starting the fights (this follows from the enemies being sorted and us having at less candies than the p-th smallest enemy). This can also be done linearly.

E1, "Yuzu should have at least $$$m-n+1$$$ candies initially to win all duels."

Isn't that wrong? Having that much candis garanties that not all duels get lost, but to garantie to win all duels Yuzu must have $$$m$$$ candies. Since all a[i] can be equal $$$m$$$.

I mean, a[] is not a permutation or somehow else pairwise distinct.

bro see my solution for that problem it is easy to understand.

I did not solve it because I was not able to come up with formular to calculate f(x) for any fixed x. And, honestly, I am still not sure about that after reading the editorial.

:D hmm editorial is not good this time

Agreed, they did not explain the solutions well, they just wrote the pseudo-code for the problem.

Btw, I solved E1 in contest with basic principle of counting, for a fixed x, I counted no. of ways to fill 1st place * no. of ways to fill 2nd place and so on up to n places. My submission 85691626

If you have $$$m$$$ candies, you can definitely win all duels. What the author meant is this:

Let $$$x$$$ be the smallest number of candies you need to guarantee a win, then $$$x$$$ $$$\geq$$$ $$$m - n +1$$$. Had it been less than that, you'll have maximum $$$m-n+n-1$$$ candies against an enemy with $$$m$$$ candies.

Ok, what he meant is "Yuzu should have at least $$$m-n+1$$$ candies initially to not loose all duels."

Having m — n + 1 candies obviously does not guarantee winning all duels. eg — 5 5 5. Maybe, he meant that if we have candies less than this, then we are definitely going to lose.

Hi! By at least it means that any x below this and we're certain that f(x) = 0, so we don't need to check below that. It's not actually necessary to do this if formula for finding f(x) handles all cases tho.

In tutorial of C it should be m <= min(a,b) instead of m < min(a,b) for answer to be "Yes".

B. Magical calender As was said before, she considers only shapes, there all cells are connected by side.

but for n=1 , r=any number output should be 0 according to question description. cuz 1 cell has no side by side connection

The test cases for E1 were such that I got a TLE when I solved in O(n*n*logn). Till today I thought log(n) does not make a difference. Got to learn something from the question.

My O(n*n*logn) solution passed well within the time limit 85702061

I think I have done the same thing. If possible could you give it a look? https://codeforces.com/contest/1371/submission/85691445

ash_vs_ash you are passing vector to function search() by value, therefore it is taking extra O(n) in every run and maybe that is why you're getting TLE. Try passing vector by reference to your function.

It worked like a charm. Thanx man.

Why am I getting

WAontest case 6ofE1: 85714673UPD:Fixed it@author can you tell me 26th test case of 3rd problem.

I can't understand how is the formula for "Magical Calendar" question derived? Plz help!!

Can anyone please tell me what is wrong with my code for E1?

https://codeforces.com/contest/1371/submission/85673827

This is ecnerwala's solution for Problem D, can somebody help me in understanding how this solution works and what was the intuition behind that :)

It's the same diagonal construction as the editorial, but computed in a different order. For each cell, we compute which diagonal it's on and use that to decide whether it should be set or not.

First three problems in O (1) and then we see D,E of O (n2) ... It was more like division 3 contest.

This editorial is so hard it needs another editorial. Seriously, it was easier for me to solve problems than to understand this, not even saying anything about jury code quality. Some standards for writing editorials are needed.

I think it should be forbidden to have editorials in the form of pseudocode or formal algorithm description. I read editorials to understand how to arrive to the solution. If I want a formal description, I can just open anyone's submission

Pseudocode might be fine, if it is easier to read than actual code. The actual solution must be present anyways. What you might need is a system of "tips", that describe a way of thinking that could lead you to the solution.

Unfortunately, most of the time this is not the case and we see "just do this" or "we can prove that this works" types of arguments. Even just checking that a thing works might be of little value for me, because during contests you are not checking solutions, you need to come up with one.

binary search solution of E2

Spoiler85716743

Hello, can anyone tell me how long we have to wait to see the change in our rating, system checking is done hours ago

they have been updated

just now I think thanks man

I try to avoid library functions like ceil and pow because for higher numbers as input they give unexpected outputs.

Thanks for the contest and a fast editorial. Was able to solve two problems today. Hopefully will improve with time.

If you prefer video explainations, I go over everything (except a rather handwavey description of E2) at the end of my screencast of the round.

Can anyone please explain how this solution works for 1371E2 - Asterism (Hard Version). Thanks in advance.

Taken from85678389

I have a proof that the solution must be a contiguous sequence of numbers here: https://codeforces.com/blog/entry/79624?#comment-654797

He found the min and max numbers of the subarray, then printed every number in between.

It seems that while I used binary search to find the maximum bound of the subarray, he found a cleverer way just using a for loop.

I have an interesting solution for E2 using binary search.

It relies on the fact that the answer will always be a contiguous sequence of numbers. I have a proof of that fact below.

85713453 (find minimum value in easy O(N), binary search to find the max value that works, print every number in between).

Proof:

Claim: f(x) can be written as some factorial, lets call it X!, multiplied by some mumbers <=X.$$$f(x)$$$ is divisible by all primes $$$<= X$$$ and not divisible by any primes $$$> X$$$, where $$$X$$$ is the number of possibilities of the index with the most possibilities.$$$X_a \le X_b$$$ for all $$$a < b$$$. This is because having a greater value of x in f(x) will never result in less possibilities for any position.I also came up with a similar idea when upsolving, code with brief comments: 85718984

The upperbound is the largest number that never has $$$\geq p$$$ options, and every greater number will surely hit $$$p$$$ options "on the way back down" as they run out of options

Thanks. That really helped.

My solution and explanation for 1371E2 - Asterism (Hard Version)

The number of permutations for a given $$$x$$$ can be written in:

Since $p$ is a prime number, in order that $$$f(x)$$$ cannot be divided by $$$p$$$, we must ensure that $$$(\sum_{j=0}^{n-1}a_j\leq x+i)-i<p$$$ holds for every $$$i$$$. And in order that there is a possible permutation, we must ensure that $$$(\sum_{j=0}^{n-1}a_j\leq x+i)-i\geq1$$$ holds for every $$$i$$$.

Proof: Let's denote $$$(\sum_{j=0}^{n-1}a_j\leq x+i)-i$$$ as $$$g(i)$$$. We can find that, since $$$\sum_{j=0}^{n-1}a_j\leq x+i$$$ never decreases, so if $$$g(i)$$$ decreases, it can decrease at most $$$1$$$. Another observation is that $$$g(n-1)=1$$$. So if there is $$$i_1$$$ such that $$$g(i_1)>p$$$, there must be $$$i_2>i_1$$$ which satisfies $$$g(i_2)=p$$$, which makes $$$f(x)$$$ divided by $$$p$$$.

So the problem is now: how many $$$x$$$ are there that satisfy

Since $g(i)$ increases with x, the answer must be a continuous segment $$$[lo..hi]$$$.

Now we sort the array $$$a$$$ in the ascending order.

For $$$lo$$$, we have

For $$$hi$$$, we have

So the final answer is the segment

Code: 85684812

I can't understand the last two steps of your solution.Why Why does this formula work ? ∀i,hi+i<ai+p−1⟹hi<min(ai+p−1−i)

$$$\forall i, hi+i<a_{i+p-1}\implies\forall i,hi<a_{i+p-1}-i\implies hi<\min(a_{i+p-1}-i)$$$

I get it! Let cnt[x] be the amount of number which is equal or less than x,then we have ∀i,0<=i<n => cnt[x+i]-i<p => cnt[x+i]<p+i => x+i<a[p+i-1] => x<a[p+i-1]-i => x<min(a[p+i-1]-i). Thanks for your solution!

anodiebird How did you come up with,

`x+i<a[p+i-1]`

, steps before this one are clear. Can you please elucidate this one?⑴ x+i<a[p+i-1] => x<a[p+i-1]-i ⑵ a+b<c => a<c-b If you look at these two equations，you will see both of them are the same form,in the first equation I just change the position of i,in the second equation I just change the position of b.

anodiebird My doubt is that how did you transition from

`cnt[x+i]<p+i`

to`x+i<a[p+i-1]`

Because we have sorted the elements of a[].If index i begins from 0,then we can see cnt[a[i-1]]=i,and you can deduce that p+i=cnt[a[p+i-1]].So let's substitute that into our expression:cnt[x+i]<cnt[a[p+i-1]] => x+i<a[p+i-1] => x<a[p+i-1]-i

If author's approach for problem F is unclear, then you may check my solution. 85721976 Imho problem F is the only problem that I've found interesting in this contest.

It is probably because it's his/her first contest but the editorial solutions to D and E1 are the worst. There is no reasoning/intuition given whatsoever. Please take it as constructive criticism.

My solution for problem F : 85724126

Nice! Thanks for your explanation. It really helps.

## My solution to 1371F - Яростный гром

## Helping information

Obviously, this is a segment-tree problem. Then what do we need to maintain in each node?

In my implementation, the following information is maintained:

Then the answer to the query within a node becomes $$$\max(hi, ll, rr)$$$.

With such information, node combination and node flipping becomes easier and clearer.

## Node flipping

## Node combination

## My code

85729867

Thank you so much for posting this. I was unable to understand after reading the editorial.

why i am getting run time error, i am unable to find that?

## include<bits/stdc++.h>

using namespace std; int main() { int t; cin>>t; while(t--){ int n,k; cin>>n>>k; int res[n][n]; memset(res,0,sizeof(res)); if(k%n){ cout<<"2"<<endl; } else{ cout<<"0"<<endl; } int p=0; int q=0; for(int i=0;i<k;i++){

}

You set

`res[p+1][q+1]=1`

while`p+1`

can be`n`

.thanks lucifer1004 that works.

What obfuscation software was used for the jury solutions? It is certainly very effective.

For E2 problem testcases are week: take an example: 9 8 3 5 9 11 15 17 19 20 23

answer is -> 0 but some codes of another coders gives output: 15,16,17,18,19

Invalid testcase. 8 is not a prime.

Can someone please provide a rigorous proof for Problem C as to why the order of guests where first all type 2 guests come and then type 1 guests come, is the most efficient ? I could not understand the explanation in the editorial.

I don't have a rigorous proof but you can think in this manner that guests of type one can always adjust to any kind of case whether only vanilla,chocolate or both (provided the number of guests of type ones is less than equal to the total number of cookies) so our number of type of cookies is independent of guests of type one.

It's not so for the guests of type two since they always choose the lesser type of cookies so if you feed the guests of type one before guests of type two it's going to be less optimal.(more chances of failure of satisfying the condition. Our problem asked to print yes even if a single situation could allow all guests to finish them off so always looking for most optimal way)

Thank You I think I get it now

Check out the video editorials:

E1. Asterism

D. Grid-00100

C. A cookie for you

B. Magical Calendar

A. Magical Sticks

Can you please make a video on E2

Are you color blind?

No

During the contest, I misread the question B as the total number of shapes of length n one can get if

for any block, there is at least one block that shares one side with that block, thereby removing the problem constraint ofn consecutive days. I still haven't been able to come up with any solution! Could anyone help with that?UPD: Got it

What is wrong in my code for Asterism(Hard version) problem, I mean I am getting WA on test case 11, can someone help in finding the wrong logic that I am using in my code https://ideone.com/iUJnps

Are the ai values in E1 and E2 distinct or can some of them be the same?

Values can be same

No idea if this is mentioned before but I think there is a much cuter solution for F.

Notice that the balls are seperated by <> only. We only need to store leftmost, rightmost and longest distance between <> in a range. For the reverse it is just ><.

code: 85723638

Thanks! I was able to solve this problem with your idea.

By the way, I noticed that you use a MAX/MIN template for multiple elements. You don't actually need that since the following is valid in C++

`x = max({a,b,c,d....})`

(We just need to use {} for multiple elements)Or is there some other benefit that I don't know of?

I didnt know that was valid syntax .-.

I have a question for the B task. What is the b variable?

there is no

`b`

variable in problem BI am a newbie Can someone explain me ( in problem D ) Why is this line included in the Jury solution ** arr[i][n]=0;** Thanks in advance

It has been used for string termination. This may help you.

Got it , I don't need to do that if I am using an Integer array right ?

Yes

Thanks a lot brother

I tried to implement problem D 1371D - Grid-00100 but am unable to do that. Can anyone tell me where I am doing wrong?

Here's my codeP.S. : This code is Python Equivalent of dgupta0812 's code. I saw this in the comments.

Here's his code85708642

Here is my approach and explanation for E1[problem:1371E1]

As Yuzu starts with

`x`

candies, we know the number of candies in each position of the array, i.e,`x+1,x+2.....,x+n`

. This means at`ith`

position with`i`

starting from 0, the number of candies at that position must be strictly less than`x+i+1`

.Sort the array and iterate from the back. Let

`m1`

be the maximum number. Now, how many positions can`m1`

occupy? It is`x+n-m1`

as`m1`

can only occupy positions having candies greater than`m1`

.Now, let

`m2`

be the next(second maximum) number. It can occupy positions`x+n-m2-1`

. We subtract 1 as one of the positons is already occupied by the maximum number. Similarly, for`m3`

it will be`x+n-m3-2`

and so on.So, for

`ith`

number(`array[i]`

), the positions it can occupy is`x+n-array[i]-(n-i-1)`

. If the number is less than x, then we have to subtract`x-array[i]`

as the number of candies cannot be less than x, hence the number of positions it occupies will be`x+n-array[i]-(n-i-1)-(x-array[i])`

.On simplification, the number of positions for

`array[i]>=x`

is`x-array[i]+i+1`

and for`array[i]<x`

is`i+1`

. or simply`min(x,array[i])+i+1-array[i]`

.Now f(x) is simply product the positions the numbers can occupy. 85781407

If someone has a simpler explanation for this approach, please let me know

for problem

1371E1 — Asterism (Easy Version), i am doing same as stated in editorial, but still getting TLE. Is it because i am usingPython 3? here is my solution Please help.I guess no, there are many Python3 accepted solutions if you see the rank list

Why the statement on the tutorial of 1371E2 that once f(x) is divided by p,then f(x+a) is divided by p too is right?

Alternative approaches to ceil() is a blog post about the discussion on Problem A. I hope you find this useful.

https://codeforces.com/contest/1371/submission/85805079

https://codeforces.com/contest/1371/submission/85805620

The test case 2 is failing in both the solutions because the "jury" is changing it's answer in the second answer. In the first approach, it says that the answer for testcase 14 is 4 but in the second solution, it says that the answer is 0.

Why is this happening?

In problem E(hard version),I'd like to ask the conclusion "It's easy to see, that there should be max(ai)−n≤x≤max(ai) to have f(x)≠0 and f(x)≠n! (they are both divisible by p)." mentioned by the author. How to deduce it? While in Solution 1, though the author doesn't mention it, he uses this trick in bucket sort. So I am eager to know about it.

Because when $$$x$$$ is too low you can't beat anyone, so there are no possible permutations. Same for high $$$x$$$, when you can beat anyone, so every permutation is good.

Yeah, thank you.

In problem E, i think , lower limit value of x where f(x)!=0 should be max(a[i])-n+1 and not max(a[i])-n and upper limit value of x where f(x)!=n! should be max(a[i])-1 and not max(a[i]). Is it so??

Yeah, maybe it's just for convenience.

Formula for problem #1 can be simplified to (n + 1) / 2 to work for both even and odd inputs.

Any advice for implementing with E2 binary search?

Anyone please help me why i am getting wrong answer on test case 9 for problem E1. Thanks in advance. https://codeforces.com/contest/1371/submission/85904339

Ohk , I got my mistake

Please tell what was it?

Why i get wrong answer on test 3 ? https://codeforces.com/contest/1371/submission/85927710

WOW, I'm stunned when reading through the author's code for problem F.

Compare to my code, It's too complicated

Here is my submission: 86278384

It's beatiful!

cảm ơn em :)

Dạ vâng a :))

hello

In C part I used :

and got a wrong answer. Could anyone please tell me.

There's something wrong with C's tutorial... $$$ m < min(a, b) $$$ should be $$$ m \leq min(a, b) $$$

In problem B, my code(Python3) 86906968 have wrong answer at test case 2.

When I replace the "int()" with "// 2" (integer division), it worked 86907899.

I don't know why it didn't work previously. Can someone explain this to me?