### chokudai's blog

By chokudai, history, 7 days ago, We will hold AtCoder Beginner Contest 178.

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

We are looking forward to your participation! Comments (253)
 » Love atcoder beginner contest
 » Reminder: Contest starts in 10 mins!
 » Are there any other contest on atcoder for beginner? ABC are very good but aren't that frequeant and i tried AGC and i shouldn't have... so any other recommendation
•  » » The ARC's should be doable, they're gonna be frequent again soon.
 » 7 days ago, # | ← Rev. 2 →   deleted
•  » » Inclusion Exclusion. Number of ways that we can pick any value from 0 to 9 = 10^N . Number of ways that 0 doesnt occur = number of ways to pick any value from 1 to 9 = 9^N. By same logic, number of ways that 9 doesn't occur anywhere = 9^N Number of ways that both 0 or 9 don't occur = 8^N Hence answer is 10^N — 9^N — 9^N + 8^N . Use mods wherever required
•  » » » 7 days ago, # ^ | ← Rev. 2 →   mnist why u added 8^N instead of subtracting it from 10^N
•  » » » » its because in both the the 0 and 9 case, the case of both 0 and 9 is considered and thus has been subtracted twice instead of just once.
•  » » » » » got it now....thanks bruh :)
•  » » » » when you take [0,8] by missing 9 and [1,9] by missing 0 — you double counted and get minused the value [1,8] twice that's why add back 8^N
•  » » » I tried (nC2)*2*10^(n-2). nC2 to choose any two places for 0 and 9. 2 because {0,9} has two permutations. 10^(n-2) to fill remaining places with 0-9. I am still not sure why this approach is wrong. Could someone point out?
•  » » » » You are counting duplicates for example,when 0,9 is fixed like this 0_9 and like this 09_ in both cases you are counting 099
•  » » » » I also tried this same approach. Don't know what went missing :')
•  » » » I am not understanding the line "Number of ways that both 0 or 9 don't occur = 8^N.".Here we are finding the number of wayes without 0 and 9 so i think there may be 0 and 9. Can you help me to find out my error?
•  » » » » Yes it's and, anyways understable from the calculation.
•  » » » 7 days ago, # ^ | ← Rev. 2 →   I thought that we could first choose one 0 in the sequence so Nc1 ways to do that and then choose one position for 9 in N-1c1 ways and then for remaining N-2 positions each has ten options so 10^(n-2) ways so my final answer was n*(n-1)*10^(n-2) ..My answer got WA.Can anyone please point out what is wrong in my reasoning.Any help or feedback is appreciated
•  » » Why so many downvotes he is just asking the approach didn't say anything wrong or asked anything wrong U can just use Inclusion exclusion
•  » » » He asked during contest which is not allowed
•  » » » » I was just try to be first so i can get reply so i can get reply quickly without having intention to get solve yes i can give these immediate after contest but i have a online class during the contest for that i gave that during the contest to be honest i have no intention to get a solve from here during contest
•  » » » » » The issue is that even if you weren't going to cheat yourself, if someone answered you during contest, anyone else could have seen the answer too, which wouldn't have been fair.
•  » » In this question you need to find all sequences so: There is some i such that Ai = 0 (count of such i may be > 1) There is some i such that Ai = 9 (count of such i may be > 1) (both conditions must be met)Let's iterate k — number of positions i such that Ai = 0/9Number of ways to create "good sequences" with 0/9 is 2^k — 2 (on every position you have 2 ways to put 0/9) (-2 because you don't need to create sequences 00..000 and 99..999)Let's count C(n, k) — the number of ways to select a set of i positions from n positions, where we want to put 0/9Finally we want to fill the remaining positions with number 1..8. It is equal to 8^(n — i)And final number of ways for some k equal to C(n, k) * (2^k — 2) * (8^(n — i))P.S. more details about Binomial Coefficients you can read here Binomial Coefficients
 » Any ideas on how to do F? If the frequency of any number in first array is greater than n — (frequency of that element in the second array) than answer is "No" else answer exists.I made a logic that the maximum frequent number in the first array will get the least frequent number from the second array. If there are multiple numbers with the same frequency then the smallest number with the smallest frequency goes to the largest number with the largest frequency. I was not quite able to implement it.
