### awoo's blog

By awoo, history, 19 months ago, translation, 1487A - Arena

Idea: BledDest

Tutorial
Solution (Neon)

1487B - Cat Cycle

Tutorial

1487C - Minimum Ties

Idea: BledDest

Tutorial
Solution (BledDest)

1487D - Pythagorean Triples

Idea: Neon

Tutorial
Solution (Neon)

1487E - Cheap Dinner

Idea: BledDest

Tutorial
Solution (BledDest)

1487F - Ones

Idea: Neon

Tutorial
Solution (Neon)

1487G - String Counting

Idea: BledDest

Tutorial
Solution (BledDest)  Comments (59)
 » In Problem C, can anyone tell me, for n=4 the output 1 0 0 1 -1 1 is correct or not?? If wrong then how??
•  » » 19 months ago, # ^ | ← Rev. 2 →   ((1 vs 2 , 1 has 3 points now , 2 has zero)) ((1 vs 3 , 1 has 4 points now , 3 has 1 point)) ((1 vs 4 , 1 has 5 points now , 4 has 1 point)) ((2 vs 3 , 2 has 3 points now , 3 still has 1 point)) ((2 vs 4 , 2 still has 3 points , 4 has 4 points now)) ((3 vs 4 , 3 has 4 points now , 4 still has 3 points))1 — > 5 points 2 — > 3 points 3 — > 4 points 4 — > 4 pointsThere is no ties
 » The time complexity of the editorial of problem E seems to be O(n*m*logm), which seems more than 1E10. How does this solution get AC?
•  » » It's just $O((n+m) \log n)$ since, for each consecutive pair of types of dishes we build a data structure for the first type (in $O(n \log n)$), and for every dish of the second type, remove the forbidden elements ($O(m \log n)$ over all dishes), query the minimum ($O(n \log n)$ for all dishes in total), and insert back the removed elements.
•  » » » Thanks for explanation. i tried implementing it but got WA on test case 19. tried many things but could nt get AC.Tried even generating random inputs and comparing with accepted solution but in vain.. any clues for the test case 19??https://codeforces.com/contest/1487/submission/108129435thanks
•  » » » » Sorry, I'm not familiar with Java. Does the TreeSet structure allow duplicates?
•  » » » » » Yes it allows if we add a custom comparator. No issues i will try to generate some more tests and see whats going wrong... thank you
•  » » » » I used priorityQueue in java but got tle on test case 14. Can you please tell if priorityQueue can be used to solve this instead of treeset.
•  » » » » it is true that on using treeset it is giving wrong answer on test case 19. That is quite peculiar.
 » 19 months ago, # | ← Rev. 2 →   I've uploaded my post-contest stream (where I explain A-E) to Youtube with timestamps: https://youtu.be/bx35l4tx_xk
 » In problem E, I have a bit of a different solution to the DP optimization. From the previous course, we sort all dp values from low to high. Then for a certain new dish, we want to know the first option in the sorted list that isn't forbidden. First we assume every dish will pick the first option, and we put the option they choose in an array (initially all zeroes). Then go through all bad edges of the first dp option and increment the lowest possible dp option for each bad dish, then do the same for the bad edges of the second option, etc. Then it is simple to calculate the new dp values. This algorithm takes only $O(n \cdot log(n) +m)$, because it goes through each edge once, and does a sort on the dp values.
