### TooDumbToWin's blog

By TooDumbToWin, 3 years ago, Code
Code
Code

Code
Code
Code
Code 1
Code 2  icm, Comments (97)
 » dynamic programming everywhere
 » 3 years ago, # | ← Rev. 2 →   Alternate solution for E. Notice that the differentiation idea gives . This motivates the idea to write xk as a sum of polynomials x, x(x - 1), x(x - 1)(x - 2), ... and x(x - 1)... (x - k + 1). The coefficients for this linear combination is known to be the Stirling Numbers of the Second Kind. (you can also just calculate them) So combine that to have a O(K2) solution.
•  » » I get the differentiation part, but after that, I'm loosing on x^k as a sum of polynomials and Stirling numbers. Can you elaborate a bit!
•  » » » If our goal was to solve for , we would be done by our first observation. However, we are to solve for . Therefore, it makes sense that we should try to write ik as a sum of i, i(i - 1), etc.After that, we can use our first observation to collect the sum quickly.About the part about Stirling Numbers, it isn't really needed (you can just calculate them directly) but it's the first equation in https://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind#Generating_functions. Here, (x)k = x(x - 1)... (x - k + 1). Hope you understand now.
 » If you know at least something about convex hull trick and have some prewritten code of it than you can easily solve problem F in not more than 10 minutes. IMHO this problem is very straightforward.
 » In Problem C P[j] = j-1 for i < j <= i+k-1 if I'm not wrong.
•  » » Fixed. Thanks. :)
•  » » » You are given N , A , B find such x and y that (A*x + B*y) = N . If such x , y does not exist print -1 , either print x and y in same line separated by space , if there are multiple x , y that satisfy the equation print any of them .For example: If N=9 , A=2 , B=5 then ans is 2 1 . Because 2*2+5*1=9 . Here A=2 , x=2 and B=5 , y=1 .Constraint:1<= N , A , B <= 10^18INPUT9 2 522 3 7439365 78717 87873505383 77277 992911000000 3 999997474441 99291 77277911106 51038 78188321361 79845 818261000000000000000000 8765433 54343345OUTPUT2 15 10 5-11 14 11 113 125359400 18397426840I have solved using linear loop . for(long long i=0; i<=n/a; i++) if((n-i*a)%b==0) { printf("%lld %lld\n",i,(n-i*a)/b); return 0; } printf("-1\n");But constraint is so big so this would not pass . For this test case n=1000000000000000000 , a=3 , b=798877665 code is getting TLE .How to solve this problem efficiently ? This is a subproblem of this problem http://codeforces.com/contest/932/problem/C
 » In problem E : (1 + x)^n = sum, the sum should start from 0, not from 1.
•  » » Fixed. Thanks.
 » Another alternative solution for E:Let's give xk a combinatorial meaning. xk is the number of combinations of picking k people from the subset with x people with repetition. For each subset we count all combinations and sum those.Why not switch sums? For each combination we count the number of subsets in which the combination appears, and sum those. This obviously computes the same result.Assume we have a combination of size k consisting of i distinct people. From which subsets can we construct this combination? Obviously the i people have to be in the subset, but for every other person we have two choices: he is in the subset or he isn't. It doesn't matter. Therefore there are 2n - i such subsets.Let dp[j][i] be the number of combinations of size j with i distinct people, then the result is . dp[j][i] can be computed very easily with, you guess it, dynamic programming. My submission: 35317193
