We will hold AtCoder Beginner Contest 174.

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

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

We are looking forward to your participation!

how many languages will be provided？

English and Japanese, usually.

thanks a lot！

Question E is really hard to understand imo, couldn't understand it at all. :(

shut up !! contest is still running ..

I didn't ask for any explanation. What do you even mean??

Yeah, statement of E is little confusing.

I don't think it was hard but you should make use of test case that is explained.

It seems like button to copy sample input has disappeared from Tasks for printing page. If I remember correctly, it used to exist before. Please add it back.

Whats the point in giving copy pasting problems -_-

It depends on the user if he/she wants to cheat or not. It's their own conscience that'll kill them. No matter how high they score, they'll just end up losing in the end since they didn't played fair.

I agree, problems E and F were very standard this time

Even without copy-pasting thing. Problems are trivial. I learned segment tree yesterday and guess what, solved the problem in 20 minutes.

I was also using segment tree but I got TLE by 200ms. How did you do it?

891 people solved all problems, which is way more than average ABC contest.

Dude please can you tell me how to do F with segment tree :)

this

I have been getting TLE on F for a while now, even using segment trees. Could anyone tell me as to how to further optimize it? Link to my submission-https://atcoder.jp/contests/abc174/submissions/15638983

Maybe it's because of maps. I'm not sure and can't get the approach too. I think your approach is different.

Okay, so basically for any range [left, right], I am storing the different values in a map, and for merging any two ranges, I am just going through the 2 maps, and storing the number of distinct values. For instance if range 1 has values=>(1, 2, 2, 3), then my map will simply contain {1, 2, 3} and if range 2 has values (3, 4, 4, 4, 4), then the corresponding map will contain {3, 4}

On merging the above two, the new map will have {1, 2, 3, 4}, and map.size() will be the answer!

how do you solve F

Link. This code in the first comment, with a direct solution

Solution(Sum segment tree) :

1. Sort the queries in order of $$$r_i$$$ and process queries offline. I don't know why it works.

2. let, $$$last_i$$$ be the last appearance of $$$i$$$. Then, do the following. traverse the array.

(a). $$$last[c[i]] = -1$$$, make $$$last[c[i]] = i$$$ and update position i in the segment tree with $$$1$$$.

(b). $$$last[c[i]] != -1$$$ update position $$$last[c[i]]$$$ in the segment tree with $$$-1$$$, make $$$last[i] = i$$$ and update position $$$i$$$ with $$$1$$$.

3. If a query ends in the current index, then query the segment tree.

And bingo, that's it. I just took a chance sorting the queries in order of $$$r_i$$$ and it worked. I don't know why it worked.

Can you please give an intuition for problem E

The idea is that

`last[val]`

keeps track of the rightmost index of`val`

so far in the array. As you iterate through the array (let's say you currently see the element`val`

), you replace the previous value of`last[val]`

.Now, the

`BIT`

contains`1`

wherever the index is a rightmost element (i,e,`BIT[idx] = 1`

if there is a`val`

such that`last[val] = idx`

), and is`0`

everywhere else.Now, when we want to process a query

`(l,r)`

, we update`last`

and the`BIT`

up to`r`

. Then we query`(l, r)`

in the`BIT`

to get the desired answer.I

thinkyou misread E as F...My bad, I thought he was asking about F because the comment he replied to was about F.

Yes Can you please provide intuition for E (Binary Search) I am not able to get what the question is asking to find

very nice method, thank you WhaleVomit and hossainzarif. May i ask if anything with segment tree or BIT is possible for online queries?

Seems challenging via a usual segtree/BIT. Here is one approach which uses persistent segment trees for online queries.

I tried to do this thing after contest to make my code work for updates but in vain. I haven't get any idea.

This can be done with a merge sort tree. See here.

Thanks a lot. I had a hard time understanding the BIT code but I understand it now.

Have a look at this blog, it might help same ans the only difference was the size of the array and order of input of query size.

I don't think this round is so well-prepared. Problem F has occured many times.

I tried F with segment tree and unordered_set<> but I got TLE by 200ms. I'm pretty sure I could speed up the merge of the segtree by not using unordered_set<>, but I couldn't come up with anything. How do you solve it?