•  » » Contest has not finished yet
•  » » I understand, I thought there's just 1 minute left which won't really make any difference. One cannot implement it that quick. Sorry though I should have been a bit more patient
 » Solved E but coudnt solve C and D :(.
•  » » approach for e
•  » » » There are only two cases to consider, if we only consider results such that Xi <= Xj. If Yi <= Yj, then the distance is (Xj + Yj) — (Xi + Yi) Otherwise, the distance is (Xj — Yj) — (Xi — Yi) By breaking it down into these cases, I have gotten rid of the absolute value function making it much easier to reason about the distances.So, we simply pick points with minimum and maximum x+y, and compute the distance. Then pick points with minimum and maximum x-y, and compute the distance. One of those two distances is your maximum.This can be done in O(n).My submission
•  » » » » 7 days ago, # ^ | ← Rev. 2 →   Copy-pasted from: stackoverflow Why can't you provide the link directly instead of copy pasting the text XD
•  » » » » » So people dont have to visit stackoverflow and look for answer.
•  » » » » » its simple as you prob. dont want to jump from one site to another for a simple reason ,so posting a simple basic explanantion is not that big of issue
•  » » » The manhattan distance of two points is the distance of the parallel diagonals of these points.The points at a distance x from a given point p form a diamond.
 » How to do D?
•  » » 7 days ago, # ^ | ← Rev. 2 →   Simple DP. very similar to thisJust coins here are {3, 4, 5 .. N}
•  » » » But using coin change dp method does not give us the different permutations right? Like for sum = 7. the answer = 3 ( {3,4} , {4,3} , {7} ). But dp gives us only answer = 2 ( {3,4} , {7} ). Please correct me if I'm wrong
•  » » » » 6 days ago, # ^ | ← Rev. 2 →   Imagine filling DP array for n = 7 by this code: Spoiler // dp[i] = how many ways to get this sum dp = 1; // there is one way to get sum 0: take nothing for(sum=3; sum<=n; sum++){ // what sum do we want to get for(coin=3; coin<=sum; coin++){ // going through the coins, note that coins bigger than sum will not be considered because we can't get that sum using it. for example it's impossible to get sum of 5 using atleast once coin 7 dp[sum] += dp[sum-coin]; dp[sum] %= mod; // taking mod } } cout << dp[n]; dp array at the end will be: dp = 1; // {0} dp = 0; // {} dp = 0; // {} dp = 1; // {3} dp = 1; // {4} dp = 1; // {5} dp = 2; // {3+3, 6} dp = 3; // {4+3, 3+4, 7} why 4+3 gets considered? answer: imagine sum = 7, coin = 3 the number of ways to get sum 7-3=4 will be added to dpHope you understood
•  » » » » » Awesome! I understood now...thanks for the efforts :)
•  » » As we cant take 1's and 2's, our number of ways to form i is : dp[i] = dp[i-3] + dp[i-4] + .... Snip ll s,i; cin>>s; vector dp(s+1,0); dp=1; for(i=3;i<=s;++i){ for(j=i-3;j>=0;--j) (dp[i]+=dp[j])%=mod; } cout<
•  » » I did it using Combinatorics, Stars and bars. Divide the number into x sums, (1 to s/3) and for each, find all possible combinations, by keeping each of it exactly 3 initially.
•  » » » I also did the same,this approach is bit intuitive as this revises the P&C concepts :)
•  » » » » Yeah, but dp would be a lot easier.
•  » » » Could you elaborate more on this approach?
•  » » » » 7 days ago, # ^ | ← Rev. 2 →   Sure. Firstly let's notice that we can only have sequences of size from 1 to s/3. (because each one should be at least 3) Then, let's call these "number of groups" as x, example: For s=9, we have, x=1, {3} ; (with 6 still remaining) x=2, {3,3} (with 3 still remaining) x=3, {3,3,3} (with 0 remaining) Now, we can distribute the remaining sum, for each x, among the x groups, in (n+r-1)C(r-1) ways, This is, distributing n identical objects among r people (stars and bars). So iterating over each x from 1 to s/3 by calculating the combination for each iteration, it's easy to find the answer. Submission
•  » » » » » thank u a lot
•  » » It’s just a simple coin change dp problem.
 » How to solve E? I tried to find smallest and largest points on x and y axis(total 8 points) and found maximum among them.
•  » »
•  » » » Thanks! Completely forgot to break mod, went in the wrong direction. :(
•  » » 7 days ago, # ^ | ← Rev. 3 →   I removed the mod (absolute value) by considering 4 cases.case 1 : xi > xj and yi > yjwe need to find now max(xi-xj + yi-yj) = max((xi+yi)-(xj+yj)) = max(xi+yi)-min(xj+yj)for other cases I multiplied coords by -1 and solved by case 1 againcase 2 : multiply all x by -1case 3 : multiply all y by -1case 4 : multiply all x and y by -1
•  » » 7 days ago, # ^ | ← Rev. 3 →   Manhattan distance between two points is: |$x$a — $x$b| + |$y$a — $y$b|. Now if can be evaluated in 4 ways: $dist$ = ($x$a — $x$b) + ($y$a — $y$b) $dist$ = ($x$a — $x$b) — ($y$a — $y$b). $dist$ = -($x$a — $x$b) + ($y$a — $y$b). $dist$ = -($x$a — $x$b) — ($y$a — $y$b).Now if you simplify them a little then the distance can be one of the two below: 1) $dist$ = ($x$a + $y$a) — ($x$b + $y$b) 2) $dist$ = (-$x$a + $y$a) + ($x$b — $y$b)Now the distance will be maximum from above expressions. submission link
•  » » » Wow, nice breif explanation.
•  » » » Thanks. Here is shorter implementation I did with your help.
•  » » » » Oh, i see. It's really concise. 80% of time i overkill (: problems.
•  » »
•  » » the approach to solve was same as this question.you were just given distance function instead of f(i, j) codevoid solve() { int n, x, y; cin >> n; vector sum(n), diff(n); for (int i = 0; i < n; i++) { cin >> x >> y; sum[i] = x + y; diff[i] = x - y; } int maxone = INT_MIN, maxtwo = INT_MIN; int minone = INT_MAX, mintwo = INT_MAX; int ans = INT_MIN; for (int i = 0; i < n; i++) { maxone = max(maxone, sum[i]); maxtwo = max(maxtwo, diff[i]); minone = min(minone, sum[i]); mintwo = min(mintwo, diff[i]); ans = max({ans, maxone - minone, maxtwo - mintwo}); } cout << ans; } 
•  » » Alternatively, you can calculate the convex hull of the given points and brute force the vertices of the polygon.
•  » » » This will TLE if to much points are on the convex hull, it could be all.
•  » » » » 7 days ago, # ^ | ← Rev. 2 →   Yes, you're right. However, I got an Accepted verdict. I guess the test cases are not strong enough.
 » Pure Mathematics Contest!!
•  » » A and B was no math ;)
•  » » and D was also of dp ..No math
•  » » » You could do it with stars and bars
 » Maybe E is just a too well known problem, but F solution was more obvious to me than E solution.