•  » » Hello Jakube. Can you tell me why this way is wrong[ Written the steps in the picture provided in the link] . And why step 4 in the picture is not the general solution?
•  » » » The differentials are wrong. You need to use the product rule!
•  » » » » Thanks. I got the mistake now. By the way in your explanation how you are calculating dp[i][j]. I mean the recurrence formula you are using to calculate it how it equal to dp[i][j].
•  » » » » » Look in my submission. It is pretty basic: either the last element already appeared in the combination or it is new.
•  » » could you tell me why is (j * dp[i-1][j] + (n-j+1) * dp[i-1][j-1])?what is the combinatorial meaning? thank a lot!!!
•  » » » To generate a combination with i elements from which j are distinct we have two options. We can start with a combination with i - 1 elements from which j are distinct, and append an element that already appears in it. => j options to choose the new element => j * dp[i - 1][j]. We can start with a combination with i - 1 elements from which j - 1 are distinct, and append a new element that doesn't appear in the combination => n - (j - 1) possibilities to choose the new element => (n - j + 1) * dp[i - 1][j - 1].
•  » » » » 3 years ago, # ^ | ← Rev. 2 →   this part is clear now, but I dont understand how to deal with x^k yet
•  » » » » » xk are just a bunch of combinations. And each one of those I count independently. This way I don't have to use xk in any of my computations.
•  » » » » » » 3 years ago, # ^ | ← Rev. 2 →   How to count independently ? by a combination of size k consisting of i distinct people?
•  » » A so nice approach Jakube. Thanks.
 » 3 years ago, # | ← Rev. 2 →   praran26 please answer this question::It came to my mind during the contest, corrrect me please: I thought that the answer will be always P=[1 2 3 ... n] . Suppose x = min(A,B), so in x moves this permutation will always satisfy both function f and g.
•  » » For your permutation, g(i) = 1 for all i. (Since x may not be 1, the permutation is wrong.)
 » Can someone explain D in a bit more detail?
 » In problem D, if every node in the sequence is a descendants of its predecessor,can it be solved?
•  » » It can be solved with a link cut tree
 » 3 years ago, # | ← Rev. 5 →   Another solution for problem E; The last algebraic is easy for calculating.
•  » » In the fourth equation,why it is ?Why not ?
•  » » » Opps, thanks. Fixed
•  » » » » And also the seventh equation,there should be at last. :D
•  » » » » Thank you very much for this awesome solution.
•  » » how the second line came?
 » 3 years ago, # | ← Rev. 2 →   ( Sorry, my English is poor )Another solution for E:It can be proved (by mathematical induction) that ans(n, k) = 2n * fk(n), where fk(n) is a k-degree polynomial.So we can use fk(0), fk(1)...fk(k) to calculate fk(n) with Lagrange interpolation method. ( Since we use consecutive integer as interpolation points, the interpolation will be very fast ) fk(0), fk(1)...fk(k) can be computed by simple O(k2) dp, or other O(k2) algorithms.( But I got a WA on test 22, :( ) ( UPD: I forgot %mod somewhere :( )
 » 3 years ago, # | ← Rev. 2 →   Another (one more XD) solution for E:The expression is But you can also look at it as Which is kinda alike the original expression but taking k-1 instead of k and "shifting" the coefficients.Let dp(k, j) be , so k means the current power and j means "how much have we shifted", and since we cannot shift more than k times then there are O(k2) different states. Note that our answer is n*dp(k,1). We have the following recurrence:  And then we have dp(k, j) = (n - j).dp(k - 1, j + 1) + j.dp(k - 1, j). Our base case is k=0, since that .
•  » » Very nice solution! I found the shifting idea first but couldn't make my DP work fast enough. I only had a O(k3) solution with O(k2) states. After that, I got the Stirling solution. It's nice to see that my failed approach could lead to such a wonderful solution :D
 » Can someone please explain Convex Hull on Tree in Task F in more details? How do we add new lines to the ancestors? Also, is it possible not to sort lines in CVH? Because recently I saw a task where lines were not sorted, but in editorial it was written that we have to use Convex Hull trick. Thanks!
•  » » You can use a set or linked list.When adding new lines,for set just insert,for linked list you need binary search.After inserted lines,check the precursors and succeeds to maintain the CVH.
•  » » » Sure, but how do we erase useless lines in the set when we add new lines?
 » F is solvable without the Merge Small to Large trick. We linearise the tree by Euler tour and then each subtree will represent a range in the tour. Then build a segment tree on it where each node will contain a CHT for the lines in its range. Initially the tree is empty, we can add lines on the go. Time complexity should be same.
•  » » 4 months ago, # ^ | ← Rev. 2 →   How do you deal with the fact that the $a_x$ won't necessarily be sorted in each subtree?Edit: never mind, I misunderstood it.
 » 3 years ago, # | ← Rev. 3 →   Can someone tell what I am doing wrong in this approach in problem E. Please specify the step. Here is the link
 » Maybe I can't read english straight right now, but I'm really confused on D condition 2:Every node in the sequence is an ancestor of its predecessor.So... it's an ancestor of it's ancestor??? It's an ancestor of itself???
