### 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!

• +106

 » 7 days ago, # |   0 Love atcoder beginner contest
 » 7 days ago, # |   +8 Reminder: Contest starts in 10 mins!
 » 7 days ago, # |   0 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
•  » » 7 days ago, # ^ |   +6 The ARC's should be doable, they're gonna be frequent again soon.
 » 7 days ago, # | ← Rev. 2 →   -31 deleted
•  » » 7 days ago, # ^ |   +4 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 →   0 mnist why u added 8^N instead of subtracting it from 10^N
•  » » » » 7 days ago, # ^ |   0 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.
•  » » » » » 7 days ago, # ^ |   0 got it now....thanks bruh :)
•  » » » » 7 days ago, # ^ |   0 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
•  » » » 7 days ago, # ^ |   0 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?
•  » » » » 7 days ago, # ^ |   +9 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
•  » » » » 7 days ago, # ^ |   0 I also tried this same approach. Don't know what went missing :')
•  » » » 7 days ago, # ^ |   0 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?
•  » » » » 6 days ago, # ^ |   0 Yes it's and, anyways understable from the calculation.
•  » » » 7 days ago, # ^ | ← Rev. 2 →   0 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
•  » » 7 days ago, # ^ |   0 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
•  » » » 7 days ago, # ^ |   +25 He asked during contest which is not allowed
•  » » » » 7 days ago, # ^ |   -11 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
•  » » » » » 6 days ago, # ^ |   +31 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.
•  » » 7 days ago, # ^ |   +1 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
 » 7 days ago, # |   0 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.
•  » » 7 days ago, # ^ |   0 Contest has not finished yet
•  » » 7 days ago, # ^ |   +15 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
 » 7 days ago, # |   0 Solved E but coudnt solve C and D :(.
•  » » 7 days ago, # ^ |   0 approach for e
•  » » » 7 days ago, # ^ |   -8 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 →   +25 Copy-pasted from: stackoverflow Why can't you provide the link directly instead of copy pasting the text XD
•  » » » » » 6 days ago, # ^ |   0 So people dont have to visit stackoverflow and look for answer.
•  » » » » » 6 days ago, # ^ |   +4 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
•  » » » 7 days ago, # ^ |   0 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.
 » 7 days ago, # |   0 How to do D?
•  » » 7 days ago, # ^ | ← Rev. 2 →   +4 Simple DP. very similar to thisJust coins here are {3, 4, 5 .. N}
•  » » » 6 days ago, # ^ |   0 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 →   0 Imagine filling DP array for n = 7 by this code: Spoiler // dp[i] = how many ways to get this sum dp[0] = 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[0] = 1; // {0} dp[1] = 0; // {} dp[2] = 0; // {} dp[3] = 1; // {3} dp[4] = 1; // {4} dp[5] = 1; // {5} dp[6] = 2; // {3+3, 6} dp[7] = 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 dp[7]Hope you understood
•  » » » » » 5 days ago, # ^ |   0 Awesome! I understood now...thanks for the efforts :)
•  » » 7 days ago, # ^ |   0 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[0]=1; for(i=3;i<=s;++i){ for(j=i-3;j>=0;--j) (dp[i]+=dp[j])%=mod; } cout<
•  » » 7 days ago, # ^ |   +10 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.
•  » » » 7 days ago, # ^ |   +2 I also did the same,this approach is bit intuitive as this revises the P&C concepts :)
•  » » » » 7 days ago, # ^ |   0 Yeah, but dp would be a lot easier.
•  » » » 7 days ago, # ^ |   0 Could you elaborate more on this approach?
•  » » » » 7 days ago, # ^ | ← Rev. 2 →   +11 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
•  » » » » » 7 days ago, # ^ |   0 thank u a lot
•  » » 7 days ago, # ^ |   0 It’s just a simple coin change dp problem.
 » 7 days ago, # |   0 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.
