### chokudai's blog

By chokudai, 3 years ago, We will hold AtCoder Beginner Contest 174.

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

We are looking forward to your participation! Comments (159)
| Write comment?
 » how many languages will be provided？
•  » » English and Japanese, usually.
•  » » » thanks a lot！
 » 3 years ago, # | ← Rev. 2 →   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??
•  » » 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 -_-
•  » » 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 :)
•  » » » » »
•  » » » » » » 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
•  » » » » 3 years ago, # ^ | ← Rev. 3 →   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
•  » » » » » » 3 years ago, # ^ | ← Rev. 4 →   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 think you misread E as F...
•  » » » » » » » » My bad, I thought he was asking about F because the comment he replied to was about F.
•  » » » » » » » 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.
 » 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.
•  » » » 3 years ago, # ^ | ← Rev. 2 →   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.
•  » » I think E has also occurred many times, although it's not as standard as F. Isn't it common Binary Search?
•  » » » Yess its a standard BS problem.
 » 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.
 » 3 years ago, # | ← Rev. 2 →   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.
•  » » » » » » 3 years ago, # ^ | ← Rev. 2 →   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 Settings #define block(i) (i / B) constexpr int N = 500001, A = 500'001, Q = 500'001; int f[A], a[N], ans[Q], B = 707, answer; struct query { int lo, hi, idx; query() {} query(int lo, int hi, int idx): lo(lo), hi(hi), idx(idx) {} bool operator < (const query& q) { if(block(lo) != block(q.lo)) return block(lo) < block(q.lo); return (block(lo) & 1) ? (hi < q.hi) : (hi > q.hi); } } q[Q]; 
•  » » » » Hilbert curve ordering works pretty fast. My implementation of MO's algo passed in 647 ms.
•  » » » » » Can you post a link to your code?
•  » » » » » »
 » 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!! -
•  » » » I am not able to understand why we not need more than K iterations???
•  » » » » You can read the proof here
•  » » 3 years ago, # ^ | ← Rev. 3 →   Here is my approach, if n%2==0 OR n%5==0 print -1; else //( I used dfs to solve the problem) ll cnt=1; ll dfs(ll a, ll n) // here a is initially 7 { if(a%n==0) return cnt; cnt++; return dfs((a*10+7)%n,n); } 
•  » » » 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 ...
 » I can't find a difference between today's F and 1188 — Fast Queries . Really disappointed with today's contest :(
 » 3 years ago, # | ← Rev. 4 →   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: » 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.
•  » » 3 years ago, # ^ | ← Rev. 2 →   With little modification (when the number is multiple of 7).
•  » » » Ok thanks for this
•  » » Think around long division method.
 » 3 years ago, # | ← Rev. 2 →   F is almost similar to Spoj-DQUERY
•  » » Similar is an understatement!
 » 3 years ago, # | ← Rev. 3 →   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 queries into $\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 the bucket of 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 I think if after K iterations you don't get a 0, that means there's no answer. 
•  » » » 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.
•  » » » 3 years ago, # ^ | ← Rev. 2 →   :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
•  » » 3 years ago, # ^ | ← Rev. 2 →   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 for( i=1 to 1e6+7) { if(((7%k)*(Modular_Exponentiation(10,i,k)%k+k-1)%k*(Modular_Inverse(9,k)))%k==0) { cout<
•  » » » For modular inverse to exist, gcd(9, k) = 1 should hold. I did the same and got RE.
 » 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.
•  » » 3 years ago, # ^ | ← Rev. 2 →   BFSstart from 7 modulo n(length = 1) and keep adding next sequences modulo n to the queue until the queue is empty. If dp is defined print that else print -1.CodeComplexity: O(n) because there can atmost n distinct values with modulo as n
 » I've written an unofficial English editorial here: https://codeforces.com/blog/entry/80956Hope it helps!
 » I think problem writers should read https://codeforces.com/blog/entry/75163 ...
•  » » 3 years ago, # ^ | ← Rev. 2 →   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.)
•  » » » 3 years ago, # ^ | ← Rev. 7 →   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 failedWhat 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.
•  » » » » » 3 years ago, # ^ | ← Rev. 2 →   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.
 » 3 years ago, # | ← Rev. 2 →   Can someone help with E, I am getting wrong answer on 2 test cases. Approach is binary search.My solution UPD: Error found
•  » » 3 years ago, # ^ | ← Rev. 2 →   Maybe use a more reliable ceil function : ceil(a/b) is same as (a+b-1)//b where // stands for integer division
•  » » » Thanks 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
•  » »
•  » » My implementation of MO's algo passed in 647ms.
 » Easiest Contest ever......
•  » » Certainly easier than div4
 » please upload the editorial in English, because peoples are participating from all over the world...
 » Video solutions to all problems for people who are interested.
•  » » Also, here's a written tutorial for people who are interested.
 » 3 years ago, # | ← Rev. 2 →   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.
•  » » 3 years ago, # ^ | ← Rev. 3 →   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.)
•  » » » 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)$
 » 3 years ago, # | ← Rev. 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.
 » 3 years ago, # | ← Rev. 3 →   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?
•  » »
 » 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 problem2These 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.
 » 3 years ago, # | ← Rev. 2 →   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-ntl;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)
 » 3 years ago, # | ← Rev. 2 →   deleted
 » 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
•  » » » 3 years ago, # ^ | ← Rev. 7 →   bro your explaination is good but i have some other doubtvoid solve(){ int n; cin>>n; set s; int x=7%n; int i=1; while(s.count(x)==0){ if(x%n==0){ cout<>n; int x=7%n; for(int i=1;i<=n;i++) { if(x==0){ cout<
•  » » » » 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 .
 » If anyone need Detail explanation of C Here
 » Please, could anyone help me debugging this code? Thx! #include using namespace std; #pragma GCC optimize("O2") #define max(a,b) (a>b?a:b) #define min(a,b) (a=s[m-1]) return t; else return s[m-1]; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> q; t = 0; c = 0; for (int i = 1; i <= n; i++) { cin >> t[i]; if (find(t, t + i, t[i]) == t + i) c[i] = c[i - 1] + 1; else c[i] = c[i - 1]; } // for (int i = 1; i <= n; i++) cout << c[i] << ' '; for (int i = 1; i <= q; i++) { int no_use; cin >> l >> r; if (l != r) { for (int j = l; j <= r; j++) no_use[j - l + 1] = c[j]; cout << maxn(no_use, n) << endl; } else cout << 1 << endl; } return 0; } 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;` not beeing initialized that solution runs $O(n*q)$, which will TLE.
 » 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?
•  » » » »
•  » » » » » Thanks, I was not aware of such sorting and filtering features.