•  » » I think they mean "Every next node in the sequence".
•  » » If the sequence of nodes is denoted by {ai}, then ai + 1 is ancestor of ai.
 » Nice B, C, D — simple ideas for solutions was hidden under deterrent (a bit) statement.
 » Sry for my noobness ;)
 » The tutorial of E is AWESOME!!!
 » 3 years ago, # | ← Rev. 2 →   Can someone elaborate more on problem D?I am quite new to the concept of binary lifting and I believe more people are in my situation.
•  » » 3 years ago, # ^ | ← Rev. 2 →   Let's imagine you already have computed nxt[]. Now for the binary lifting, you keep an array jump[u][i] which shows the vertex in which you will be if you do nxt[nxt[nxt[...nxt[u]...]]] exactly 2i times.Obviously jump[u] = nxt[u]. Well you can find that jump[u][i] = jump[jump[u][i - 1]][i - 1]. This way we can compute the jump values of a vertex in .In a similar way, we will store sum[u][i], which is the sum of weights of the vertices, on the nxt[] path of length 2i (without jump[u][i]). Now to answer the queries Let's find the highest ancestor in the jump array of vertex R, such that the sum on the path is less than or equal to X. This is obviously — we simply go through the array jump[R]. Lets call the cell which we found jump[R][j]. Now we add 2j to the answer, and remove sum[R][j]. We also move R to jump[R][j]. Well now we simply need to perform a query on these new R and X.Obviously the length of the query chain is at most , so the complexity for each query will become . You can notice that after each query the j decreases and achieve .
 » 3 years ago, # | ← Rev. 2 →   brute_force Accepted problem F:http://codeforces.com/contest/932/submission/35336205
 » You are given N , A , B find such x and y that (A*x + B*y) = N . If such x , y does not exist print -1 , either print x and y in same line separated by space , if there are multiple x , y that satisfy the equation print any of them .For example: If N=9 , A=2 , B=5 then ans is 2 1 . Because 2*2+5*1=9 . Here A=2 , x=2 and B=5 , y=1 .Constraint:1<= N , A , B <= 10^18INPUT9 2 522 3 7439365 78717 87873505383 77277 992911000000 3 999997474441 99291 77277911106 51038 78188321361 79845 818261000000000000000000 8765433 54343345OUTPUT2 15 10 5-11 14 11 113 125359400 18397426840I have solved using linear loop . for(long long i=0; i<=n/a; i++) if((n-i*a)%b==0) { printf("%lld %lld\n",i,(n-i*a)/b); return 0; } printf("-1\n");But constraint is so big so this would not pass . For this test case n=1000000000000000000 , a=3 , b=798877665 code is getting TLE .How to solve this problem efficiently ? This is a subproblem of this problem http://codeforces.com/contest/932/problem/C
•  » »
•  » » » 3 years ago, # ^ | ← Rev. 2 →   But extended eucleidean algorithm will work only when A*x+B*y=GCD(A,B) . That means N (variable of above problem) should have to be GCD(A,B) . But in this problem it is not guaranteed that N will always be gcd(A,B) .
•  » » » »
 » Can anyone explain the following lines of problem C :So, if there exists a solution (x, y) where x ≥ 0 and y ≥ 0, for Ax + By = N, we can in turn generate a permutation P satisfying our needs. Otherwise, no such permutation is possible.So, now for any one of the solution (x, y), generate x cycles of length A, beginning from indices 1, A + 1, A * 2 + 1 ... A * (x - 1) + 1 and then beginning from indices A * x + 1, A * x + 1 + B, ... A * x + 1 + B * (y - 1), generate y cycles of length B.Thanks!
•  » » Anyways, I got it... We here are trying to break N into x parts of size A and y parts of size B.. IF this equals N then only solution is possible.
 » Can someone explain the code of problem B in more detail or suggest any related tutorial ?
•  » » 3 years ago, # ^ | ← Rev. 2 →   Let di = a1 + a2 + ... + ai (in other words, di = di - 1 + ai). Then, al + al + 1 + ... + ar = dr - dl - 1.
 » RECURSIVE QUERIES And as 1 ≤ g(n) ≤ 9, using partial sum arrays for each possible value of g(n), we can answer the queries in O(1) explain this thing plzz how to use partial sum arrays and where did the author use this technique in its programme