•  » » My SubmissionI have done exactly the same it doesn't seem to work and is giving wrong answer on test case 16.I have created 3 dp vectors of pairs (strg1,strg2,strg3) which store minimum cost to have till 2nd 3rd and 4th course of meals.If we can't have a particular meal it's cost is marked as -1.Before starting to calculate the next dp array we sort the previous dp table and our counter starts from the 1st val which isn't -1.Then for a particular meal we increment the counter till a compatible meal is found and again reset the value to the minimum for the next meal. Time complexity is (n*logn +m).Please help me find my error or a test case which my code fails
•  » » » AcceptedSorry for the trouble It is working actually i forgot to check the index of sorted ones i.e. dish[i].second i was checking ith one.
 » 19 months ago, # | ← Rev. 2 →   My solution for F:Supposing $m_i=111...1$($i$ digits). And you can think of this problem as: make $x=x+m_i/ x-m_i$with price $i$ and let $x=0$ with minimal price.Let $dp_{y,x}$ mean after you plus/minus all $m_i (i > y)$ the minimal price with value $x$ now. And we can use map/unordered_map and Bigint(I write it myself) for $dp_y$. Initially $dp_{y+1,n}=0$For every $x$ in $dp_{y,x}$ there are only at most two options for $x$ in $dp_{y-1,x}$. Assuming $x$ is nonpositive those two options $x$ are $x-km_y$ and $x-(k+1)m_y$ where $x-km_y$ is nonnegative and $x-(k+1)m_y$ is negative. Other $|x-km_y| \geq m_y$ mean you can always minus/plus a more $m_y$ and save much more price.There seems to be at most $2^{50}$ different $x$ in $dp_y$, but we can see $m_i \mod m_{i-1} = 1$. It means plus/minus one more $m_y$ will at most result in $1$ difference in $dp_{y-1}$. So there are at most $O(|n|)$ different $x$ in $dp_y$.Iterate $y$ from $|x|+1$ to $1$ $(O(|n|))$, enumere $x$ in $dp_y$ and update $dp_{y-1}$ $(O(|n|))$, with Bigint calculation complexity $O(|n|)$ and map $O(log|n|)$, the total complexity as I think is $O(|n|^3log|n|)$ my code