•  » » 7 days ago, # ^ |   +3
•  » » » 7 days ago, # ^ |   +4 Thanks! Completely forgot to break mod, went in the wrong direction. :(
•  » » 7 days ago, # ^ | ← Rev. 3 →   +1 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 →   +11 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
•  » » » 7 days ago, # ^ |   0 Wow, nice breif explanation.
•  » » » 6 days ago, # ^ |   0 Thanks. Here is shorter implementation I did with your help.
•  » » » » 6 days ago, # ^ |   0 Oh, i see. It's really concise. 80% of time i overkill (: problems.
•  » » 7 days ago, # ^ |   0
•  » » 7 days ago, # ^ |   +5 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; }
•  » » 7 days ago, # ^ |   +3 Alternatively, you can calculate the convex hull of the given points and brute force the vertices of the polygon.
•  » » » 7 days ago, # ^ |   +6 This will TLE if to much points are on the convex hull, it could be all.
•  » » » » 7 days ago, # ^ | ← Rev. 2 →   0 Yes, you're right. However, I got an Accepted verdict. I guess the test cases are not strong enough.
 » 7 days ago, # |   +10 Pure Mathematics Contest!!
•  » » 7 days ago, # ^ |   +4 A and B was no math ;)
•  » » 3 days ago, # ^ |   0 and D was also of dp ..No math
•  » » » 35 hours ago, # ^ |   0 You could do it with stars and bars
 » 7 days ago, # |   0 Maybe E is just a too well known problem, but F solution was more obvious to me than E solution.
•  » » 7 days ago, # ^ |   0 how to solve F ? It seems easy at first.
•  » » 7 days ago, # ^ |   0 how to solve F ? Think about 70 mins and more but still fail to figure
•  » » 7 days ago, # ^ |   0 What was the approach for F?
•  » » 7 days ago, # ^ |   0 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
•  » » » 7 days ago, # ^ |   0 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
•  » » » 7 days ago, # ^ |   0 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.
•  » » » 7 days ago, # ^ |   0 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.
•  » » » » 7 days ago, # ^ |   0 Hey can you please suggest which one test case is failing for this solution
•  » » » » » 6 days ago, # ^ | ← Rev. 5 →   0 Try this2392850397 652442710430284160 923935256Correct answer:308926309Your answer:493651095
•  » » » » » 6 days ago, # ^ |   0 Sorry for prvious wrong case.I updated it. Please check it,Sorry for my bad English.
•  » » » » 7 days ago, # ^ |   0 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?
•  » » » » » 7 days ago, # ^ |   0 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.
•  » » » » » 6 days ago, # ^ |   0 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
•  » » » » » » 6 days ago, # ^ |   +1 Nice. Thanks!
•  » » 7 days ago, # ^ |   0 Can you check why I am getting WA on F. here is submission
•  » » 7 days ago, # ^ |   +19 More than being well known, E is pretty easily searchable I think.
 » 7 days ago, # |   +7 MathCoder literally :V
 » 7 days ago, # |   0 What are the solutions for problems E and F?
•  » » 7 days ago, # ^ |   +3 e is well-known, u can google it
•  » » » 7 days ago, # ^ |   0 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?
•  » » » » 7 days ago, # ^ |   0 hereyou're welcome :))
 » 7 days ago, # |   0 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
•  » » 7 days ago, # ^ |   0 can I get any test case for which my code is failing?
•  » » » 7 days ago, # ^ |   0 there is dropbox link to all atcoder test cases in one of the rng_58's blog
 » 7 days ago, # |   0 Can someone share his approach for solving F?
 » 7 days ago, # |   +118 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.
