### chokudai's blog

By chokudai, history, 2 years ago,

We will hold AtCoder Beginner Contest 179.

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

We are looking forward to your participation!

• +79

 » 2 years ago, # |   0 Front row!
 » 2 years ago, # |   0 "We are looking forward to your participation!"We are looking for quick editorials.P.S. I love ABC's.
•  » » 2 years ago, # ^ |   0 same to you...looking for quick editorials
 » 2 years ago, # | ← Rev. 2 →   +6 What are the ALC contests of Atcoder? Edit: It's is described in this blog
 » 2 years ago, # |   +4
•  » » 2 years ago, # ^ |   0 (-_-)
•  » » 2 years ago, # ^ | ← Rev. 3 →   0 No, it is me after solving ABCDEThough it seems F was not particularly difficult this time
 » 2 years ago, # |   0 I tried a DP solution for D. Somehow getting TLE in 6 TCs. I tried to optimize it as much as I could. Am I missing something?Link to Submission
•  » » 2 years ago, # ^ | ← Rev. 2 →   +3 post qns only after contest is over.
•  » » » 2 years ago, # ^ |   0 It is over.
•  » » » » 2 years ago, # ^ |   0 U posted few minutes before it was over.
•  » » » » » 2 years ago, # ^ |   +4 I could have posted the comment barely moments before the contest got over. But anyways, you can't open the submission link before the contest gets over anyways. Don't be a cop. If you can help then you are welcome.
•  » » » » » » 2 years ago, # ^ |   0 direct DP is O (n^2). U need to use either prefix array or seg tree to solve in O(n logn)
•  » » » » » » » 2 years ago, # ^ |   0 I tried d with segment tree but in runtime error in some test cases. Can you help me? https://atcoder.jp/contests/abc179/submissions/16889464
•  » » 2 years ago, # ^ |   +1 i used fenwick tree to optimize it, note that k <= 10, your solution fails because ranges can be very big, so your solution is n^2
•  » » » 2 years ago, # ^ |   0 But the sum of ranges won't be more than O(n) I suppose. Because the segments are nonintersecting.
•  » » » » 2 years ago, # ^ |   +8 Yes, the sum of ranges is $O(n)$, but you also have $O(n)$ states and for each, you iterate through all of them. In total it is $O(n^2)$.
•  » » » 2 years ago, # ^ |   0 I used a presum array to find the sum of dp in previous ranges.
•  » » 2 years ago, # ^ |   0 you can try it in O(n*k). Refer to below link for reference.https://www.geeksforgeeks.org/constant-time-range-add-operation-array/My Submission
•  » » 2 years ago, # ^ |   0 your code runs in O(K*N) where K is the number of possible jumps, theoretically in worst case it would reach O(n^2) time complexity which could defenitely get TLE. In this problem i used dynamic programming with fenwick tree, you can look at my code here
•  » » 2 years ago, # ^ |   0 For d, you have to combine the dp you are talking about, with the range covering problem (it is easier than segment tree I leave you this video in which that guy explains that problem https://youtu.be/Zze-O2oxoEo?t=219 ) and then you just have to be careful about modulo operations (you might be doing them with negatives)I hope this was useful
•  » » » 2 years ago, # ^ |   0 Thanks for this. I checked out the video. Isn't what he is talking about, called the Fenwick tree?
•  » » » » 2 years ago, # ^ |   0 No, Fenwick tree is a little more complex than that, it is about having sum until last significant bit and when doing a query going up from last significant bit to most significant bit (with a complexity of logn) and when updating is almost the same, but instead of getting an answer, updating; that is why it is also called Binary Indexed Tree
•  » » 2 years ago, # ^ |   0 My code is similar with yours and I wonder how to optimize it too. [submission:#17148205]
 » 2 years ago, # |   0 Loved solving E !