solve it offline and using bit instead of segment tree

Segment tree worked for me.

can you share the code please? thanks in advance

You can just see other's codes in atcoder. You can just randomly pick one.

May i know the meaning of "solve offline"

"Solving offline" is when you process the queries in a convenient order that may not correspond to the original one. This is not possible, for example, in interactive problems.

I dont see why this will work, I mean merging or querying of segment tree will be not constant time per level right? Assuming that you are using set as node of segment tree.

aYes the merging isn't constant time (i am using sets node for segment tree) it's O(c) (c is the number of colors). I was hoping I could optimize the merging in some way, but it turns out the solution has a different way of using the segment tree.

D and C also

Try using two pointers for D. One for the left and one for the right of the string. Count the number of times a W and an R meets until l >= r.

did i ask how to solve it ?

yavva! lol

I think E has also occurred many times, although it's not as standard as F. Isn't it common Binary Search?

Yeah simple binary search

Yess its a standard BS problem.

Can you please provide the idea behind E or even some source for problem like E? thank you

So, I asked if I can do process queries and print after processing or I have to process one query and answer immediately. And I get no comments. I don't know why and certainly don't know if I have done mistake or not. Anyway, solved the task where I needed clarifications.

Well, it seems obvious that you can do whatever you want with the input, it isn't an interactive problem.

how to solve problem F . please help???

Mo's algorithm

Did you manage to make your solution pass with Mo? TL is 2s and $$$n, q$$$ are up to $$$5\cdot10^5$$$.

I see many people having done so, and it's quite confusing to me. How can Mo's pass such high constraints??

If you use the usual query ordering you will get TLE. But if you use the condition to sort according to the parity of the block (decreasing and then increasing) it was enough to get AC.

It was once you get right constant approximating sqrt(n). I tried constants around $$$200$$$ which didn't work. But once you use $$$500$$$ it's enough to pass.

But actually there was only one testcase large enough to have any chance of defeating Mo and it was random, so you can just be lucky to generate order of queries with relatively low total distance between consecutive ones.

For me using constant as 1000, passed luckily.

It passed easily (in about 1250ms) with block size $$$\left \lfloor{\sqrt{N}}\right \rfloor$$$ and the parity-based comparator.

The SettingsHilbert curve ordering works pretty fast. My implementation of MO's algo passed in 647 ms.

Can you post a link to your code?

My submission

About Hilbert Curve ordering

After contest end then please give me some idea on C.

Intitially i thought there must be some pattern, but after struggling for some time solved it using school mathematics. Important observations, 1) if n is even answer is always -1 2) answer cannot exceed the number itself. Check out my solution https://atcoder.jp/contests/abc174/submissions/15619615

Wow! that was easy!!

-And also if n if multiple of 5 the answer is -1.

Man missed the last line. Great solution but.

I am not able to understand why we not need more than K iterations???

You can read the proof here

difficult to understand proof for me can you please explain in some easy language

Can you please explain why your code works.. I can't understand why k = (k%n)*10+7

Can you please explain why n%5 is -1

Lol, it will always end with either 5 or 0. So we can never get any of the required 7, 77, 777 ...

ohh yea , thank you missed it !!

I can't find a difference between today's F and 1188 — Fast Queries . Really disappointed with today's contest :(

The problems seemed comparatively easy to me. I solved a SPOJ problem exactly like the problem F. I think the contest was not well prepared.

Same Question F from CSES can be found in GFG and other websites

Me during contest:

Toothbrush is new problem setter for ABC contest.. from now onwards..

Can anyone give a little bit of hint on C Repsept. Is there any kind of tradition of Atcoder that problem C is more intresting and challenging than D.

Here: https://stackoverflow.com/questions/31413626/how-to-calculate-smallest-multiple-formed-only-of-the-digit-1

With little modification (when the number is multiple of 7).

Ok thanks for this

Think around long division method.

F is almost similar to Spoj-DQUERY

Similar is an understatement!

Can someone please help out with Task C?

Here is a link of problem for finding multiple of k formed by only "1"s.

You only need to divide k by 7 when k is multiple of k.

My Substitution: https://atcoder.jp/contests/abc174/submissions/15639193

this link contains your submission visible only to yourself

Now it is corrected.