•  » » 7 days ago, # ^ |   0 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!
•  » » » 7 days ago, # ^ |   +8 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$ ...
•  » » » » 7 days ago, # ^ |   +8 Ya I missed it thanks , Amazing F!
•  » » » » 6 days ago, # ^ |   0 How to prove that answer is always Yes when every f_A + f_B <= n
•  » » » » » 6 days ago, # ^ |   0 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
•  » » » » » » 6 days ago, # ^ |   0 oh,actually its very obvious...what am i thinking...Thanks!
•  » » 7 days ago, # ^ |   +25
•  » » » 7 days ago, # ^ |   0 great idea man
•  » » 7 days ago, # ^ |   +15 This is the best solution I have ever read!!
•  » » » 7 days ago, # ^ |   0 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
•  » » » 7 days ago, # ^ |   +3 Yes, key insight is that only a[i] = b[i] = c will be the only value, and form an interval
•  » » » » 7 days ago, # ^ |   0 Can you prove it ?
•  » » » » » 7 days ago, # ^ | ← Rev. 4 →   +5 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
•  » » » » » » 7 days ago, # ^ |   0 Amazing!!!!!!
•  » » 7 days ago, # ^ | ← Rev. 2 →   +3 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!
•  » » 7 days ago, # ^ |   0 i think thats the easiest code to read and understand.
 » 7 days ago, # |   0 Man problem C ruined the round for me!:) Can anyone please explain the approach for it?
•  » » 7 days ago, # ^ |   +3 10^n-2*9^n+8^n Inclusion exclusion
•  » » » 7 days ago, # ^ |   0 Wow, thanks! Very interesting!
•  » » » 7 days ago, # ^ |   +1 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.
•  » » » » 7 days ago, # ^ |   0 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!
•  » » » » 7 days ago, # ^ |   0 There will be repeation, like in your solution you will count (2 1 9 2) twice.
•  » » » 7 days ago, # ^ | ← Rev. 2 →   +5 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 →   0 You can use DP for accounting for double counting
•  » » » 7 days ago, # ^ |   +1 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 →   0 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[0][0] = 1; for(int i=1;i<=n;++i){ dp[i][3] = ((dp[i-1][1]+dp[i-1][2])%M+10*dp[i-1][3]%M)%M; dp[i][2] = (dp[i-1][0] + 9*dp[i-1][2]%M)%M; dp[i][1] = (dp[i-1][0] + 9*dp[i-1][1]%M)%M; dp[i][0] = 8*dp[i-1][0]%M; } cout << dp[n][3] << endl; return; } int32_t main(){ int t=1; while(t--){ solve(); } return 0; }
•  » » » » » 7 days ago, # ^ |   0 Nice solution!
•  » » » » » 7 days ago, # ^ |   0 Please, can you explain your solution? I couldn't get it.
•  » » 7 days ago, # ^ | ← Rev. 2 →   +3 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)
•  » » » 7 days ago, # ^ |   0 Nice explanation, thank you!
•  » » » 7 days ago, # ^ |   0 I am still confused with duplicates. Can anyone please write all the sequences if n = 3 ? Thanks in advance.
•  » » » » 7 days ago, # ^ |   0 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.
•  » » » 7 days ago, # ^ |   0 thanks, it helped.
•  » » » 7 days ago, # ^ |   0 nice explanation!!
•  » » 6 days ago, # ^ | ← Rev. 2 →   0 deleted
 » 7 days ago, # | ← Rev. 3 →   +4 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
 » 7 days ago, # |   0 Anyways to do C or D without DP ?
•  » » 7 days ago, # ^ |   0 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)));
•  » » 7 days ago, # ^ |   0 10^n-2*9^n+8^n
•  » » 7 days ago, # ^ |   0 D could be solve by brute force the number of elements in the sequence and the do a star-and-bar-like counting trick.
•  » » 7 days ago, # ^ |   +5 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<
•  » » » 6 days ago, # ^ |   0 AK. Well explanation brother.
•  » » 7 days ago, # ^ | ← Rev. 2 →   0 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
 » 7 days ago, # |   0 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 :)