•  » » how to solve F ? It seems easy at first.
•  » » how to solve F ? Think about 70 mins and more but still fail to figure
•  » » What was the approach for F?
•  » » Can you please explain your solution to E, I don't know which one test case was failing, I got 17 AC and 1 WA
•  » » » This might help https://atcoder.jp/contests/abc178/submissions/16705345 I sorted the points considering points to be equal if they lie in the same circle centered at the origin
•  » » » Put three comparators. one comapring x1-y1 other with x1+y1 one with y1-x1 Take the max of difference of the end terms and max of three would be the answer.
•  » » » Here is my take on E,ans=(x2-x1)+abs(y2-y1) ,x2>x1Now we have two options for answerans1=(x2+y2)-(x1+y1) ==== max(x+y) -min(x+y) for optimal answerans2=(x2-y2)-(x1-y1) ==== max(x-y) -min(x-y) for optimal answer.Final answer would be the max of two.
•  » » » » Hey can you please suggest which one test case is failing for this solution
•  » » » » » 6 days ago, # ^ | ← Rev. 5 →   Try this2392850397 652442710430284160 923935256Correct answer:308926309Your answer:493651095
•  » » » » » Sorry for prvious wrong case.I updated it. Please check it,Sorry for my bad English.
•  » » » » I have a doubt. The two cases are coming from assumptions, like case 1 comes from y2 >= y1. So how is it that when we calcuate the answer, we don't check for these restrictions and still get the correct answer. Can't it be that for i and j which gives max(xi + yi) — min(xj + yj), yi is actually less than yj which makes that pair invalid?
•  » » » » » Actually I have this same doubt and I have used the same assumptions that invalid pairs wont affect the best answer for the problem and got a AC, I was really trying to find a intuitive or mathematical proof for the same but havent got one, I would really appreciate if someone comes up with a proof for the same.
•  » » » » » Here we are only concerned about the largest valueLets get our definitions clear firstoption1 = (xj-xi) + (yj-yi) for yj>=yioption2 = (xj-xi) + (yi-yj) for yi>=yjNow for yj>=yi we can never have option2>option1 and vice versa.So though we are considering some invalid pairs our answer is never going to be affected
•  » » » » » » Nice. Thanks!
•  » » Can you check why I am getting WA on F. here is submission
•  » » More than being well known, E is pretty easily searchable I think.
 » MathCoder literally :V
 » What are the solutions for problems E and F?
•  » » e is well-known, u can google it
•  » » » I googled couldn't find the same problem, can you please provide a link where solution of this problem is explained. like gfg or something?
•  » » » » hereyou're welcome :))
 » Hi, this is my code for F problem I am failing in 3 test cases and I couldn't figure out those TCs,you can find my code in this link please help mehttps://ideone.com/7PaZ9kMy submission link https://atcoder.jp/contests/abc178/submissions/16710181
•  » » can I get any test case for which my code is failing?
•  » » » there is dropbox link to all atcoder test cases in one of the rng_58's blog
 » Can someone share his approach for solving F?
 » F solution: Reverse array B, then A will be in ascending order and B will be in descending order. If we now look at positions where A[i] == B[i], they will form a segment [l, r], and will all have equal element A[i] == B[i] == C. Then you just need to swap all positions i from this segment with such positions j that A[j] != C and B[j] != C. And if there are not enough such positions j then I think it's not hard to prove that reordering is not possible at all.
•  » » I think reordering is not possible only in the case where lets say x occurs m times in A and k times in B and both k>=(n+1)/2 and m>=(n+1)/2. By pigeonhole principle!
•  » » » No consider A = [1 1 1 1 2] B = [1 1 2 3 4], maybe we can say that frequency of some element must obey $f_A + f_B <= n$ ...
•  » » » » Ya I missed it thanks , Amazing F!
•  » » » » How to prove that answer is always Yes when every f_A + f_B <= n
•  » » » » » That is shown from answer of madn. When A is ascending and B is descending, and lets say $f_A + f_B = N$ for boundary condition, maximum intersection is possible if both A and B have near equal frequencies, intersection will be of length $\lfloor N/2 \rfloor$. So we have adequate numbers ($\lceil N/2\rceil$) to put in this positions of overlap, so answer is always possible
•  » » » » » » oh,actually its very obvious...what am i thinking...Thanks!
•  » »
•  » » » great idea man
•  » » This is the best solution I have ever read!!
•  » » » Yeah I find it very simple and easy to see correctness. In my contest I was just wondering why they gave A, B in sorted order, maybe it was hint toward this solution
•  » » » Yes, key insight is that only a[i] = b[i] = c will be the only value, and form an interval
•  » » » » Can you prove it ?
•  » » » » » 7 days ago, # ^ | ← Rev. 4 →   if there are two different value that a[i] = b[i], let a[i] = b[i] = c, a[i+1] = b[i+1] = d, and c != da[i] < a[i+1], so d > cb[i] > b[i+1], so d < ccontradiction
•  » » » » » » Amazing!!!!!!
•  » » 7 days ago, # ^ | ← Rev. 2 →   I am not claiming following is correct but I also cannot see why it is wrong. It must be wrong because it gives runtime error. The process was: Find net frequency of each element in the two arrays. If it is greater than n, then it is not possible to rearrange. Otherwise we use a max-heap to store pair of {"frequency in B", "number"}. Now we loop A from 1 to N and simply pop from heap and check it with A[i]. If not equal then we place it in our final array. Otherwise we pop another value and place it in final array. We push back the changed frequency. I think it runs into dead end where it no longer finds a suitable pair, but why it happens? Thanks!
•  » » i think thats the easiest code to read and understand.
 » Man problem C ruined the round for me!:) Can anyone please explain the approach for it?