Mini-editorial:

A: if print else print. Code.B: loop through all points and count where`x[i]*x[i] + y[i]*y[i] <= d*d`

. Code.C: Notice that "X is a multiple of K" means that`x % k == 0`

. We can use modular arithmetic (modulo K) to find possible remainders. Adding another 7 to the end of X is the same as`x * 10 + 7`

.I think if after K iterations you don't get a 0, that means there's no answer, because there's only K different remainders modulo K. However, during the contest I wrote a different solution that detected loops in the sequence of remainders. Code.

D: The resulting sequence looks like $$$RRR..RWWW..W$$$, i.e. 0 or more R and then 0 or more W. Note that flipping the color fixes at most 1 stone, whereas swapping two stones can fix two, so we can ignore the color flip operation.Maintain two pointers: one pointing to the leftmost wrong element (=W) and the other pointing to the rightmost wrong element (=R). Swap the elements until the pointers meet. Code.

E: Use (integer) binary search on the answer. Suppose the answer is X ($$$0 \leq X \leq 10^9$$$). Then go through all logs $$$A_i$$$ and count how many splits $$$K_i$$$ you need to get the length of each of them under X, using the equation $$$A_i / (K_i + 1) = X$$$. There's some funky arithmetic with the rounding, which I'm unsure of, but it works. Code.F: Mo's algorithm (a type of sqrt optimisation). Note that if you know the answer for a window $$$[L, R]$$$, it's easy to move either side of the window one position to the left or to the right: You can use a vector (don't use map or unordered_map, they are too slow) to store how many elements of each color you have, and keep a counter of unique colors (to get the answer in $$$O(1)$$$.Then, using the idea of Mo's algorithm, you can sort the

queriesinto $$$\sqrt{Q}$$$ buckets in such a way that the total run time will be $$$O(N\cdot \sqrt{N})$$$ instead of $$$O(N^2)$$$. The trick is to first sort the queries by thebucketof their left edge $$$L$$$, and then within each bucket, sort them by the position of the right edge $$$R$$$. This ensures that you will move the left end of your window $$$O(Q \cdot \sqrt{N})$$$ times, and the right end of the window $$$O(N \cdot \sqrt{Q})$$$ times (and given the constraints, we can assume $$$Q = O(N)$$$).This can be implemented with a custom comparison function, no explicit buckets needed. Code.

Can you prove it

Pigenhole

how?

At a maximum the modulo value can take $$$k$$$ distinct values. If in $$$k$$$ iteration you did not arrive at $$$0$$$ then you never will because you are stuck in a cycle as said by Norrius

Got it after every k iterations cycles of modulo will repeat as (2*k+1)%k=(k+1)%k=1%k. Thanks , I was not sure of this condition but this one word made everything clear.

Eliminating corner cases where $$$k$$$ is $$$1$$$, $$$7$$$, or a multiple of $$$2$$$ or $$$5$$$, we can prove the statement.

Suppose $$$7, 77, ..., \frac{7}{9}*(10^{k-1}-1)$$$ are all not multiples of $$$k$$$. By pigeonhole principle, $$$2$$$ of the numbers will have the same remainder when divided by $$$k$$$.

We consider an example, say $$$7777777$$$ and $$$777$$$ are $$$x$$$ mod $$$k$$$. Subtracting, we get that $$$7777000$$$ is a multiple of $$$k$$$, which implies that $$$7777$$$ is a multiple of $$$k$$$. However, this gives us a contradiction since we supposed that it couldn't be a multiple.

Thus, we will have a value which is a multiple of $$$k$$$ after $$$k$$$ iterations.

For E, instead of using doubles, you can use $$$\lfloor(a-1)/b\rfloor$$$. To see why this works, if $$$a$$$ is a multiple of $$$b$$$ you need $$$a/b-1$$$ cuts.

:facepalm: I did (a+b-1)/b $$$-$$$ 1.

Isn't that the same?

Yes, it does the same thing! But (a-1)/b, looks better. O_o

For Question C My Approach was since any such number will be of the form {7/9}∗{10^i−1} so I am iterating from i=1 to 1e6 and taking modulo with k. if at any case my modulo==0 then it's my answer. //Pseudo Code

But I don't know why 5/27 test cases are giving WA. Please Can anyone help where it is getting wrong..

For modular inverse to exist,

`gcd(9, k) = 1`

should hold. I did the same and got RE.Ohkk, I understood.. Thanks for Help !!

I think that my approach to ABC-D is quite simple.

Can you explain the rounding in E. By using simple ceil(a[i]/m), it doesn't work. But in the equation, we have a[i]/m<=ki and we want sum of all ki<=k. How to handle these things as they might occur in different problems.

Can someone share his/her approach of solving problem C. I tried it a lot , but was not able to solve it.

Think around long division method. You just need to add a digit 7 in every iteration. Code

May I know why only that k % 5 == 0 or k % 2 == 0 must don't have answer?

cause multiple of 2 & 5 always produce even number..but 777... is always odd..

Trying I have seen your code, may i please know how you came to the conclusion that "when the given number ends with 1,3,7,9 ,the answer exists".

2:2,4,6,8,10,12,... it will not generate '7' digit 5:5,10,15,...

That didn't answer my question.

Number to have a multiple in form of 777... should be co-prime with 10. For more formal explanation you can look at this editorial provided by AnandOza.

Owhh, so thats why only have 2 and 5 doesn't have answer, thanks alot

BFS

start from 7 modulo n(length = 1) and keep adding next sequences modulo n to the queue until the queue is empty. If dp[0] is defined print that else print -1.

Code

Complexity: O(n) because there can atmost n distinct values with modulo as n

Here is my approach ABC-C

I've written an unofficial English editorial here: https://codeforces.com/blog/entry/80956

Hope it helps!

I think problem writers should read https://codeforces.com/blog/entry/75163 ...

I think it's (partially) a difference in intended difficulty rather than problem-setting philosophy, Atcoder Beginner Contests feel as though A (and sometimes B) are designed to be solved by everybody, as long as they can write basic code. Whereas on a CF Div2/3, sometimes you don't even solve A or B since they have some tricky observation.

In general, though, I agree (as I usually can solve Div2 A anyway), I also prefer when A and B have some interesting idea, and recent CF rounds have been really good about this! (And I think the blog post is valuable and worth reading.)

I think it is not about difficulty levels. Difficulty levels assume some difficulty.

What is the point of such problems as today's A?

If you look at the standings there is literally not a single person who tried to solve it (at least made a submission) and failed

What is the point of a problem that cannot be failed by any participant?

Moreover, interestingly, there is not a single person who solved only A. They either solve both A and something else or none. Which pretty much means that weak participants also don't think about this problem as existing and just start with more difficult ones to check whether they can solve it and see no reason to solve only A. Also, probably people who persistently can solve only A just don't visit such sites.

Some participants submitted a wrong solution on A resulting in 5 minutes penalty. In my case that did not really matter, for hitonade it means position 9 instead of first place.

Yes, penalties is pretty much the only way it affected the standings. But I don't think it justifies its existence

All of this is true, and I didn't think of it either.

I was mostly annoyed that F, presumably what the problem writers thought was the most interesting problem, was just a google search for most people. E was a quick think 'okay apply binary search on answer'

typing begins.The contest was not interesting for beginners, and therefore who was it interesting for lol???

Problem F just requires searching "Range Distinct Query". I know this is a Beginner contest so it's educational in nature, but maybe only E and F can be more carefully selected, since it's rated till a rather high bound.

Very true, E and F were easy this time, I didnt solve F because I didn't know about Mo. But it seems like CF Educational round C, D to me.

Educational Round never gives standard problems like F which requires 0 thinking.

i tried it with mo , but got TLE (block size = 555), any help ?

Mo solutions run ~1500ms, so there must not be any slow part.

I made block size 710 and it worked.

https://cp-algorithms.com/data_structures/sqrt_decomposition.html#toc-tgt-4

at the end it says, "In odd blocks sort the right index in ascending order and in even blocks sort it in descending order. This will minimize the movement of right pointer, as the normal sorting will move the right pointer from the end back to the beginning at the start of every block. With the improved version this resetting is no more necessary"

something like this

i did that and it worked

Can someone help with E, I am getting wrong answer on 2 test cases.

Approach is binary search.My solution

UPD:Error foundMaybe use a more reliable ceil function :

`ceil(a/b)`

is same as`(a+b-1)//b`

where`//`

stands for integer divisionThanks for the suggestion but it was not a ceiling error. The error was because I took low=0.

oh yes right nevermind _:)

