### awoo's blog

By awoo, history, 3 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)

• +83

 » 3 months ago, # |   0 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??
•  » » 3 months ago, # ^ | ← Rev. 2 →   0 ((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
 » 3 months ago, # |   0 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?
•  » » 3 months ago, # ^ |   0 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.
•  » » » 2 months ago, # ^ |   0 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
•  » » » » 2 months ago, # ^ |   0 Sorry, I'm not familiar with Java. Does the TreeSet structure allow duplicates?
•  » » » » » 2 months ago, # ^ |   0 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
•  » » » » 2 months ago, # ^ |   0 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.
•  » » » » 2 months ago, # ^ |   0 it is true that on using treeset it is giving wrong answer on test case 19. That is quite peculiar.
 » 3 months ago, # | ← Rev. 2 →   -10 I've uploaded my post-contest stream (where I explain A-E) to Youtube with timestamps: https://youtu.be/bx35l4tx_xk
 » 3 months ago, # |   0 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.
•  » » 2 months ago, # ^ |   0 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
•  » » » 2 months ago, # ^ |   0 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.
 » 3 months ago, # | ← Rev. 2 →   +21 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
•  » » 3 months ago, # ^ |   +5 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!
•  » » 3 months ago, # ^ |   +8 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$.
•  » » » 3 months ago, # ^ |   +19 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 •  » » » » 3 months ago, # ^ | +8 Thanks for your explanation! This looks plausible.  » 3 months ago, # | 0 What does the array dp mean in the solution code? Does dp[0] stands for f and dp[1] stands for g? Obviously the meaning of dp does not directly correspond with the tutorial. •  » » 3 months ago, # ^ | 0 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).  » 3 months ago, # | 0 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 •  » » 3 months ago, # ^ | 0 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)) •  » » » 3 months ago, # ^ | 0 thanks! i had fixed  » 3 months ago, # | 0 In Problem D, why are we checking i*i <= n-1 in the loop? •  » » 2 months ago, # ^ | 0 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. •  » » » 2 months ago, # ^ | 0 Understood. Thank You.  » 3 months ago, # | +4 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. •  » » 3 months ago, # ^ | ← Rev. 2 → +7 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. •  » » » 3 months ago, # ^ | 0 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? •  » » » » 3 months ago, # ^ | +5 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.
 » 3 months ago, # | ← Rev. 3 →   -15 Pythagorean Triples (Problem D)int a = sqrt(n*2);if( a%2 == 0)a--;int ans = a/2;
•  » » 3 months ago, # ^ |   0 Thanks
•  » » 3 months ago, # ^ |   -11 sqrt() function itself take O(log n) time so your solution is not in O(1) bro
•  » » » 3 months ago, # ^ |   0 sqrt() function is a single CPU instructionso it's really O(1)
 » 3 months ago, # |   0 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!!
•  » » 3 months ago, # ^ |   0 Oh , I got it! It seems we use a approximate number to describe the range of "a".. No problems any more...
 » 3 months ago, # |   0 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
•  » » 3 months ago, # ^ |   +8 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)
 » 3 months ago, # |   -8 If anyone needs a math based solution for D, you can have a look at my solution here
 » 3 months ago, # | ← Rev. 3 →   +8 In Neon's editorial of problem F, why is this condition met carry>=(carry+cp)/10? Thanks!
 » 2 months ago, # |   0 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)
 » 2 months ago, # |   0 I can not understand question F. Can anyone help me with it?