•  » » 10^n-2*9^n+8^n Inclusion exclusion
•  » » » Wow, thanks! Very interesting!
•  » » » Can you please help me to understand what wrong with this approach.Take two numbers and assign 0 and 9 to them , ways=n*(n-1)remaining (n-2) numbers can be arranged in 10^(n-2) ways making ans=n*(n-1)*10^(n-2)Thanks.
•  » » » » The 10 ^(n-2) replicates in many cases when n>=3 see lets say you place 0,9 at 1 2 so possible values are 0 9 1, .....0 9 9 SImilarly suppose in another permutation you fixed 0 9 at position 1 3 and other 10 values accordingly at remaining position .Observe both the vectors carefully.0 1 9........0 9 9.You can clearly see 0 9 9 appearing common in both thus WA!
•  » » » » There will be repeation, like in your solution you will count (2 1 9 2) twice.
•  » » » 7 days ago, # ^ | ← Rev. 2 →   Intuition behind this:There are $x$ ways of choosing which of $x$ numbers to place in a spot, each choice is independent of each other, so for $n$ places there are $x^{n}$ ways.Number of ways of placing numbers in the range $[0, 9]$ -> $10^{n}$.Number of ways of placing numbers in the range $[0, 8]$ or $[1, 9]$, that is the number of ways of generating an array that is guaranteed to be missing either a $0$ or an $8$ -> $9^{n}$ each.But note that we have double counted the number of ways to place $[1, 8]$ (both missing) while removing the bad array count, so add back the number of ways of doing that which is $8^{n}$.
•  » » 7 days ago, # ^ | ← Rev. 2 →   You can use DP for accounting for double counting
•  » » » Thanks! I tried to do a dp approach but i didn't manage to solve it during the contest! In my opinion, D was easier than C.
•  » » » » 7 days ago, # ^ | ← Rev. 2 →   Exactly! I was stumped by C as well. Here's my DP solution to C Code#include using namespace std; #define int long long const int M = 1e9+7; vector>dp; void solve(){ int n; cin>>n; dp.assign(n+2,vector(4,0)); dp = 1; for(int i=1;i<=n;++i){ dp[i] = ((dp[i-1]+dp[i-1])%M+10*dp[i-1]%M)%M; dp[i] = (dp[i-1] + 9*dp[i-1]%M)%M; dp[i] = (dp[i-1] + 9*dp[i-1]%M)%M; dp[i] = 8*dp[i-1]%M; } cout << dp[n] << endl; return; } int32_t main(){ int t=1; while(t--){ solve(); } return 0; } 
•  » » » » » Nice solution!
•  » » » » » Please, can you explain your solution? I couldn't get it.
•  » » 7 days ago, # ^ | ← Rev. 2 →   Answer = (total sequence with length n and all element <= 9) — (total sequences such that it contains neither 0 nor 9)first term above is simply pow(10,n) for the second term I used inclusion exclusion:A = Set of sequences that doesn't contain 0B = Set of sequences that doesn't contain 9n(A union B) = n(A) + n(B) - n(A intersection B)n(A) = n(B) = pow(9,n)n(A intersection B) = pow(8,n)
•  » » » Nice explanation, thank you!
•  » » » I am still confused with duplicates. Can anyone please write all the sequences if n = 3 ? Thanks in advance.
•  » » » » answer for 3 is 54 which is too big to enumerate manually. But if you are asking why pow(10,n-2)*C(n,2) is not right answer, I'll try to show duplicates. Suppose n = 3 and you decide to fix first and third position one of the sequence you get is 009 which is repeated when you fix second and third position, thus counting same sequence twice.
•  » » » thanks, it helped.
•  » » » nice explanation!!
•  » » 6 days ago, # ^ | ← Rev. 2 →   deleted
 » 7 days ago, # | ← Rev. 3 →   what's wrong in my solution of F? getting wrong answers on 6/50 test cases?https://atcoder.jp/contests/abc178/submissions/16721049UPD: I did some changes in my above code but still it's giving wrong answer on 3 test-cases :( https://atcoder.jp/contests/abc178/submissions/16724818
 » Anyways to do C or D without DP ?