•  » » 7 days ago, # ^ |   +1 nC2 * 10^(n-2) will duplicate
•  » » » 7 days ago, # ^ |   0 can you give correct combination formula for this problem ?
•  » » » » 7 days ago, # ^ |   +1 I used dp https://atcoder.jp/contests/abc178/submissions/16709001 Here, dp[n][0][0] means sequence of length n and 0 zeros and 0 nines, dp[n][0][1] means sequence of length n and 0 zeros and atleast 1 nine, dp[n][1][0] means sequence of length n and atleast 1 zero and 0 nines, dp[n][1][1] means sequence of length n and atleast 1 zero and atleast 1 nines
•  » » » » » 7 days ago, # ^ |   0 thanks
•  » » » 7 days ago, # ^ |   0 so the 0's and 9's are getting duplicated?
•  » » » » 7 days ago, # ^ |   0 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
•  » » 7 days ago, # ^ |   0 There are repetitions in your formula. For n = 4 the answer is 974. Check with your submission
•  » » 7 days ago, # ^ |   0 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 ).
•  » » 7 days ago, # ^ |   0 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
 » 7 days ago, # |   +21 atdynamicprogrammer
 » 7 days ago, # |   +6 lose again because of my speed
 » 7 days ago, # |   0 Was trying to do D , using DP but gave TLE... https://atcoder.jp/contests/abc178/submissions/16721075 Can someone pls help what is wrong
•  » » 7 days ago, # ^ |   +3 Index is not necessary in the state.
•  » » » 7 days ago, # ^ |   0 Damn I messed up !!!, after removing the index state got AC....Thanx
 » 7 days ago, # | ← Rev. 3 →   +43 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 →   +3 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][2][2]; 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; }
•  » » » 7 days ago, # ^ |   0 Yeah, dp can also work!
•  » » 7 days ago, # ^ |   +8 Hey, in problem D, I think you also need to add 1 to dp[i].
•  » » » 7 days ago, # ^ |   0 Fixed!
 » 7 days ago, # |   0 Is there a case to be handled for mod of negative numbers in C ?
•  » » 7 days ago, # ^ |   0 if a<0 then use mod-abs(a) will work.
•  » » 7 days ago, # ^ |   0 Don't forget to add mod when you are subtracting two numbers under mod.
•  » » » 7 days ago, # ^ |   +1 I just have learnt a very valueable lesson.
•  » » 7 days ago, # ^ |   0 we can do this : ans = (a-b+MOD)%MOD;
•  » » » 5 days ago, # ^ |   0 How does it work?Can you please explain or give me any reference?
•  » » » » 4 days ago, # ^ |   0 I'll leave that for you to figure out :)
 » 7 days ago, # |   +11 And.. AtCoder becomes MathCoder.
•  » » 7 days ago, # ^ |   0 Lol
 » 7 days ago, # |   0 c explanation ?
•  » » 7 days ago, # ^ |   +9 Total Number: 10^nWithout digit '9': 9^nWithout digit '0': 9^nWithout both: 8^nFinal Result: 10^n — 9^n — 9^n + 8^n
•  » » » 7 days ago, # ^ |   0 :O i feel dissapointed about myself
•  » » » 7 days ago, # ^ |   0 Too simple to understand!!
•  » » 7 days ago, # ^ |   0 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);
•  » » 7 days ago, # ^ |   0 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.
 » 7 days ago, # |   +54 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.
 » 7 days ago, # |   0 I have tried to solve F using bipartite graph matching but it failed just in test 16 :)))
•  » » 7 days ago, # ^ |   +8 WA or TLE?
•  » » » 7 days ago, # ^ |   0 TLE, actually I was expecting it! I was checking a random edge to find a match each time.
•  » » 7 days ago, # ^ |   +16 I don't think it can work , the nodes and edges are too many !
•  » » » 7 days ago, # ^ | ← Rev. 2 →   +6 It worked :)) submmision #16726993I guess test-cases are not that much strong.
•  » » 7 days ago, # ^ |   +8 of course it will :))))
•  » » 7 days ago, # ^ |   -8 maybe you can try using a segment tree to build the graph.
•  » » » 7 days ago, # ^ |   +8 But the nodes are too many :)
•  » » » » 7 days ago, # ^ |   -8 yeah, it is.
 » 7 days ago, # |   +9 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 →   +6 Resulting ordering of B array is some cyclic shift of itself. (If no shifting is possible, no solution).
