We will hold AtCoder Beginner Contest 166.

- Contest URL: https://atcoder.jp/contests/abc166
- Start Time: http://www.timeanddate.com/worldclock/fixedtime.html?iso=20200503T2100&p1=248
- Duration: 100 minutes
- Number of Tasks: 6
- Writer: gazelle, kort0n, kyopro_friends, sheyasutaka, ynymxiaolongbao
- Rated range: ~ 1999

The point values will be 100-200-300-400-500-600.

We are looking forward to your participation!

Thank you atcoder for the wonderful weekend !!

Your profile pic is itself wonderful !!

I wish it will be a good round！

Why so easy A,B,C,D,E?

Atcoder beginner contests are just a joke. Only 1-2 good problems (probably E and F) and others are just fillers. Atcoder beginner contests should be of Div-2 difficulty or atleast in-between Div-2 and Div-3.

I think it's actually for Division 2. That's why it is called as

Beginner's ContestI think it's Div.3, because there are three kinds of regular contests: ABC, ARC, and AGC.

Still, ABCs are good enough for a guy with 0 ratings.

Anyone give me a counter test case where my logic fails for F. I have done a greedy approach , where for each string , I will increase the one with min value and decrease the one with maximum in b/w those two variables and storing the values accordingly and then printing.

1 1 0 and AB. You need to know what comes next to make the right choice.

yeah,thanks.got it!

I think F is easier than E. (I'm trying to solve F)

I solved it! I made 1600 points!

D was very interesting.

what's the solution for D??

If answer exist(for given x there always exist a pair (A,B)). It will be -1000<=A<=1000 and -1000<=B<=1000. use two loop

why -1000 to 1000?

Actually I took -1000 to 1000 for safety. but u can see (100)^5>1e9. U can take -120 to 120 too. It will also pass

May be not I tried

say if the difference given to us comes at (1000)^5- (999)^5

the number (1000)^5 — (999)^5 is greater than the possible range of values for x

"B" in "ABC" stands for beginner.

Even Div-3 contests in codeforces are also for beginners but they don't ask you to print the circumference of the circle with a given radius or swap 3 numbers.

or just print what is said! AGC AND ARC are good by the way

Why so much hate in this world

cryGet AC on A, B, C, D, E and WA only on the last of the 60 tests of the F...

![ ]()

How to solve F!

Keep alive this round, try to alive next round(Be careful when there are two 1, and one 0).

Always increase a smaller variable and decrease a greater one in a pair, except for the one case: when two variables you need to change are $$$1, 1$$$ and the third one is $$$0$$$, in this situation you have to choose what variable to increase depending on the next request in order to avoid a case like where you have $$$2, 0, 0$$$ and the next request is $$$BC$$$.

Here's my solution.

How to solve problem E?

You need to use the frequency map, In first loop perform f[ i — a[i] ]++, and in second loop perform count += f[ a[i] + i ]. that's it

For two attendees to pair i-j = hi + hj (Assume i>j) should hold where i and j are indices of the attendees. Move both the i terms on i side and j terms on the other then it reduces to i-hi = hj + j. Which is equivalent to finding number of comment elements in two arrays by making arrays for both i-hi and the other as hj+j.

E is pretty easy if you make the observation that for any j > i following condition has to holds, j — A[j] = A[i] + i. so keep a map to count the occurrence of A[i] + i observed till now. add the count of j — A[j] into the answer.

solution

You need to find number of pair of indices such that

|j-i| = A[i] + A[j]. Assume j > i, then, you need to check which (i,j) satisfyj — A[j] = i + A[i].You can start iterating from left, keep a count of i+A[i] and for every new index j, do,

ans+=count[j-A[j]]We are considering

`j > i`

so we need to find all i and j such that`j - i = a[j] + a[i]`

which is`a[j] - j = - a[i] - i`

so for each j we need find count all of those whose value

`- a[i] - i`

is equal to`a[j] -j`

. We can use a map to store values of`- a[i] - i`

and add it to our answer in each step for value`a[j] - j`

.Code

got it.

Just use the fact that

`A[i]+A[j]=abs(i-j)`

expands to`i-A[i]=A[j]+j`

(atleast in positive value of abs sign). For the other one it'll cover cases by symmetry. No compute the two arrays with entries`i-A[i]`

and`j+A[j]`

, store the counts of elements in a frequency map and return the answer.A simple solution is here

An easy code to understand

why greedy for F fails? anyone Please?

What do you do when both A and B are equal and next event is AB?

For example: A = 1, B = 1 and C = 0 and event is AB.

You need to check if next event is BC or AC and work accordingly. Can't just make anyone of it +1 and other -1.

Yeah for this I did offline thing. At every stage I checked if both are equal then which is having more query remaining. So the one with higher no of query remaining I add to it(give it priority)

You should give priority to the next closest move, not to the most frequent among remaining. For example A = 1, B = 1, C = 0, and the sequence of moves is AB, AC, BC, BC, BC. On move AB you should add 1 to A, to make move AC possible, not to B, even though BC is more frequent.

this F is not very hard,i think,but has a number of details to deal with.

i finally got accepted after 5 tries with a loooooooooooooong code.

I guess only 2 details to deal with no ? Check if one of them is 0 or both of them are 1. My code wasn't that long either : https://atcoder.jp/contests/abc166/submissions/12771073

oh,maybe i did a lot more usless things.

you can see what i did here XD

Well at least you got the AC haha it took me a while to think it through xD

Can you share your detailed approach?

here

Got 457 lines of code in F)