•  » » C is simple inclusion — exclusion. My one line code : cout << sub(add(power(10, n), power(8, n)), add(power(9, n), power(9, n)));
•  » » 10^n-2*9^n+8^n
•  » » D could be solve by brute force the number of elements in the sequence and the do a star-and-bar-like counting trick.
•  » » My approach of D (Without dp ) : First of all fix the length of the sequence . Suppose length is 5 ; so a1 + a2 + a3 + a4 + a5 = S ; now for all i , a[i]>=3 ; using stars and bars technique , no of solution of that equation is ncr(s-3*length +length -1 , length-1);here length = 5 ; final answer = sum of solutions for each fixed length .  Codeint ans=0; for(int len=1;len<=s;len++) { if(s<(3*len))continue; int sum=s-3*len; int yo=ncr(s-(3*len)+len-1,len-1); ans=(ans+yo)%mod; } cout<
•  » » » AK. Well explanation brother.
•  » » 7 days ago, # ^ | ← Rev. 2 →   I did D using combinatorics. It is the modified version of distributing n balls into r urns such that each urn contains at least 1 ball. Solution of standard problem: (n-1)C(r-1).Here problem can be reformulated as if you can use any number of urns (because the length of sequence can vary) and each urn should contain at least 3 balls. Solution: summation of (n-1-2*i)C(i-1) for all i, where i is the number of urns and n is the sum we want. The term -2*i because we first put 2 balls in every urn so that problem is now transformed into a standard one containing at least 1 balls, for each i.https://atcoder.jp/contests/abc178/submissions/16705277
 » For problem C: I'm thinking like this, place 0 and 9 in any two places (nC2) then other n-2 places will have 10 possibilities hence (10^(n-2)). So overall answer is nC2 *(10^(n-2)).Can anyone say what fact am i assuming wrong/missing. Thanks :)
•  » » nC2 * 10^(n-2) will duplicate
•  » » » can you give correct combination formula for this problem ?
•  » » » » I used dp https://atcoder.jp/contests/abc178/submissions/16709001 Here, dp[n] means sequence of length n and 0 zeros and 0 nines, dp[n] means sequence of length n and 0 zeros and atleast 1 nine, dp[n] means sequence of length n and atleast 1 zero and 0 nines, dp[n] means sequence of length n and atleast 1 zero and atleast 1 nines
•  » » » » » thanks
•  » » » so the 0's and 9's are getting duplicated?
•  » » » » In cases when you put 9, 0 like this: ab90 09cd sequences may coincide, when a = 0, b = 9, c = 9, d = 0: 0990 0990
•  » » There are repetitions in your formula. For n = 4 the answer is 974. Check with your submission
•  » » I also thought of it at first but if you think about it cleary it will have places with repetitive counting.for example, consider n = 5 and you choose places 0 9 8 0 9 will be a valid sequence but it will be counted twice (as nc2 will include 1&2 and 4&5 ).
•  » » in this way you will count some sequences more than once lets say that n=3 one of possible way is to put 10 as a first element and 9 as a last element and lest say that the element in middle is 10 so the sequence is 10 10 9 another possible way is to put 10 as a second element and 9 as a last element and lest say that the first element is 10 so the sequence is 10 10 9 again and we already count it
 » atdynamicprogrammer
 » lose again because of my speed
 » Was trying to do D , using DP but gave TLE... https://atcoder.jp/contests/abc178/submissions/16721075 Can someone pls help what is wrong
•  » » Index is not necessary in the state.
•  » » » Damn I messed up !!!, after removing the index state got AC....Thanx
 » 7 days ago, # | ← Rev. 3 →   Editorial: Acout<2*n,the answer is "NO". Otherwise the answer is "YES".You can construct it as follow.We can find all the positions that a[i]=b[i],and store them in one "vector".We can let count[i] = the number of times that i appear in the vector.If for all i that count[i]*2<=vector.size(),we can construct them like this probelm:https://codeforces.com/contest/1381/problem/COtherwis we can swap the most appearing elements with other positions that are not in the vector,to satisfy the condition above.
•  » » 7 days ago, # ^ | ← Rev. 2 →   In $C$ constraints are smaller, so if someone has weak combinatorics(like me) then dp is more intuitive imo. here is my sol using dp: Spoiler#include using namespace std; constexpr int mod = 1e9 + 7; struct M { unsigned v; M(long long a = 0) : v((a %= mod) < 0 ? a + mod : a) {} M& operator+=(M r) { if ((v += r.v) >= mod) v -= mod; return *this; } M& operator-=(M r) { if ((v += mod - r.v) >= mod) v -= mod; return *this; } M& operator*=(M r) { v = (uint64_t)v * r.v % mod; return *this; } }; uint32_t add(M a, M b) { return (a += b).v; } uint32_t mul(M a, M b) { return (a *= b).v; } uint32_t sub(M a, M b) { return (a -= b).v; } int n; const int maxn = 1e6; int dp[maxn]; int go(int id, bool zero, bool nine) { if (id == n) { return zero & nine; } int &ref = dp[id][zero][nine]; if (~ref) { return ref; } int ans = 0; // 8 ways to place a numbers from [1, 8] ans = add(ans, mul(8, go(id + 1, zero, nine))); // place a 0 here ans = add(ans, go(id + 1, 1, nine)); // place a 9 here ans = add(ans, go(id + 1, zero, 1)); return ref = ans; } void solve() { memset(dp, -1, sizeof dp); cin >> n; cout << go(0, 0, 0) << '\n'; } signed main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); solve(); return 0; } 
•  » » » Yeah, dp can also work!
•  » » Hey, in problem D, I think you also need to add 1 to dp[i].
•  » » » Fixed!
 » Is there a case to be handled for mod of negative numbers in C ?