•  » » x[i][j] is the count of numbers between 1 and j such that the value of g is i. So, each query can be answered as x[k][r] - x[k][l - 1].
•  » » » did u actually use dp technique in your programme i am not able to see it
•  » » » » 3 years ago, # ^ | ← Rev. 2 →   No, it's not DP. Try running the code using pen/paper on some small test cases. That might help. You can learn more about partial sums here.
•  » » » » » then we can use bottom up dp for calculating g(n) for all the integers 1 ≤ n ≤ 10^6 in O(n). :/howz the time for calulating the g(n) is O(n).Let us consider n=200140 then 1-> is n>10 true so,f(n)=8 (time taken to do this is O(width of no) that traversing the no form right to left) then 2-> 8>10 false return 8 If i analyse this for running time complexity then it should be till the number not reduces to single digit every time (loop for non zero digit product calculation runs from right to left that is over whole width of the no)..Generally for any large number it no more then takes 4 to 5 compressions then my time taken (4 or 5)*(width of the newly arrived no every time till the ans returned by the f(n) is not a single digit)..How the time taken to find g(n) is O(n) here .. and n is the no So ur code want to say time is O(200140) if consider my case then plzz clear it !!
•  » » » » » » It is time complexity to calculate g(i) for i ≤ n using DP.
•  » » » » » » » 3 years ago, # ^ | ← Rev. 3 →   f(n) ≤ 9^k - 1 × nk, how????
•  » » » » » Is using the concept of partial sum with modification query better than using Segment Trees ?
 » 3 years ago, # | ← Rev. 2 →   For my submission http://codeforces.com/contest/932/submission/35328280 getting a wrong answer verdict, I have checked with the solution provided and not able to find any mistakes.
•  » » Found the error. array ovelflow
 » How would you go about solving A if you had to find the string of minimum length (which satisfies the given properties)?
•  » » try to iterate over possible centers of the palindrome. In basic version it takes O(n) to verify and find answer for a center. But using hashing we can verify and find ans for center in O(logn). So we can solve it for 1e5 length also. Though there might be ways to get around hashing.
•  » » » You can also use palindromic trees : 35425210
 » 3 years ago, # | ← Rev. 2 →   Can someone elaborate more on problem F?The tutorial says that "Thus, convex hull trick of dp optimization can be used to find the minimum value. Once the value is calculated for a node, a line with slope b[i] and y-intercept dp[i] is added to the hull."Does it mean that we treat every line as a point (b[i], dp[i]) and maintain the dynamic convex hull when we insert the point? If so, why?THX.
 » Can anyone explain how next[] array is calculated in problem D? I am not able to understand from editorial.
 » 3 years ago, # | ← Rev. 2 →   I’m so sorry that I missed this post and the abundance of solutions to problem E, (possibly one of the greatest realizations of mathematics and computer science combined this year), so here I am with yet ANOTHER solution to problem E: Make the “educated guess” that the solution is of form ansn = 2n * P(n), where P(n) is a polynomial of degree at most k + 5 over ZMOD (the  + 5 part might be optimized out in future iterations, but let’s be honest to ourselves; we don’t care that much, do we?) Gather some small answers (i.e. by the brute-force technique) and interpolate to find P Bonus: can you find how I managed to make the educated guess about the form of the solution?Seriously now, since when did I start to get the notion that combinatorics and formula calculations are not what competitive programming’s all about?
•  » » ans0 = 2n,  ans1 = n·2n - 1 — seems like a proof
 » Problem F. Weak tests. Solutions that take into account only 30 vertices deeper than current get AC. http://codeforces.com/contest/932/submission/35515087