F

E anyone?

Start traversing a from back to front and at each i from $$$1$$$ to $$$N$$$ keep count of $$$i$$$-$$$Ai$$$ and check how many previous elements are there with current sum $$$i$$$+$$$Ai$$$.

My Solution

Comment

I had a lot of TLEs with my solutions for this contest. For example, here is my code for problem E (C++):

I got AC for half of the test cases, and TLE for the other half. Can anyone tell me why?

Also, if someone can tell me what might generally be the case when AtCoder grading returns a TLE, please let me know. Thanks in advance!

Your code has a complexity of O(N^2) and N is 1e5. So that is why you are getting TLE.

TLE = Time limit exceeded.

Checking every possible pair (complexity O(N^2)) is not efficient enough for such a big value of n as 2*10^5.

What is the approach for F?

I tried defining each of the 8 choice possibilities (For AB choose A first or B if both remain, similary for AC and BC) at the start and check if it works. Do you have to do something like this at each step and maintain an 8 * n DP or something like that?

My solution was to choose such an option that I will survive current and also next round. If both choices are survivable then go greedy.

You could just run a DFS and check if you can reach a path of length n. Start with state a,b,c and then at each point proceed to a 2 posibble state given by choice[i].

Here is my submission: https://atcoder.jp/contests/abc166/submissions/12776155

isn't the complexity of your code 2^n?

Yes, it seems like an oversight that these solutions pass?

It even works in Python:

https://atcoder.jp/contests/abc166/submissions/12781991

How is your code working but not this simple recursion? Aren't they of the same complexity https://atcoder.jp/contests/abc166/submissions/12784875

It's because you're not passing references to your function, it creates a copy which take time for each call. you can find your accepted version here

Thanks a lot for helping! Learnt something new. Nice of you to help so many beginners in the comment section.

We're all beginners in some way, glad I could be of help

I tried with 27*n dp but it failed

Any one passed using dp?

i did a 64*n dp which got AC.

can you share your approach. Also explain your dp states

I have similar (maybe even the same approach) — you can notice that we don't care what a value is if it's bigger than 3, so we can have states (position, a, b, c), and after each step a = min(a, 3) and so on. You have 64 * n states and two transitions from each state. Here's my code

https://atcoder.jp/contests/abc166/submissions/12752950

`we don't care what a value is if it's bigger than 3.`

I think we can make a=min(a,2), with 27*n states is also acceptable. my submissionis keeping pre computed dp in your second recursive function also work?

I don't get it perfectly what you are asking, but second recursive function is for printing path, and yes precomputed dp works in that.

Like this. https://atcoder.jp/contests/abc166/submissions/12803104

It works fine, I see your submission, In second recursive function when you are comparing

(dp[in+1][a-1][qq][c]==1)here you have to add condition thata-1>=0same in other 2 If statement also. corrected your submissionthanks.

Can you share your intuition behind why don't we care for values that are greater than 3 (or 2)?

Why just taking 3 would do? I understand by looking at examples but am not able to wrap my head around it. It would help if you could share your intuition perhaps.