•  » » OMG, I actuallly thought the complexity was $O(2 ^ {|n|})$ during the contest.I was too worried to code it. :(After all, $m_i\mod m_{i-1}=1$ seems the key to the solution.What a nice job you did,bro!
•  » » Seemingly you claimed that if $|x| \geq m_y$, $m_y$ should be in the optimal solution. Why?Also, could you explain why is there $O(|n|)$ different $x$ in $\mathrm{dp}_y$? I cant see why it is true given $m_i \bmod m_{i + 1} = 1$.
•  » » » Well, every $m_y$ should be used less than $10$ times (in fact maybe even less), otherwise you can always change $10m_y$($10y$digits) into $m_{y+1}-m_1$($y+2$ digits). If your $|x| \geq m_y$ and don't use $m_y$ to reduce it. Considering $\sum_{i=1}^{y-1} 9m_i < \sum_{i=1}^{y-1}10^{i+1} = m_y$, Some of $m_i(i •  » » » » Thanks for your explanation! This looks plausible.  » What does the array dp mean in the solution code? Does dp stands for f and dp stands for g? Obviously the meaning of dp does not directly correspond with the tutorial. •  » » As I say in the last paragraph of the editorial, the third version of dynamic programming ($g$) can be used not only to calculate the number of strings which violate the constraint on a pair of characters, but also the number of strings which don't violate any constraints and which violate the constraints on one character; so, the only dynamic programming in this solution actually stands for$g$(though it stores only two last layers of it to optimize memory usage).  » in problem D , i searched it in Wikipedia "a = k(2xy) b = k(x^2 — y^2) c = k(x^2 + y^2)" then i calculated that: x = y + 1. So my idea is using a loop for from 1 to sqrt(n) and check. But it's wrong. Can anyone help me. This is my submission: https://codeforces.com/contest/1487/submission/107657994 •  » » you just need to add cnt by 1 during the loop, because when you can keep running the loop, it means you find one answer for the question, not n/(y*y + (y+1)*(y+1)) •  » » » thanks! i had fixed  » In Problem D, why are we checking i*i <= n-1 in the loop? •  » » You mean i*i <= 2*n - 1. We concluded that we need numbers that follow a^2=2b+1. You can think of this equation as squares which are odd and we don't want c or b to exceed n. From c = b+1, and a^2 = 2*b+1 implies a^2 = 2*c-1. Thus max value of a^2 can be 2*n-1. So in the loop we are counting all the a^2 less than 2*n-1 such that a is odd. You can see why it is more than 1 from simple maths. •  » » » Understood. Thank You. •  » » » 14 months ago, # ^ | ← Rev. 2 → How C = B + 1? •  » » » » 13 months ago, # ^ | ← Rev. 2 → It is derived in the editorial. we got b(b+1) = c(c-1) by solving the 2 equations. Clearly, substituting c = b+1 in c(c-1) gives you b(b+1), hence c = b+1 is true.  » For Problem G, is there any explanation for why the optimizations described in the solution work? Why do$l$and$m$only have 3 states and why do we only need to run DP once? I'm still kind of confused over these. •  » » 19 months ago, # ^ | ← Rev. 2 → My English is very poor...so if I make grammar mistakes, please forgive me. TATThe reason that$l$and$m$only have 3 states is we only need to know whether the last two characters are the two we fixed. If so, we can choose the rest 25 characters as the next one then update the value of$j$or$k$. If not, we only need to choose the rest 25 characters as the next one, without updating the value of$j$or$k$.The reason that why do we only need to run DP once is all the characters are essentially the same. No matter which two characters you fix, your DP processes are all the same. •  » » » Ah the first part makes sense. I'm still not sure about the second part, mainly that the DP processes are all the same. How can you find the values for each pair of characters given just one DP? •  » » » » To get the answer, we calculate the number of strings of length$n$that don't contain palindromes of odd length greater than$1$. Then we iterator all the$26$characters and subtract the number of strings that violate the limit of at least one character(suppose this character is$c$, then the number is$\sum_{j=0}^n\sum_{k=limit_c+1}^ndp_{n,j,k,*,*}$) from the answer. Then we iterator all the$26$characters and add the number of strings that violate the limit of at least two characters(suppose these two characters are$c1,c2$, then the number is$\sum_{j=limit_{c1}+1}^n\sum_{k=limit_{c2}+1}^ndp_{n,j,k,*,*}\$ ) to the answer. You can think of the whole process as an inclusion-exclusion.
 » 19 months ago, # | ← Rev. 3 →   Pythagorean Triples (Problem D)int a = sqrt(n*2);if( a%2 == 0)a--;int ans = a/2;
•  » » Thanks
•  » » sqrt() function itself take O(log n) time so your solution is not in O(1) bro
•  » » » sqrt() function is a single CPU instructionso it's really O(1)
•  » » » » really ?
 » Can anyone explain the " i*i<=2*n-1 " in the Neon's solution of problem C ? How can we get "2*n-1" ? Much obliged!!
•  » » Oh , I got it! It seems we use a approximate number to describe the range of "a".. No problems any more...
 » In problem B can anyone tell what are the collision points for n = 7.It is supposed to be collide after n//2 steps but for n = 7 ,collision happened only at pt 3 and 1 and after that it is not colliding.Please explain where m i doing wrong
•  » » collision happened at 4 -> 1 -> 5 -> 2 -> ... {7,1},{6,2},{5,3},!{4,5},{3,6},{2,7},!{1,2},{7,3},{6,4},!{5,6},{4,7},{3,1},{2,3} it seems that the collision happened on pt (1+t*(n-1)/2)
 » If anyone needs a math based solution for D, you can have a look at my solution here
 » 19 months ago, # | ← Rev. 3 →   In Neon's editorial of problem F, why is this condition met carry>=(carry+cp)/10? Thanks!
 » Pfft, that solution for B was way too complicated, I came up with much simpler solution. Obviously, the solution is (1 - n % 2) * (((k - 1) % n) + 1) + (n % 2) * (((((k - 1) % (n * (n / 2))) / (n / 2)) / 2 + (((k - 1) % (n * (n / 2))) % (n / 2)) + ((((k - 1) % (n * (n / 2))) / (n / 2)) % 2) * (n / 2 + 1)) % n + 1)
 » I can not understand question F. Can anyone help me with it?
 » Can any one help me: Why 3n(n-1)/2 mod n = n / 2. I tried to prove it but I think 3n mod n = 0 so 3n(n-1)/2 also 0...
•  » » 15 months ago, # ^ | ← Rev. 2 →   Of course, 3 * n mod n = 0 and that's why 3 * n * (n — 1) mod n = 0. But after dividing 3 * n * (n — 1) by 2, this condition is not necessarily satisfied (for example, with n = 2: 3 * 2 * 1 = 6, 6 mod 2 = 0. But (3 * 2 * 1) / 2 = 3, 3 mod 2 != 0). Also statement 3 * n * (n — 1) / 2 mod n = n / 2 is correct if and only if n is even (if it is odd left part is integer and right is not). proof: 3 * n * (n — 1) / 2 mod n = n / 2 <=> 3 * n * (n — 1) / 2 — n / 2 mod n = 0 <=> (3 * n * n — 4 * n) / 2 mod n = 0 <=> n * (3 * n — 4) / 2 mod n = 0 — this is correct because 3 * n — 4 is even (because n is even) and (3 * n — 4) / 2 is integer.
•  » » » Yay, your reply is very helpful. I also have another question: is there any ways to find result: n / 2 without proving...
•  » » » » 15 months ago, # ^ | ← Rev. 2 →   Yes. You can do it in following way: We want to find a number from 0 to n — 1 inclusive such that 3 * n * (n — 1) / 2 mod n = x. 3 * n * (n — 1) / 2 mod n = x <=> 3 * n * (n — 1) / 2 — x mod n = 0 <=> 3 * n * n — 3 * n — 2 * x mod 2 * n = 0. It means (1) that 3 * n + 2 * x mod 2 * n = 0 (because 3 * n * n mod 2 * n = 0, as n is even and 0 mod 2 * n = 0) and (2) 2 * x mod n = 0 (because 3 * n * n — 3 * n mod n = 0 and 0 mod n = 0). It follows from (2) that x = 0 or x = n / 2 (because 0 <= x < n). If x = 0, (1) sometimes is false (for example, if n = 2). If x = n / 2, 3 * n + 2 * x = 4 * n, 4 * n mod 2 * n = 0. So the only solution to this problem is x = n / 2.
•  » » » » » If I could upvote more than 100 times, I will do it for your helpful reply :D. Thanks sir
 » Why solution in B do k-- ??? I cant understand
•  » » maybe 0-based indexing
 » In question D. how (C = B + 1)?
•  » » From c(c-1)=b(b+1), we get c=b+1
 » For Problem-C , Judge's answer and my answer stands like this for any even number , Your text to link here... can anyone explain me why I'm getting wrong answer here.
 » My Solution for B: https://codeforces.com/contest/1487/submission/155115532 ~~~~~ long ans=0; if(n%2==0) ans=k%n; else ans=(k+(k-1)/(n/2))%n; if(ans==0) ans=n; ~~~~~ if n is even, no clash between A and B otherwise for every n/2 moves there is a clash and the normal sequene for n=5 looks like 1, 2, 4, 5, 2, 3, 5, 1, 3, 4, 1, 2... Here at 3, 1, 4, 2, 5 had a clash. So for every n/2 there is a clash. To know the exact location, i used a trick here. Write down the infinite sequence i.e; 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1 ... For a given k we are omitting certain clash positions. So we just need to find how many clashes happened for a given k and add that to k will give the entire sequence.(k-1)/(n/2) gives omitted positions for a given k and k+(k-1)/(n/2) gives the location in the infinite sequence. Hope it helps!
 » 5 months ago, # | ← Rev. 2 →   my implementation for c using 2D arrayhttps://codeforces.com/contest/1487/submission/156393407**if I choose v[i][j] to be equal to 3 then I had to make v[j][i]=0, my transition:fisrt I make each of (0, 0) (1, 1) (2, 2).... is equal to 0...the I start to filling with 3 and 0 3 for winner and 0 for loser**0'BASED indexif n=5;(1, 0)->(2, 1)->(3, 2)->(4, 3)->(0, 4) =3(0, 1)->(1, 2)->(2, 3)->(3, 4)->(4, 0) =0(2, 0)->(3, 1)->(4, 2)->(0, 3)->(1, 4) =0(0, 2)->(1, 3)->(2, 4)->(3, 0)->(4, 1) =3and if n is even we can stop when the remainning cells that doesnot filled is equal to n I know that my solution is complicated but I want to share wit others..sorry for my bad english.. ****
 » can anyone explain me why are we doing like this for (int i = 3; i * i <= 2 * n - 1; i += 2) in problem D
•  » » hi