•  » » » 7 days ago, # ^ |   +9 Thank you for your reply:)But can you prove it?And how to achieve it?
•  » » » » 7 days ago, # ^ | ← Rev. 2 →   0 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.
•  » » » » » 7 days ago, # ^ |   +9 Can you share your code.
•  » » » » » » 7 days ago, # ^ |   0
•  » » » » 7 days ago, # ^ | ← Rev. 2 →   +6 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
•  » » » » » 7 days ago, # ^ |   0 Can you prove it?
•  » » » » » » 7 days ago, # ^ |   +6 Sorry, I didn't prove it during contest. I generate all possible cases for small N and it seemed work.
•  » » » » » » » 4 days ago, # ^ |   0 alvinvaja , I really like the quality of your profile picture. May I know which camera was used?
•  » » » » » » » » 3 days ago, # ^ |   0 It wasn't mine. Got it from pinterest
•  » » » » » » 6 days ago, # ^ |   +6 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.
•  » » » » 7 days ago, # ^ |   +12 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
•  » » 7 days ago, # ^ |   +5 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
•  » » » 7 days ago, # ^ |   0 Thanks:)
 » 7 days ago, # |   0 Can someone check why I'm getting WA on C . here is my submission
•  » » 7 days ago, # ^ | ← Rev. 2 →   0 Result of a1 — a2 + b1 may be negative, so just add MOD to it before taking remainder
•  » » » 7 days ago, # ^ |   +1 Thanks, it worked!
•  » » 7 days ago, # ^ |   0 "ll ans = (a1 — a2 + b1)%M" should be ll ans = (a1 — a2 + b1 + M)%M;
•  » » 7 days ago, # ^ |   0 ll ans = (a1 - a2 + b1 + MOD)%M;
 » 7 days ago, # |   +1 Just 2 min short to get AC in problem F. Nice F anyway
•  » » 7 days ago, # ^ |   0 What was your idea behind F? Was it greedy?
•  » » » 7 days ago, # ^ |   0 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 →   +7 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)$.
 » 7 days ago, # |   0 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[0][0] = 1; for(int i=1;i<=n;++i){ dp[i][3] = ((dp[i-1][1]+dp[i-1][2])%M+10*dp[i-1][3]%M)%M; dp[i][2] = (dp[i-1][0] + 9*dp[i-1][2]%M)%M; dp[i][1] = (dp[i-1][0] + 9*dp[i-1][1]%M)%M; dp[i][0] = 8*dp[i-1][0]%M; } cout << dp[n][3] << endl; return; } int32_t main(){ int t=1; while(t--){ solve(); } return 0; }
•  » » 7 days ago, # ^ |   0 can you Please explain the states and transition.
 » 7 days ago, # | ← Rev. 2 →   +7 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!：）
 » 7 days ago, # |   +22 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.
•  » » 7 days ago, # ^ |   0 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.
•  » » » 7 days ago, # ^ |   +8 Reversing B works in this case.
•  » » » » 7 days ago, # ^ |   +11 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 →   +8 Yeah it prints No for this case. gen// py n = 200000 print(n) a = [1] * (n // 2) + [2] * (n // 2) b = [1] + [2] * (n // 2) + [3] * (n // 2 - 1) print(*a) print(*b)
 » 7 days ago, # |   0 give us editorial please..
 » 7 days ago, # |   +17 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.
•  » » 7 days ago, # ^ |   -12 Isnt there a lot of cyclic shifts possible?
 » 7 days ago, # | ← Rev. 2 →   +21 My Unofficial tutorial (A-F) for this contest. And the Chinese version.