•  » » 2 years ago, # ^ |   0 How to solve ?
•  » » » 2 years ago, # ^ | ← Rev. 2 →   0 Its running a while loop until you face one among three situations,i) when x becomes 0 (once x becomes 0, the following numbers will be 0 too)ii) when count==n (it means you no more needed to continue evaluation)and the third observation is probably the challenging oneiii) once you see any x repeating then come out of the loop, because you know the following numbers repeat too
•  » » » » 2 years ago, # ^ |   0 i used the same thing can u plz tell whats wrong with my solution string solve(){ ll n,x,m; cin>>n>>x>>m; vector vec; vec.push_back(0); set s; ll num=x; ll sum=0; ll count=0; while(s.count(num)==0){ vec.push_back(num+vec.back()); s.insert(num); sum+=num; num=(ll)power(num,2,m)%m; if(num==0) ret(sum); count++; if(count==n) ret(sum); } ll times=n/count; ll rem=n%count; ll ans=times*sum; ret(ans+vec[rem]); }
 » 2 years ago, # |   +27 First time in ABC I was able to solve all problems. Here are short explenations. FUse two segment tree with lazy propagation to maintain the first blocking position per row/col. Submission EThe sequence repeats after at most M elements. Find the size of the loop and the sum of the loop, then "jump" to n. Submission DUse segment tree with lazy propagation to maintain the dp-array. This makes it possible to quickly update the intervals. Submission CBrute force count the number of pairs $i*j •  » » 2 years ago, # ^ | +7 For d instead of segment tree + dp you can use prefix array + dp. •  » » » 2 years ago, # ^ | 0 please share your code •  » » » » 2 years ago, # ^ | +3 •  » » » » » 2 years ago, # ^ | 0 plz give a brief explanation also. thnx •  » » » » » » 2 years ago, # ^ | ← Rev. 5 → +3 Basicdp[i] represntes number of ways to reach ith index.suppose you can make jump of value a1,a2,a3.then dp[i] = dp[i - a1] + dp[i - a2] + dp[i - a3] Solutionconsider a segment (L1,R1)so dp[i] = dp[i - L1] + dp[i - (L1 + 1)] + ... + dp[i - R1]So for this, we can use the prefix array.dp[i] = prefix[i - L1] - prefix[i - R1 - 1]We have to repeat this process for all segments.Submission •  » » » » » » » 2 years ago, # ^ | 0 thnx a lot:) Helped a lot •  » » 2 years ago, # ^ | +6 You don't really need a segment tree for D I just used prefix sums •  » » » 2 years ago, # ^ | 0 please share your code •  » » » » 2 years ago, # ^ | 0 •  » » 2 years ago, # ^ | 0 D and E should have been swapped ,I didn't even attempt E after continuous TLE's in D . •  » » » 2 years ago, # ^ | ← Rev. 2 → +1 D is more difficult in terms of the idea I guessE is heavier in terms of implementation •  » » » 2 years ago, # ^ | +1 even i think so •  » » 2 years ago, # ^ | ← Rev. 2 → +24 You don't need segment tree for F either. Code#include using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; set> se[2]; se[0].insert({n, n}); se[1].insert({n, n}); long long ans = 1LL * (n - 2) * (n - 2); while (q--) { int ty, i; cin >> ty >> i; ty--; auto it = se[ty].lower_bound({i, -1}); int k = (*it).second; ans -= k - 2; se[ty ^ 1].insert({(k - 1), i}); } cout << ans << '\n'; return 0; }  •  » » » 2 years ago, # ^ | 0 can you explain your approach for F •  » » » » 23 months ago, # ^ | 0 I'm more than a month late, you may have got the answer till now, but here is what I think the approach is:Here two sets are used to maintain position of lines of white stones. There are two kinds of lines length wise :1. Partial lines(going from one end and ending halfway)2. Complete lines (going from one end to other end)There are two kinds of lines possible orientation wise :a) Horizontal Line - Can be stored as$y$co-ordinate it starts on and$x$co-ordinate it ends on.b) Vertical Line - Can be stored as$x$co-ordinate it starts on and$y$co-ordinate it ends on.Now what exactly those two sets do, set[0] stores position for horizontal lines, and set[1] stores position of vertical lines.Formally a pair stored in respective sets looks like: s[0]= (Ending abscissa, originating ordinate) s[1]=(Ending ordinate, originating abscissa) Note that at the start we only have two complete lines, one horizontal and one vertical that's why a pair$(n,n)$is stored in both the sets. Query 1 We search for nearest horizontal line we can find that ends after$x$, and thus add remove black stones in that interval from the answer accordingly and now one vertical line is created in the process that starts at$x$and ends just before the horizontal line we found. Query 2 Search for nearest vertical line, remove black stones in that range from the answer and add a horizontal line in the corresponding set. •  » » 2 years ago, # ^ | 0 F: you can use std::map for the same query (lower_bound to find closest) SubmissionD: you can use only partial sums of your dp and fill dp[i] with sum of previous values Submission •  » » » 2 years ago, # ^ | 0 Can you explain your approach for F? •  » » 2 years ago, # ^ | 0 I also thought of using segtree with lazy prop on F, but I had no segment tree template with lazy propagation :P and I had only 35mins left. So I quit. Missed my chance of solving all problems in ABC for the first time. I'm taking your template now XD. •  » » » 2 years ago, # ^ | 0 I also got idea of two lazy segment tree immediately but I haven't implemented even a basic lazy segment tree before so I gave up. Now I am seeing there are other solutions too •  » » » 2 years ago, # ^ | 0 I had to fix a bug in that template in function rangeInc(). Not sure if everything works fine. •  » » » » 2 years ago, # ^ | 0 Oh,thanks for informing. I'll test it properly after customizing it for myself. :) •  » » » » » 2 years ago, # ^ | 0 In AtCoders ACL library there is a segment tree, too. I would like to switch to that one, but need time to get used to it.ACL lib •  » » » 22 months ago, # ^ | 0 Question: I thought that we should need to be able to get the historically minimum of some certain index? Won't that be more than just a plain old segtree? •  » » 2 years ago, # ^ | 0 I got tle in this, could you plz tell me why? int ans = 0; for(int i = 1 ; i •  » » » 2 years ago, # ^ | ← Rev. 5 → 0 You have asymptotic of$O(n \sqrt{n})$which is too big. You can enumerate only the smallest number of$A, B$which would be$\sqrt{n}$and calculate how many multipliers bigger than your number you can have such that$A \times B$is smaller than$N$. •  » » » 2 years ago, # ^ | 0 n is 1e6, so the inner loop runs up to 1e3 times... that are aprox 5e8 iterations. •  » » 2 years ago, # ^ | 0 spookywooky — Love the way you add inspiring quotes to your code. /** * Dont raise your voice, improve your argument. * --Desmond Tutu */  •  » » 2 years ago, # ^ | 0 Can you please elaborate solution of D a little? •  » » 2 years ago, # ^ | +3 Me too for the first time solved set in any contest lol •  » » 2 years ago, # ^ | 0 every time i used to fight myself to solve at least 3. But u guys are solving all. •  » » 2 years ago, # ^ | 0 Can you please further explain how did segment tree help with this problem? I know segment tree but I cannot utilize it to solve this problem. •  » » » 2 years ago, # ^ | +3 I assume problem D.If we would do standard knapsack we would need to loop over all the values in the K ranges, which is O(n).With the segment tree we are able to do these updates in O(log n).Note that the solution with the partial sums do these updates in virtually O(1). see here •  » » » » 2 years ago, # ^ | 0 Thanks a lot:) you always provide useful insights •  » » 23 months ago, # ^ | 0 correct me if I am wrong, the time complexity of your D solution is N*k*log(n) and not N*log(n) which the editorial says is the optimum •  » » » 23 months ago, # ^ | 0 Yes, you are right. Because of this my submission needs 800+ms instead of possible 10ms.  » 2 years ago, # | 0 How to solve problem D? •  » » 2 years ago, # ^ | 0 Spoileryou can just use range query datastructure like fenwick tree and update every index •  » » 2 years ago, # ^ | ← Rev. 2 → +7 I solved it without segment treeI used difference array. Time complexity of$O(N*K)$basically let dp[i] be no of ways to each i.Then you increment,$i$+$L_j$to$i$+$R_j$with dp[i], increment using difference array method, and keep summing as you move toward right.The answer would be dp[n]Submission •  » » » 2 years ago, # ^ | 0 Hello , I thought D problem is same as Coin Combinations I as expected get TLE for that.Anyway to improve my solution? My submission is here •  » » » 2 years ago, # ^ | 0 n, k = map(int, input().split()) dec = {} mem = {0: 1} for i in range(k): l, r = map(int, input().split()) dec[l] = 1 dec[r] = 1 dec = list(dec) for i in range(1, n): ans=0 for j in dec: ans += mem.get(i-j, 0) % 998244353 mem[i] = ans print(mem[n-1] % 998244353) please can you tell where am i doing wrong •  » » 2 years ago, # ^ | 0 I solved it maintaining a prefix sum array and using dp. dp[i] = sum of dp[j] for all j, i is reachable. As the range was contiguous it was easy to get the sum of dp[j] using prefix sum. My Sumbission •  » » » 2 years ago, # ^ | 0 could u plz explain more •  » » » » 2 years ago, # ^ | ← Rev. 2 → 0 Here dp[i] is the number of ways to reach i. dp[i] was calculated by the sum of dp[j] for all j from where i is reachable. Assume i = 4 and i is reachable from 2 and 3. If the number of ways to reach 2 is 1 aka dp[2] = 1 and the number of ways to reach 3 is 2, dp[3] = 2 then dp[4] = dp[2] + dp[3] which is dp[4] = 3 (rule or sum)And for any i corresponding js were calculated from the ranges and their sum was calculated from the the prefix sum array. For any position iand a range L, R i is reachable from all valid i-R, i-L. Finally the answer is dp[n]. Complexity O(N*K) •  » » » » » 2 years ago, # ^ | 0 Tx a lot  » 2 years ago, # | 0 I got runtime error in sometest cases in prob D with segtree can somebody help me? https://atcoder.jp/contests/abc179/submissions/16889464  » 2 years ago, # | ← Rev. 2 → 0 void test_case() { int n , x; cin >> n >> x; for(int i = 0 ; i < 2*x ; i++){ int p; cin >> p; c.insert(p); } dp[1] = 1; for(int i = 2 ; i <= n ; i++){ for(auto j : c){ if(j <= i){ dp[i] = (dp[i]%M+dp[i-j]%M)%M; } } } cout< •  » » 2 years ago, # ^ | ← Rev. 3 → +3 they want you to union the segments [Li,Ri] i mean {Li,1+Li,2+Li,3+Li,....,Ri} not only values {Li,Ri}  » 2 years ago, # | ← Rev. 3 → 0 string solve(){ ll n,x,m; cin>>n>>x>>m; vector vec; vec.push_back(0); set s; ll num=x; ll sum=0; ll count=0; while(s.count(num)==0){ vec.push_back(num+vec.back()); s.insert(num); sum+=num; num=(ll)power(num,2,m)%m; if(num==0) ret(sum); count++; if(count==n) ret(sum); } ll times=n/count; ll rem=n%count; ll ans=times*sum; ret(ans+vec[rem]); } ** what is wrong with this code for the problem E **  » 2 years ago, # | +3 Its the first time I AK abc! LOL  » 2 years ago, # | ← Rev. 2 → 0 I tried to solve D by repeating knapsack dp approach with time complexity O(N*m).But TLE destroyed my today's contest.Can anyone help me with that? •  » » 2 years ago, # ^ | +1 D can be done in O(n*k).Use range update operation on array, it will take O(n) for each range.My submission •  » » » 2 years ago, # ^ | 0 Can you elaborate this part?if(i+range[j].F<=n) temp[i+range[j].F]=(temp[i+range[j].F]+dp[i])%M; if(i+range[j].S+1<=n) temp[i+range[j].S+1]=(temp[i+range[j].S+1]-dp[i]+M)%M; •  » » » » 2 years ago, # ^ | +1 Suppose you have array 1 2 3 4 5 You want to add 2 from position 2 to 4 [1 based-indexing) You can take a temp array and make temp[2]=2 and temp[5]=-2Now you can iterate over array and take sum+=temp[i]And add sum to a[i] . You can get the required array after update in O(n) this way. •  » » » » » 2 years ago, # ^ | 0 now I get it,thanks....  » 2 years ago, # | ← Rev. 2 → 0 This is for C: Here we have simply calculate number of factors of (n-c) where 1<=ca*b=n-c;  » 2 years ago, # | +3 Eagerly waiting for geothermal's editorial.  » 2 years ago, # | ← Rev. 2 → 0 Solution of C : https://ideone.com/gpZjgX •  » » 2 years ago, # ^ | 0 server not found :(  » 2 years ago, # | 0 Hello in D no I used the similar pattern of dp like CSES-Coin Combinations I. but get TLE from that.Isn't it the similar pattern problem.My solution is here •  » » 2 years ago, # ^ | 0 Thats' so because the time complexity of your solution is O(n*n). •  » » » 2 years ago, # ^ | 0 I thought it's like Coin Combinations I problem.My bad.How can I solve it using dp ? •  » » » » 2 years ago, # ^ | 0 Well, usually i solve 4-5 problems in ABC contest but today i was able to solve only the first three!  » 2 years ago, # | +1 Could anyone please explain the 1 testcase this is failing on? I have tried to find a cycle and then adding to the sum the sum of that cycle and then adding the rest seperately.Submission •  » » 2 years ago, # ^ | 0 nvm, fixed it, was not solving correctly for when the cycle would end without it ever repeating. fixed code  » 2 years ago, # | 0 //Beginner Problem E Runtime Error for few cases accepted for others! please help me why? Your code here... ~~~~~ ~~~~~ Your code here...  include using namespace std; unordered_map mp; long long func(long long x, long long n, long long mod) { if (n == 0) return 0; if (mp.find(x) != mp.end()) return mp[x]; // cout << x << endl; long long ans = x + func((x % mod * x % mod) % mod , n - 1 , mod); return mp[x] = ans; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long n; long long x, m; cin >> n >> x >> m; long long ans = func(x, n, m); cout << ans; }   » 2 years ago, # | 0 how to solve D? someone with good explanation? •  » » 2 years ago, # ^ | ← Rev. 2 → 0 You can use this dp: dp[i] means how many ways do you have to go to the i-th cell. Let's get an array of long long dp[2n]. First we fill it with zeros. Denote the sums[2n] as prefix sums array of dp. dp[n] = 1, sums[n] = 1 for each cell from n+1 to 2*n and each segment (l[j], r[j]) we calculate: dp[i] = (mod + dp[i] + sums[i - l[j]] - sums[i - r[j] - 1]) % mod You can understand that part as: to get how many ways I have to go to the i-th cell, I should sum the ways from all dp[the previous one, from which you can get into this].The value sums[i - l[j]] - sums[i - r[j] - 1] gets us sum: dp[i - r] + dp[i - r + 1] + ... + dp[i - l] •  » » » 2 years ago, # ^ | 0 Why are you taking dp[2*n] and not dp[n] or dp[n+1]? •  » » » » 2 years ago, # ^ | 0 Just because I don't want to care about bounds •  » » » » » 2 years ago, # ^ | 0 Thanks I implemented it and got AC. if anyone wants to know what he means Here is my clear cut solution with more explanation. https://ide.geeksforgeeks.org/lpwZlbNN8D  » 2 years ago, # | ← Rev. 2 → +11 And the Chinese version.  » 2 years ago, # | 0 how do you solve c? I got TLE my solution is only O(n^2) •  » » 2 years ago, # ^ | 0$O(N^2)$is too large for$N=10^6$. •  » » » 2 years ago, # ^ | 0 How can i know that it's large or not? •  » » » » 2 years ago, # ^ | 0 You just put N into the time complexity expression and evaluate it. Generally speaking,$10^7$is totally acceptable, while$10^8$can be a bit dangerous (on some OJs it cannot pass), and$10^9$is almost impossible to pass the time limits.However, sometimes we also need to consider the constant, which is omitted in the time complexity expression. •  » » 2 years ago, # ^ | 0 can somebody please explain the logic for c. n = int(input()) cnt = 0 for i in range(1, n): cnt += (n - 1) // i print(cnt)  •  » » » 2 years ago, # ^ | ← Rev. 2 → 0 Since N is fixed, we don't need to enumerate all possible triplet of (A, B, C), we only need to enumerate (A, B) and compute C accordingly. Such method can be improved by enumerating only A and find the upperbound and lowerbound of B. All the values between lowerbound and upperbound are valid B. Note that B should be integer.$0 \lt A \cdot B = N - C \lt N \implies 0 < B < N/A$N = int(input()) ans = 0 for A in range(1, N): lb = 1 ub = math.ceil(N / A) - 1 ans += ub - lb + 1 print(ans)  •  » » » » 2 years ago, # ^ | 0 yeah.. got the logic thanks. •  » » 2 years ago, # ^ | 0 You can solve C in O(n) or O(nlogn) time complexity.Both of them will pass pretty convincingly.However the nlogn version would require a bit of precomputation You can refer to my O(nlogn) solution (I use Java)https://atcoder.jp/contests/abc179/submissions/16892756  » 2 years ago, # | 0 I believe F can be solved using monotonic set  » 2 years ago, # | 0 Can any one help me figure out how to solve D using recursive dp? I do not quite understand the iterative dp solution.  » 2 years ago, # | ← Rev. 3 → +5 I have written an unofficial English editorial.you can find it here.UPD: Added editorial for problem F. •  » » 2 years ago, # ^ | +5 In the explanation for C : Could you please explain how the number of ways of choosing is N/A ?I have tried to do it on paper but couldnot come up with the intution. •  » » » 2 years ago, # ^ | +5 It should be$\frac{N - 1}{A}$fixed it now, thanks for notifying me.The reason for that is for every$A$there can be$\frac{N}{A}$numbers for be such that$ A \times B \le N$, however we should subtract$1$because$C$can't be equal to zero. •  » » » » 2 years ago, # ^ | 0 what is the range for c? is it 1<=c<=n-1?  » 2 years ago, # | ← Rev. 2 → 0 For problem C i counted the number of factors of n-c In order to precompute number of factors for numbers in range [1,1e6]. I tweaked seive like this: vi factorize (vi v){ v[0]=0; v[1]=1; for(int i=2;i=1 for(int i=2;i •  » » 2 years ago, # ^ | 0 My solution was quite similar to yours.You can check my solution for reference(I use Java) https://atcoder.jp/contests/abc179/submissions/16892756 •  » » 2 years ago, # ^ | 0 Can you link your submission for that problem •  » » » 2 years ago, # ^ | 0 I couldn't debug this so i didn't submit :( •  » » » » 2 years ago, # ^ | 0 You can check my solution. •  » » » » » 2 years ago, # ^ | 0 Nice solution. orzCan you please explain the while(num>1) part in your solution? Thank You •  » » » » » » 2 years ago, # ^ | 0 if num<=1 then the number has no prime factor.So as long as it greater than 1 the loop will run. •  » » » » » » » 2 years ago, # ^ | 0 So you're using this formula am I right?Let n = (p1^a1)*(p2^a2)*...(pn^an) where p1,p2,..,on are prime numbers And a1,a2,..,an are powers of primes Then number of factors of n = (a1+1)*(a2+1)*(a3+1)*...*(an+1) •  » » » » » » » » 2 years ago, # ^ | 0 Yes I am using this formula to count the number of distinct divisors and add to the count. •  » » » » » » » » » 2 years ago, # ^ | 0 Yes Thank You very much for helping me.I didn't want do it this way it's tedious to code and prone to error because I am bad at programming(I knew the formula but couldn't have implemented it the way you did it). So I tried to go around in order not to implement it and couldn't solve it. •  » » » » » » » » » 2 years ago, # ^ | 0 Welcome.You can follow CodenCode NumberTheory lectures to get better at solving these type of problems.I am learning from there. •  » » » » » » » » » 2 years ago, # ^ | 0 Thank You  » 2 years ago, # | ← Rev. 3 → 0 UPD: Corrected ...at the time of init of seg tree i increased size by(n+2) Problem D just failing one testcase...can anyone help me out ?? I applied segment tree in this code..  » 2 years ago, # | 0  » 2 years ago, # | 0 Problem D was quite interesting.  » 2 years ago, # | 0 Can E be solved with Matrix exponentiation??  » 2 years ago, # | ← Rev. 3 → 0 I tried to solve E,but for some unkown reason,I got two RE.Can you help me? Code`#include #define int long long using namespace std; int n,mod,x,ans; pair a[100005]; int a2[100005]; signed main() { cin>>n>>x>>mod; x%=mod; int pos=0,len=0,pos2=n,sum=0; for(int i=1;i<=n;i++) { ans+=x; a[x].first=1; a[x].second=i; a2[i]=x; x=(x*x)%mod; if(a[x].first==1) { pos=a[x].second; pos2-=i; len=i-pos+1; for(int k=pos;k<=i;k++) sum+=a2[k]; break; } } ans+=(pos2/len)*sum; pos2%=len; for(int i=1;i<=pos2;i++) { ans+=x; x=(x*x)%mod; } cout<  » 2 years ago, # | 0 I couldn't even understand D,Please correct me if I am wrong in my Interpretation:We are given K ranges. After union of all those segments, we will have distinct distances.So we need to find number of ways to reach from 1 to N using these distances.Please help me  » 2 years ago, # | +3 Editorial DHow to solve in O(nlogn) when the transitions cannot be written as a sum of small number of segments? •  » » 2 years ago, # ^ | 0 Wondering the same thing, please let me know if you find the answer. •  » » 2 years ago, # ^ | ← Rev. 2 → +8 [ignore this, too much complex] •  » » 2 years ago, # ^ | ← Rev. 2 → +12 let$J$be the set of possible jump lengths, now$ J = \cup[l_i,r_i] $Now we can use generating function$ G $to define that set.$G = \sum_{i=0}^N c_i x^i $where$ c_i = 1$if${i \in J}$else$c_i = 0$. Now, number of ways to reach from$1$to$N$using exactly$k$jumps =$[x^{N-1}] G^k$. As there is no restriction of jumps,so we sum it over all possible$k$. so, finally what we need is$[x^{N-1}]\sum_{k>=0} G^k = [x^{N-1}]\frac{1}{1-G}$now coefficient of$ x^{N-1} $in the polynomial$\frac{1}{1-G}$can be easily calculated by calculating inverse of polynomial$ 1 - G $restricted to max degree$N$. Overall complexity is$O(N\log{}N)\$ For calculating inverse of polynomial you can refer Operations on polynomials and series
 » 2 years ago, # |   0 Editorial FCould anyone help me understand the approach. I am not able to get it. Thanks!
 » 2 years ago, # |   0 I am getting stuck in mod operations. Getting WA for few cases. Can someone take a look at the mod operation and let me know if you find the error.Link to my submission: https://atcoder.jp/contests/abc179/submissions/16925676Thanks!
 » 2 years ago, # |   0 ABC turning into AGC :/
 » 2 years ago, # |   0 How to apply binary search to D?
 » 2 years ago, # |   0 In problem C, why is this #b =(n-1)/2. Precisely, why we have to subtract 1?
 » 23 months ago, # |   0 I could not understand the editorial of the problem C.Can anyone explain it?