•  » » if a<0 then use mod-abs(a) will work.
•  » » Don't forget to add mod when you are subtracting two numbers under mod.
•  » » » I just have learnt a very valueable lesson.
•  » » we can do this : ans = (a-b+MOD)%MOD;
•  » » » How does it work?Can you please explain or give me any reference?
•  » » » » I'll leave that for you to figure out :)
 » And.. AtCoder becomes MathCoder.
•  » » Lol
 » c explanation ?
•  » » Total Number: 10^nWithout digit '9': 9^nWithout digit '0': 9^nWithout both: 8^nFinal Result: 10^n — 9^n — 9^n + 8^n
•  » » » :O i feel dissapointed about myself
•  » » » Too simple to understand!!
•  » » Total numbers you can make using all digits (10^n) — numbers with all digits but 9 (9^n) — all but 0 digits (9^n) + all but 0 and 9 (8^n) (because they were deleted twice);
•  » » Total possible sequences = 10^N. Sequences with no 0 = 9^N. Sequences with no 9 = 9^N.Subtract sequences with no 0 and sequences with no 9 from total possible combinations. But observe after doing so you will remove sequences with no 0 and no 9 twice. So add sequences with no 0 and no 9Sequences with no 0 and no 9 = 8^N.Ans = 10^N — 2*9^N + 8^N.
 » Maybe this is an off-topic.I think Atcoder should show counts of solve for each problem in dashboard/tasklist(like CF). I know it is in the standings page, but viewing standings itself some number of times is well-disgusting.
 » I have tried to solve F using bipartite graph matching but it failed just in test 16 :)))
•  » » WA or TLE?
•  » » » TLE, actually I was expecting it! I was checking a random edge to find a match each time.
•  » » I don't think it can work , the nodes and edges are too many !
•  » » » 7 days ago, # ^ | ← Rev. 2 →   It worked :)) submmision #16726993I guess test-cases are not that much strong.
•  » » of course it will :))))
•  » » maybe you can try using a segment tree to build the graph.
•  » » » But the nodes are too many :)
•  » » » » yeah, it is.
 » I found my solution for F is realy complex(Though it is right)!!Can you share your idea for F?
•  » » 7 days ago, # ^ | ← Rev. 2 →   Resulting ordering of B array is some cyclic shift of itself. (If no shifting is possible, no solution).
•  » » » Thank you for your reply:)But can you prove it?And how to achieve it?
•  » » » » 7 days ago, # ^ | ← Rev. 2 →   I did not scratch a full proof yet.For each element in B, we find what shifts are not possible. This can be done using set/segment tree.
•  » » » » » Can you share your code.
•  » » » » » »
•  » » » » 7 days ago, # ^ | ← Rev. 2 →   my F solutionWhenever there exists index i where A[i] is equal to B[i], shift array B one time. Loop until there are no such indices in one interation. I use two pointers to maintain the shift
•  » » » » » Can you prove it?
•  » » » » » » Sorry, I didn't prove it during contest. I generate all possible cases for small N and it seemed work.
•  » » » » » » » alvinvaja , I really like the quality of your profile picture. May I know which camera was used?
•  » » » » » » » » It wasn't mine. Got it from pinterest
•  » » » » » » I think mine is similar where I take a number, x, in both a and b and if the start of b's interval of x isn't over the end of a's interval of x, I calculate how many shifts it would take to make b's start over a's end of x's interval (if b's start isn't already over a's end) and take the maximum shift for all numbers that are both in a and b. Maybe there's a counterexample but I couldn't find one. After the greatest shift needed is done, for that number x in b that needed the largest shift, all the numbers < x in b should be assigned to a number bigger than it in a and all the numbers >= x in b are shifted up to match numbers bigger than it in a until n, and then they match to lower numbers that are <= x because the shift circles it to the beginning of a. Unless the array is impossible I don't see how any numbers can conflict yet.
•  » » » » I used the same approach, but I also can’t prove it. But I know how to achieve it. Each position of B can be expressed as "not shifting in range [l, r]" which is equivalent to ~[x >= l and x <= r] = (~(x >= l) or (x >= r + 1)).Now the requirement is just AND product of all boolean clauses which is nothing, but 2-SAT. I thought that they put some problems to practice Atcoder library, turned out that I overcomplicated things up. lol (They didn’t add library yet, so I got compile error.) code
•  » » you can see this solution I just tried to solve F using this and got AC. Logic is simple as well as code. Here is my code for this approach
•  » » » Thanks:)
 » Can someone check why I'm getting WA on C . here is my submission
•  » » 7 days ago, # ^ | ← Rev. 2 →   Result of a1 — a2 + b1 may be negative, so just add MOD to it before taking remainder
•  » » » Thanks, it worked!
•  » » "ll ans = (a1 — a2 + b1)%M" should be ll ans = (a1 — a2 + b1 + M)%M;
•  » » ll ans = (a1 - a2 + b1 + MOD)%M;
 » Just 2 min short to get AC in problem F. Nice F anyway