This whole idea came to me when I noticed that configuration "1 1 1" is always correct if we choose greedy approach (add 1 to smaller of two). That led me to conclusion, that if sum is bigger or equal to 3 (unless it's 3 0 0 and operation is BC) is always correct as well. Connect this with pretty natural DP solution and you get my solution. If you have more questions feel free to ask

Greedy approach can get AC, You only need to check a couple of things first :

0you have to add it+1.ABthenBC(A = 1, B = 1, C = 0) you have to add+1toBotherwise you add it to the other.+1to the minimum of them.0returnNoSolution : https://atcoder.jp/contests/abc166/submissions/12771073

why does this work

It can be easily shown if one value is greater than the other, it's safe to increase the min value. Our only concern is when both values are equal to

1, that way one of them will become a zero, we need to know which one to increase. The choice is based on which value will be used again first. Same example as the last comments :A = 1, B = 1 and C = 0it will depend on the order.Suppose the order is :

ABthenBC, if we increaseAour solution will fail.And if the order is :

ABthenACthen if we increaseBour solution will fail too.It is clear that we need to increment the value that comes next first in that case.

got it, thanks a lot.

UPD : Deleted

For F I did complete search using recursion and it passed. Can anybody explain why?

Maybe Highly optimized,choose the right answer first, so it succeed when first get into end state.

Can you share your code?

here

It all comes to when is the answer

No: The only possibility it fails is if there are3zeros or2and a certain ordering to make it fail. That being said, The answer can be found fast enough for both cases.can someone help me in problem D ?

D is quite easy. You just need to use brute force and compute all fifth powers of numbers from -1000 to 1000, and then you can use an O(n^2) algorithm to see if x is equal to the difference of any two. Code

pff i feel so stupid !! thanks bro

In my first atempt i used 100 instead of 1000, and it failed. But the 1000 was only a lucky guess.

How would we find the correct minimum number?

Find the smallest number x for which (x^5-(x-1)^5) > 1e9.

I think it comes around 120.

I guess look at $$$b=a-1$$$ for boundary condition because that minimise $$$x$$$ for given $$$a$$$. I tried $$$100$$$ because I misread number of digits in $$$100^5-99^5$$$. Then I used $$$200$$$ and it passed.

I don't have a strong proof as to why will there always be a pair less than

`|a| , |b| <= 10^3`

but had some intuitive feeling and it ACedCodex^5 — (x-1)^5 is an increasing function for postive integers. The first time its value crosses 10^9 is when x is around 10^12.

English Editorail of F

Three Variables Game • When A + B + C = 0, N is 1 or more, so the answer is No. • When A + B + C = 1, at least one of the two variables is zero at any time, so the action on each turn is uniquely determined. • When A + B + C> = 2, the answer is No if the variables added and subtracted in the first turn are both zero, otherwise the answer is Yes. In the latter case, you can keep one of the variables being added positive by taking the following strategy each turn. {When one of the variables to add and subtract is zero and the other is positive, add one to the one that is zero and subtract one from the other. (A + B + C = 2, the variables to add and subtract are both 1, and when the character string of the next turn and the character string of the current turn are different, not the last turn, the character addition and subtraction of the next turn is performed. Add 1 to the variable you plan to do and subtract 1 from the other. {Otherwise, select appropriately

What wrong in my code for the problem D

Your limits are too small, you can subtract values so you may need values greater than 1e9. Your code with greater limits get AC. I don't know how to find the correct limits.

the language of D and E was so clear still couldn't solve it, got WA on last test case of E and TLE on D. I like atcoder's format easy language difficult task .makes these problems more approachable. Thanks Atcoder

Please help me with problem E and F

In E you can see a pattern emerge... Try to map the sum of the index of first element and height of the first element with:

The difference in the index of the second element and its height.

Store the sum of the index and height of first element in the map in one pass. In the second pass count how many occurrences of the index — height is present in the map. Be sure to take Long. I got WA due to that.. Here is my solution: https://atcoder.jp/contests/abc166/submissions/12778280

thank you! https://atcoder.jp/contests/abc166/submissions/12786984 solved it in 53 ms

Yay I full solved all 6 problems with 0 incorrect attempts!

Timing was slow though :(

I thought the problem statement on B was not clearly defined. Otherwise, it was such a great contest! got WA on both D and E because I did not take long. Feel like killing myself :( Gonna upsolve F now.

I solved D by expressing A^5 — B^5 as (a − b)(a^4 + a^3*b + a^2*b^2 + a*b^3 + b^4).

At this point, I iterated over all possible divisors. Since a-b < (the term consisting of all positives), I only had to check the divisors less than sqrt(X). Then, once a-b was set, I binary searched to find the value of b (if any) to satisfy the expression.

My final runtime: optimized O(sqrt(X)*log(X))

You can also check if A^5-B^5 equals to x for all a and b from -200 to 200. It's faster.

It can be -120 to 120.

can you share your code

This code is easier

Code

just check from -1000 to +1000

because if a = 1001 the minimum positive result is b = -1000 and the result is 1.0004901e+20 and it's bigger than x

It can be -120 to 120, 120^5-119^5=1,019,663,401, it's bigger than 1e9.

Yeah I also did first part of your method, but I think it was overkill when I looked at $$$100^5-99^5$$$. So I wrote the easy brute force.

In codeforces https://codeforces.com/contest/1345/problems we get list of all problems, how to do the same in atcoder, like all the task they appear in the same page. If anyone knows please comment the link of that page where all task appear on one page.

You could use kenkoooo for that.

Thanks it's an awesome site.

I too found the page where like all task of a particular contest appear —

https://atcoder.jp/contests/abc165/tasks_print

Format —

https://atcoder.jp/contests/"contest_name"/tasks_print

Oh, I got you wrong then :)

I overkilled F with dp, followed by 14-15 RTE's :(

PS: Atcoder could really work to differentiate between MLE and RTE.

Here is my solution to problem F using DP.

Solution in simple words:

A. Just check the input string.

B. Compute the food each person has. One can use a frequency array to do this.

C. Iterate through all paths. Each path makes one observatory non-peak (or two, if they have same height).

D. Since $$$a^5 - b^5 = X > 0$$$, we have $$$a > b$$$, that is $$$b \leq a - 1$$$. So $$$a^5 - (a - 1)^5 \geq a^5 - b^5 = X$$$. Obviously $$$f(x) = x^5 - (x - 1)^5$$$ is increasing when $$$x > 1$$$ and decreasing when $$$x < 0$$$, so we can find the upper and lower bound of $$$a$$$ from $$$X$$$. Simply enumerate all possible $$$a$$$'s and check if $$$a^5 - X$$$ is the fifth power of some integer. This can be done with precomputing all fifth powers in the potential range. In practice we can use a looser bound to estimate the range of $$$a$$$.

E. Consider the number of pairs $$$(i, j)$$$ satisfying $$$i < j$$$ and $$$A_i + A_j = j - i$$$. Fix $$$j$$$, the number of $$$i$$$'s pairing with $$$j$$$ satisfying $$$A_i + i = j - A_j$$$. Just iterate from left to right and keep the frequencies of all $$$A_i + i$$$.

F. In each operation we can greedily take $$$1$$$ from the greater number and add $$$1$$$ to the smaller one. The only exception is there being two $$$1$$$'s and one $$$0$$$ and we are going to operate on the $$$1$$$'s. We have to create another $$$0$$$ in this operation, so we need to consider the next operation in order to make it doable. The answer is

`No`

if any operation creates a negative number.https://atcoder.jp/contests/abc166/submissions/12803726 i am getting wrong ans on killer test 5 Can somebody help me out. It is for problem F atcoder 166

My python solution12793022

I am getting WA for one test case (killer_05.txt). Can somebody please tell me what am I missing? Thank you.

same here

https://atcoder.jp/contests/abc166/submissions/12803726 i am getting wrong ans on killer test 5 Can somebody help me out. It is for problem F atcoder 166

I read it a bit fast but I think your mistake is in this case: "if(a[id1]==a[id2] && a[id1]==1 && a[id3]==0)".

You are not taking into consideration the other special case where a[id2]=1 and a[id3]=0. Also the way you are searching for the future cases is wrong because you assign a char variable to a string one.

Generally, I think that you only need to check for equality and increase the a[id], which is the one that will participate in a future choice sooner than the other.

For D — detail explanation and reduced range of searching with code Here

For problem E, I found an interesting solution. Let's imagine there are n persons standing on n consecutive cells in a row, the question ask for how many pairs of them can reach each other if they lay down to the left or to the right, formally it 's number of pairs which have sum of their heights equal to their distance. So we just let each person lay down to the left and to the right and mark these points. Then we iterate all marked points and add $$$from\_left[point]*from\_right[point]$$$ to the answer, check my code