•  » » 7 days ago, # ^ |   0 What is the codeforces problem similar to F?
•  » » » 7 days ago, # ^ |   +6
 » 7 days ago, # |   0 Just a thought: How to solve F if the elements are unsorted in both the arrays?
•  » » 7 days ago, # ^ |   +5 You can look at my solution, which does not depend on whether the arrays are sorted or not.
•  » » 7 days ago, # ^ |   +8 You can always sort them and use your solution for the sorted input...
 » 7 days ago, # |   0 Can somebody please help me find the only test case that is failing for my solution of problem E?
•  » » 7 days ago, # ^ |   +6 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.
•  » » » 7 days ago, # ^ |   0 Thanks dude, Another stupid mistake
 » 7 days ago, # |   0 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[3000][30000]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int s; cin >> s; for (int i = 3; i <= 9; i++) dp[1][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; }
•  » » 7 days ago, # ^ |   +1 The elements of the sequence can be arbitrary positive integers rather than digits.
•  » » » 7 days ago, # ^ |   0 Yes, I get it! Thank you so much!
•  » » 3 days ago, # ^ |   +1 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.
•  » » » 2 days ago, # ^ |   0 Nice solution:D
 » 7 days ago, # | ← Rev. 2 →   0 Why do you have the problem whose solution is available in CPH? The most popular book on CP, talking about problem E.
 » 7 days ago, # |   0 For atcoder editorial I search always on codeforces.lol.. xD..
 » 7 days ago, # |   0 Sry for a newbie question, how to we see the test cases at atcoder???
•  » » 7 days ago, # ^ |   +3
•  » » » 7 days ago, # ^ |   0 This page might not updated for a while... There are no test cases for last or last-to-last ABC
 » 7 days ago, # |   0 How to solve E??
 » 7 days ago, # |   0 Can someone Tell me the approach for E ?
 » 7 days ago, # | ← Rev. 4 →   0 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<
•  » » 7 days ago, # ^ |   0 I guess you forgot to take mod from the res at the end
•  » » » 7 days ago, # ^ |   0 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!
•  » » » » 7 days ago, # ^ |   0 Since it is c++, mod can also give negative result, you need to handle this
•  » » » » » 7 days ago, # ^ |   0 Thank you so much, sol accepted!
 » 7 days ago, # |   0 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.
•  » » 6 days ago, # ^ |   0 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.
 » 7 days ago, # |   +25 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.
 » 7 days ago, # |   +13 I have written unofficial English editorial, you can find it at: editorial
•  » » 7 days ago, # ^ |   +1 great!!
 » 6 days ago, # |   0 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[0] 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 →   +8 Check this test case: 4 2 3 3 4 1 2 3 3 Correct answer: Yes Your answer: No
•  » » » 6 days ago, # ^ |   +1 Thank you!
 » 6 days ago, # |   +43 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
•  » » 5 days ago, # ^ |   0 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.
•  » » » 5 days ago, # ^ |   +3 Problem E is just a standard problem of changing Manhattan distance to Chebyshev distance.Problems using this technique are everywhere.
 » 2 days ago, # |   0 How can I solve D using top-down?
•  » » 41 hour(s) ago, # ^ | ← Rev. 4 →   0 $f(n) = \sum\limits_{i = 3}^n {f(n-i)}$https://atcoder.jp/contests/abc178/submissions/16705977
•  » » » 24 hours ago, # ^ | ← Rev. 2 →   0 I don't understand why dp[0]= 1 and also why ret+=f(x-i) ?
•  » » » » 14 hours ago, # ^ | ← Rev. 2 →   0 if S = 0, there is only one sequence i.e. empty sequence which sum to zero, hence dp[0] = 1. ret += f(x-i) is simply there to implement the formula above.
 » 22 hours ago, # |   0 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