•  » » What was your idea behind F? Was it greedy?
•  » » » The idea is tough to explain for me. I try. 1.A is in ascending order and B is in ascending order. So, just try to pick up elements like two pointer from start to end. Then, pick the unpicked elements too. Now, check the answer. If its OK then print.2. Otherwise, reverse B once again and go like 1. Check the answer. If OK then print it, else there is no answer.
 » 7 days ago, # | ← Rev. 2 →   A Combinatorics solution for D — RedistributionRecall that, by the famous Stars and Bars Theorem, the number of non-negative integral solutions of $x_1 + x_2 + \dots + x_r = n$is ${n + r - 1} \choose {r - 1}$Let us denote this value as $stars(n, r)$. Clearly, the value is 0 if $n$ is negative.Moreover, if we add a constraint that $x_i \geq t$, then, we can replace $x_i$ by $y_i$, where $x_i = y_i + t$This transforms the problem to finding the non-negative integral solutions of $y_1 + y_2 + \dots + y_r = n - r \cdot t$Clearly, the number of valid solutions is $stars(n - r \cdot t, \ r)$So, for each $r$ from 1 to $n$, we sum up the values of $stars(n - 3 \cdot r, \ r)$.
 » DP Solution for Problem C — Ubiquity Code#include using namespace std; #define int long long const int M = 1e9+7; vector>dp; void solve(){ int n; cin>>n; dp.assign(n+2,vector(4,0)); dp = 1; for(int i=1;i<=n;++i){ dp[i] = ((dp[i-1]+dp[i-1])%M+10*dp[i-1]%M)%M; dp[i] = (dp[i-1] + 9*dp[i-1]%M)%M; dp[i] = (dp[i-1] + 9*dp[i-1]%M)%M; dp[i] = 8*dp[i-1]%M; } cout << dp[n] << endl; return; } int32_t main(){ int t=1; while(t--){ solve(); } return 0; } 
•  » » can you Please explain the states and transition.
 » 7 days ago, # | ← Rev. 2 →   A great contest.Unfortunately,I nealy AK(AC 5 problems),but I'm in trouble with the sixth problem.All of them are Math problems(.(xiao xue ao shu in Chinese)And there's Chinese in English task:配点。Hope the abc will be better!：）
 » So I did a randomized solution for F. I try three ways: Reverse $B$. Random shuffle $B$ for $T$ times. Rotate $B$ randomly for $T$ times. $T = 20$ is sufficient to get AC, and it runs in 61 ms. Of course, I have no proof (well not even an intuition) for this. :DSo, I'll be glad if someone can hack this solution or give some proof/intuition for it.
•  » » Assume odd n, and n/2 '1' in a[], and n-n/2 '1' in b. Then we need to rotate exactly n/2 positions.
•  » » » Reversing B works in this case.
•  » » » » Ok, let the sympobls be '2', not '1'. And a '1' in first position of both arrays. Then the reverse of b[] does not work, but still we need to rotate exactly n/2 positions.
•  » » » » » 7 days ago, # ^ | ← Rev. 2 →   Yeah it prints No for this case. gen// py n = 200000 print(n) a =  * (n // 2) +  * (n // 2) b =  +  * (n // 2) +  * (n // 2 - 1) print(*a) print(*b) 
 » How to prove that on F, if a solution exists, one of the cyclic shifts of b is also a solution?I wrote my solution based on it and it passed.
•  » » Isnt there a lot of cyclic shifts possible?
 » 7 days ago, # | ← Rev. 2 →   My Unofficial tutorial (A-F) for this contest. And the Chinese version.
•  » » What is the codeforces problem similar to F?
•  » » »
 » Just a thought: How to solve F if the elements are unsorted in both the arrays?
•  » » You can look at my solution, which does not depend on whether the arrays are sorted or not.
•  » » You can always sort them and use your solution for the sorted input...
 » Can somebody please help me find the only test case that is failing for my solution of problem E?
•  » » Changing the initial value of your variable ans1 to LLONG_MIN should solve your problem, because it's not necessary that x+y or x-y is always greater than -1.
•  » » » Thanks dude, Another stupid mistake
 » What's wrong with my solution of D?I used dp, $dp_{ij}$ means the sequence‘s length is $i$ and its sum is $j$. Spoiler#include using namespace std; const int MOD = 1e9 + 7; int dp; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int s; cin >> s; for (int i = 3; i <= 9; i++) dp[i] = 1; for (int i = 1; true; i++) { if (i > s / 3) break; for (int j = 3 * i; j <= 9 * i; j++) { for (int k = 3; k <= 9; k++) { dp[i + 1][j + k] += dp[i][j]; dp[i + 1][j + k] %= MOD; } } } int ans = 0; for (int i = (s + 8) / 9; i <= s / 3; i++) { ans += dp[i][s]; ans %= MOD; } cout << ans << "\n"; return 0; } 
•  » » The elements of the sequence can be arbitrary positive integers rather than digits.
•  » » » Yes, I get it! Thank you so much!
•  » » Can be simplified using a 1D dp array. Starting from i=4, dp[i]=dp[i-1]+dp[i-3] and then take the mod.
•  » » » Nice solution:D
 » 7 days ago, # | ← Rev. 2 →   Why do you have the problem whose solution is available in CPH? The most popular book on CP, talking about problem E.
 » For atcoder editorial I search always on codeforces.lol.. xD..
 » Sry for a newbie question, how to we see the test cases at atcoder???