•  » » Yes I also use O(brute_force) and get AC,and I'm also curious if my code can be changlleged admin please add more tests...
•  » » » Here is a test which is not passed by both your and my solutions: https://drive.google.com/open?id=1f14u7zAiGKol_HQIOxjb9jWlttGumocQ
•  » » » » 3 years ago, # ^ | ← Rev. 3 →   seems my answer is correct -10 100000 times...except for one zero on leafbtw I think it is not easy to cha my code both deep<=20 and deep<=200
•  » » » » » No, this is not correct. For the first 33333 vertices answer is -30, for the next 33333 vertices answer is -20, for the next 33332 vertices answer is -10, and the last one is 0. There are 3 vertices with "B" equal to -1: leaf of the tree and two more vertices with depth 33333 and 66666. So your solution gives WA for this test.
•  » » » » » » oh,I didn't notice three -1s ,but I can modify my algo a bitwhen update dp[id], choose there children as candidate point bfs 200 distance,and choose children's decision nodeand perform bfs 200 distance, how to cha it..
•  » » » » » » » You can try to make your modifications, submit it and, I will try to challenge it tomorrow )
•  » » » » » » » » this is my new submissions it can pass the test you gave:http://codeforces.com/contest/932/submission/35550022
•  » » » » » » » » » test13 from this folder https://drive.google.com/open?id=1CGDePAKpdm8CTLEgQ2LJd9S_0imsF5QJ
 » In problem E didn't understand the part where b+c is a constant and that reduces the state space for implementation.Can anyone explain?.Thanks in advance.
 » 3 years ago, # | ← Rev. 3 →   another approach to E:we can see that after each step of differentiation and multiplying it by 'x' we have the polynomial like xi * (1 + x)^(n - i). So, we can use normal differentiation rule for differentiating such polynomial and add it to the next step and after we have done we just put x = 1 and our answer is the sum of (2i * dp[k][i]) as xi = 1 for x = 1. here dp[k][j] denotes the co-efficient of the expression xi * (1 + x)^(n - i) after differentiating it k times.
 » what does the line "struct LineContainer : multiset{....}" do?
•  » » It means that the LineContainer inherits multiset
•  » » » Thanks
 » There's another way to reduce G to counting palindromic partitions. Consider a greedy palindromic partition of the string $s$ — by greedy, I mean the partition which takes the smallest suffix-prefix of $s$ and removes it from the beginning and the end of $s$. This is also known to be the partition with the maximum number of elements — see problem "Palindromic Partitions" from CEOI 2017. Take the first half of the partition and to each string in it assign a unique label. For example, for ab|b|ab|ab|ab|ab|b|ab remove the second half ab|b|ab|ab and assign $x$ to $ab$ and $y$ to $b$. The solution is the number of palindromic partitions of the resulting string, in this case xyxx. Note that now we're counting all palindromic partitions, not just those where the lengths of palindromes are even.
 » I understood $E$ but can someone help me in understanding why the operation we are considering is 'differentiate-and-multiply' rather than just 'differentiate'. Simple $k$-differentiation would be enough to get $i^k$ as the coefficient.
•  » » After differentiation, the power reduces by 1, so if not multiplied by x, next time differentiating multiplies the coefficient by (i-1) instead of i. So, you'd get i*(i-1)..(i-k+1) instead of i^k.
•  » » » Ah, thanks. Another issue is that we will lose a term each time if we just differentiate. Thanks for your explanation :)
 » In problem C, I am not able to prove that if there does not exist non-negative integers x and y such that Ax+By=N, then it is not possible to construct such a permutation. Please help.
•  » » 7 months ago, # ^ | ← Rev. 2 →   First of all i will prove that for any f(i, j) = i, a cycle of length j will be establised in which each of its element will follow the above rule and if above condition fails and remaining elements are left of length not A or B, then g(i) is either A or B will fail, let's say you found either of the sequence which follows g(i) = A, if you start with any other of link in this cycle like if you start calculating f(p[i], j), you will eventually get to p[i] because f(p[i], j-1) was i and f(i, 1) will be p[i], thus now you can see that if you find any j such that f(i, j) = i, then a cycle has been established of length j between all elements that were traversed until f(i, j) reduced to i, that's why u need 2 non-negative integers x and y which fulfill Ax + By = N.
 » 2 months ago, # | ← Rev. 2 →   Can someone please help me figure out why I'm getting WA test case 6 on problem F? I am using Li-Chao Tree.90353161Thanks!
 » in the problem Permutation Cyclewhy is it sufficient to break into cycles of lengths $A$ and $B$ ?i mean — how can we be sure that the only way is when $A*X+B*Y=N$ ? why cant there be other overlapping solutions? is this because its a functional graph? [directed graph in which all vertices have outdegree equal to 1]