I tried F using Mo's algo , but got TLE . This was similar to Dquery Spoj , But with higher constraints. Dquery

LOL https://www.geeksforgeeks.org/queries-number-distinct-elements-subarray/

My implementation of MO's algo passed in 647ms.

Someone please explain me problem C. How i can solve. What concepts were required.

Easiest Contest ever......

Certainly easier than div4

please upload the editorial in English, because peoples are participating from all over the world...

Can someone Plz tell me the process of solving c?I cant understand the google translated english of the editorial...

(Brute force) I just iterated over all 7, 77, 777,....(lrg value) and checked condition and got AC.

7 -> 7+(7*10)->7+70+(7*100)......till 10^6 times , dont forget to take Mod k at each step

whats wrong in my approach in E ??```` int n,k; cin >> n >> k; int a[n]; cout << fixed << setprecision(10); multisets; for(int i=0;i<n;i++) { cin >> a[i]; s.insert(1.000000000 * a[i]); } //sort(a, a + n, greater()); for(int i=0;i<k;i++) { double x = *s.rbegin(); double a, b; a = x / 2; b = x / 2; auto itr = s.find(x); s.erase(itr); s.insert(a); s.insert(b); } cout << (int)*s.rbegin() << en;

binary search over answer...

Video solutions to all problems for people who are interested.