•  » »
•  » » » This page might not updated for a while... There are no test cases for last or last-to-last ABC
 » How to solve E??
 » Can someone Tell me the approach for E ?
 » 7 days ago, # | ← Rev. 4 →   Can someone plz tell me why my code for task C is failing for 5 test cases? #include using namespace std; typedef long long ll; #define mod 1000000007 ll help(ll a,ll b){ if(b==0)return 1; if(b==1)return a; ll ans = 1; if(b&1){ ans = help(a,b-1)*a; } else { ans = help(a,b/2); ans = ans * ans; } return ans%mod; } int main(){ ll n; cin>>n; ll x = help(10,n); ll y = help(9,n); ll z = help(8,n); ll res = x-2*y+z; // error in this line cout<
•  » » I guess you forgot to take mod from the res at the end
•  » » » I have changed it after you said, but still those same 5 test cases are failing. I'll update the latest code in my comment. Can you plz look into it again. Thanks!
•  » » » » Since it is c++, mod can also give negative result, you need to handle this
•  » » » » » Thank you so much, sol accepted!
 » I got wrong answer in problem E my approach is : my approach is getting the distance between each point and point(0,0) then sort these distances ascending and the answer will be the distance between the point which has the longest distance and the point which has the smallest distance. May anyone tells me why this approach fails.
•  » » Consider (1,1),(100,1),(1,2).Here (1,1) is at min from (0,0) and (100,1) is at max from (0,0) so,according to you, answer should be |(100-1)|+|(1-1)|=99 but the point (100,1) and (1,2) gives |(100-1)|+|(1-2)|=100. There are lots of such points.
 » F can be solved by maintaining an invariant as follows: Hint1Let $count(x)$ denote the number of occurrences of $x$ in $B$ + that in $A$.If there's an $x$ where $count(x) > n$, then there is no solution. Else, there is.We need to construct a solution iteratively while maintaining that invariant, that there's no $x$ where $count(x)$ exceeds $n$. Note that $n$ is decremented each iteration, and also $count(x)$ represents the count of $x$ in the remaining elements in $A$ and $B$. Hint2Let us call $x$ critical, if $count(x) = n$.There can be at most 2 critical numbers. Let us assume there's only one, and it'll be easy to see how the same idea solves the other case.After an iteration, the invariant will be broken iff there's $x$ s.t. $count(x)$ exceeds $n$, hence in the previous iteration $count(x)$ was $n$ (i.e. $x$ was a critical number) and its $count$ didn't change. Hint3Hence, if the current iteration is $i$ and there's a critical number, then assign it to $b_i$, so that its count decrements in the next iteration and the invariant is maintained. But what if that critical number is equal to $A_i$? Then actually its count will be decremented too.So either way, you can maintain the invariant. If there's no critical element, just put any number and the invariant will be maintained anyway.Thanks to Yousef_Salama for showing me this idea, you can check his submission.
 » I have written unofficial English editorial, you can find it at: editorial
•  » » great!!
 » For F, I am reversing B and then checking if B[i]=A[i]. If so at index=p, I am reversing the array from B to B[p] and from B[p+1] to B[n-1]. This works as for the reversed B, all numbers left of B[p] will be >= B[p] and to right of B[p] will be <= B[p]. Whereas in A, all numbers left of A[p] will be <= A[p] and to right of A[p] will be >= A[p]. Theoretically it should work but I am getting wrong answer in 3 of the 60 test cases and I cant figure out why. Can anyone point out the fault in my algo? Submission: https://atcoder.jp/contests/abc178/submissions/16733148
•  » » 6 days ago, # ^ | ← Rev. 2 →   Check this test case: 4 2 3 3 4 1 2 3 3 Correct answer: Yes Your answer: No 
•  » » » Thank you!
 » Problem F is EXACTLY the same with Perm Matrix (just with smaller constraints).I've seen this problem,so I got an unexpected high rank.My atcoder account: sjcsjcsjc
•  » » It's nothing new at atcoder, many time I got the exact questions which I had previously solved or seen somewhere. Problem E was also copied in the same contest.
•  » » » Problem E is just a standard problem of changing Manhattan distance to Chebyshev distance.Problems using this technique are everywhere.
 » How can I solve D using top-down?
•  » » 41 hour(s) ago, # ^ | ← Rev. 4 →   $f(n) = \sum\limits_{i = 3}^n {f(n-i)}$https://atcoder.jp/contests/abc178/submissions/16705977
•  » » » 24 hours ago, # ^ | ← Rev. 2 →   I don't understand why dp= 1 and also why ret+=f(x-i) ?
•  » » » » 14 hours ago, # ^ | ← Rev. 2 →   if S = 0, there is only one sequence i.e. empty sequence which sum to zero, hence dp = 1. ret += f(x-i) is simply there to implement the formula above.
 » Hi can someone please tell why my submission for problem C is working in atcoder c++ compiler but not in my local compiler.I have tried recursive dp solution for this problem and it is working well . But in some large input example for n = 869121 it gives segmentation fault in local compiler as well as in online cpp compiler but works well upon submitting can some please tell why this happening ? Can this be due to memory limit or recursion depth ? please help