Also, here's a written tutorial for people who are interested.

Tu kitna awesome hai yaar

Please provide english editorials

In E why we can't cut longest log in len/2 and len-len/2 two parts greedly and put them in priority queue.

I also done the same way. but I am not able to figure out mistake

That's actually what I implemented first -- luckily it got WA on the samples so I stopped and thought more.

For a small counterexample, consider a single log of length 6 and $$$k=2$$$ cuts allowed. Our priority queue algorithm will cut into [3, 3] and then cut again into [3, 2, 1]. But it's possible to cut into [2, 2, 2].

For a correct solution, see my English editorial.

(But I encourage you to think about it more before looking, now that you know why the priority queue doesn't work.)

Thanks for helping sir.Now i understood why priority queue will not work.

Priority queue does work, see submission.

The problem is that

`k`

can be`1e9`

in which case it TLEs. Otherwise, it would run fine.Interesting. That looks more complicated than the simple greedy we discussed. Can you explain the algorithm?

It is still greedy. Here, you store the number of cuts made too. Now when you make a new cut, you uniformly cut the whole log instead of the part you did.

Implementation:`Store total length, number of divisions and length of cut. Priority queue compares on basis of length of cut. When you get a new log, cut it into a unit more divisions.`

Taking your example, log is length 6, first cut cuts into [3,3] (storing as Log{total = 6, divisions = 1, length of cut = 3}) and second cut cuts into [2,2,2] (storing as Log{total = 6, divisions = 2, length of cut = 2}).

Oh, that's very nice! Thanks for sharing.

i use BigInteger class in problem C but didn't get the answer. Can any body tell where i was doing wrong...

It is much to slow because this runs basically $$$O(n^2)$$$

Can someone help and explain me why this Solution gives TLE on problem F please? I'm using persistent segment tree

Guys can you suggest good resource for number theory for competitive. I found today's C very hard.

you can learn modular arithmetic

On Problem E, I got WA. After contest I check some test case in which I got WA. I got this one. 1 4784 450968417 and the answer is 94247. But If I cut the log with 94246.3 interval, I think it take exactly 4784 cut. So why 94246 is not the answer? Can anyone please help me? Sorry. I missed rounding 'Up' word.

!

Can someone provide editorial for this in English?

https://codeforces.com/blog/entry/80956

chokudai Many people have just copy pasted the solution of F from this: https://www.geeksforgeeks.org/queries-number-distinct-elements-subarray/ Please look into this matter!!!

Searching the web is allowed in the rules. See: AtCoder Rules

Web searching is a different thing but the thing here is cheating, that is why there r so many people with AC

I was reading problem F many times to be sure. Because it was too standard and I was thinking it can't be asked.

You haven't still got the worst thing about atcoder. They can do everything. Get an example.

problem1 problem2

These are pretty similar, well, fair enough. But, they don't even care to display new testcase.

Those two problems have totally different solutions, and the samples being the same is probably a joke.

I needed to just modify the dp a little bit. Obviously these two are different problem. But, let's say someone googled the test cases during contest. That's taking them to the 1st problem. He opened the editorial. Now, he would easily get what's going on in the 2nd problem.

Who would google the testcases, during a contest? And then take the time to read the editorial of that other problem.

Apparently there's a solution to F for online queries and can even handle updates. It uses 2D range trees with fractional cascading:

https://stackoverflow.com/questions/39787455/is-it-possible-to-query-number-of-distinct-integers-in-a-range-in-olg-n

tl;dr; store 2d points of

`(currIndex, nextIndex)`

(default nextIndex is inf). Query the rectangle`L <= currIndex <= R && R < nextIndex <= inf`

to count last occurrences in [L, R]Can someone point me to a submission implementing it? (I'm mostly interested in the fractional cascading part to shave a log(n) factor)

I taught it will be interesting to see how I solved the C problem. I did it in a more complex way then others solutions. I try to make the last digit as a seven, check if the final product is all sevens, if it is not I repeat the process. https://atcoder.jp/contests/abc174/submissions/15630123

Can F be solved without specific algorithms and data structures?

Can anybody give a easy explanation in problem C as to why loop of i is only required till k and answer of number greater than k is not possible? But why

Maybe this can help

bro your explaination is good but i have some other doubt

`void solve(){ int n;

}` this solution is acceptable but I saw one optimized approach using for(i=1;i<=n;i++) instead of while(s.count(x)==0) but i am not able to understand how is it acceptable.... please give some valid explaination after optimization code is —

``void solve(){

} ``

Simply If we mod x , n times there will be at least two same remainder (pigeonhole theorem) . Now in the first code we put remainder in the set and whenever we encounter a repeat in the set aka repeat in the remainder then its not possible (it has form a infinite loop and will never end in mod 0)

In second code we just tried n times if there is not found mod 0 then it is sure that there will be repeatation of mod number so we break and print -1 (if we found mod 0 then we print number of step needed to get there)

but why is the answer guaranteed in less than n steps where n is the number which is given as input.

for(int i=1;i<=n;i++) how within this loops answer is confirmed ...can you please explain using example

7 8 13 and we have N=3 if we mod 3 of them by N we get 1 2 1 It repeats , why? pigeonhole principle :there can be atmost N-1 mod numbers(boxes) but we have N mod number (pigeon) . So some mod number must be repeated .

Thanks that was a quite understandable explaination

If anyone need Detail explanation of C Here

Please, could anyone help me debugging this code? Thx!

PS.2AC, 4TLE, 1WAanother PS. I cannot speak English very well, and I can't speak Russian as well. So if some grammer errors exist, just omit it or tell me, thanks!It is simpler to help if you link your submission instead of copy the code.

Apart from

`int no_use[500005];`

not beeing initialized that solution runs $$$O(n*q)$$$, which will TLE.well, this is my wrong solution of F

When will English editorial be published?

Can anyone help me with E-logs? Some people said its a simple binary search. I know binary search but even now I have no clue how it relates. It was my first contest. If anyone could link some articles or some similar problems, it will be very helpful. Thanks.

For E, try to imagine how the number of cuts is related to the maximum height of the chopped logs. If we have less k(suppose 0) then the answer will increase i.e the maximum height of the logs after chopping will be comparitively greater. If the number of cuts allowed is more(suppose 100000) then for a fix number of logs we can cut them more times than when cuts was 0, hence the maximum optimised length of any log will be less. Thus more the number of cuts, lesser will be the maximum length, when chopped optimally. Now, can you apply BS here? Sure you can :)

Yes, It makes sense now. I hope I can code it. Thanks. Can you suggest some problems on the same or similar logic?

https://codeforces.com/problemset?order=BY_RATING_ASC&tags=binary+search

Thanks, I was not aware of such sorting and filtering features.

Till when the English editorial would be uploaded?

Can anybody help with question C of this contest?