### Endagorion's blog

By Endagorion, history, 3 years ago, translation,  Tutorial of Technocup 2020 - Elimination Round 2   Comments (101)
 » Why does 63503240 gives TLE in div2 B2?
•  » » t = 1e4 and k = 1e6
•  » » Because you are allocating and initializing cnt for every test case, which makes your code O(tk). Check the values of $t$ and $k$ in the question — these values will definitely result in a TLE.
•  » » » Thank you guys. I completely ignored k. Changing array to map does the trick
•  » » » » or you can delete cnt after each query
•  » » you can use map,not the ans to index,the ans will TLE
•  » » forn(i,0,k) cnt[i] = 0; If k and queri is too big, it will tle. You should repair to forn(i, k — d + 1, k) cnt[i] = 0;
•  » » I made the same mistake hahI thought about clean cnt with n but I didn't fix it
 » 3 years ago, # | ← Rev. 3 →   I solved D, with a different approach(after contest). I calculated divisors of $x^k$ for every $x$ such that 1 <= $x$ <= $10^5$ and $x^k$ <= $1e10$.Since $x$ can be only in range $1$ to $10^5$ in worst case, we can calculate the divisors of $x^k$ using prime factors of $x$. Caluculating prime factors of $x^k$ is easy if we know prime factors of $x$, we just need to multiply power of every particular prime factor by $k$. Since we know divisors of every $x^k$ we can easily count pairs such that $Ai*Aj$ = $x^k$.
•  » » I don't see your "Accepted" for this problem and how would you then calculate the answer not in an at least quadratic time? It doesn't seem like a good solution to me.
•  » » » 3 years ago, # ^ | ← Rev. 4 →   Link to Submission. I already mentioned that i solved it after contest. Time complexity of my solution is $(\Sigma_{i=1}^{10^5} divisors(i^k)) * C$ (let C be some constant) for calculating divisors,and worst case of this approach occurs when k = 2 because in this case we have to calculate divisors of $10^5$ numbers. When i computed on my pc they were roughly $6*10^6$ which is not much for a problem with 2 sec TL.
•  » » » » Oh, I thought that I see all your submissions when I go to your profile but apparently I don't. Thanks for the link.
•  » » » » » There is an option named "show unofficial".
•  » » » » » » Thank you! I could have thought about it.
•  » » » » 2 years ago, # ^ | ← Rev. 2 →   Thanks for your method to calculate divisors. I used a similar overall solution but the only difference was the method in which I tried to calculate divisors. It gave a TLE for $k==2$ For each x such that $x^k<(10)**10$, I calculated the divisors using $O(\sqrt(x))$ method. Which apparently miserable fails for $k==2$ but works fine for all others
•  » » See this for finding divisors using prime factors.
 » Can someone tell me what is wrong in my coding approach for Power Products(D)I am sorting the array and using two pointer approach to find possible pairsI63471990
•  » » Check on this Tc: 5 2 1 1 1 1 1 It's answer is 10 and yours give 4.
•  » » » Yeah, I guess because of the repeated numbers two pointers approach won't work. Thanks for checking it out.
 » 3 years ago, # | ← Rev. 2 →   Has the tutorial not been made or there is some bug.It is showing tutorial not available for problems D-GEdit:resolved
•  » » It was available few seconds ago...
•  » » The analysis is complete, but there is an issue with loading it from Polygon automatically. This usually resolves itself eventually.
•  » » » It has not been uploading for me for about 30 mins now, could you please resolve it or tell how to resolve the same. Thanks!
 » in Problem E what is the best RSQ can be used with a few implementation cause i thought of segment tree so that we have n segment tree of base m and m segment tree of size n but it would be a lot of implementation so what do u think ?
 » 3 years ago, # | ← Rev. 2 →   In problem $C$, why does $n-kp$ has to be at least $k$ can someone please throw some light on it?
•  » » Because you’re writing it as sum of k power of 2s which are all bigger than or equal to 1
•  » » » Thanks man! Got in now
•  » » because the number of digits in the binary representation of the max value can be
 » Please look upon the issue of editorial.It's showing that "Tutorial is not available"
 » can someone explain how to solve div2E/div1C , in editorial it's not clear about defination of D,R arrays?
 » It would be great if someone can explain me easier way solution of div2-C. I cant understand the solution and I,m not so fimiliar with binary numbers, bit cnt etc.Thanks a lot. :)
•  » » 3 years ago, # ^ | ← Rev. 2 →   Let the minimum number of $p$-binary numbers required to represent $n$ be $l$. We start from $l = 1$ and increment $l$, testing its possibility. If it's possible in $l$ $p$-binary numbers, then that means that $n = (\text{sum of }l\ \text{powers of}\ 2) + lp$, because we get a $+p$ contribution from each of the $l$ $p$-binary numbers. So we need to check if $n - lp$ can be represented as a sum of $l$ powers of $2$, not necessarily distinct.Now you can represent a number uniquely in any integral base greater than 1. It will result in a unique representation. Now given $n - lp$, you need at least $\lfloor log_2{(n-lp)} \rfloor + 1$ bits to represent $n$, and that is the binary representation of $n - lp$. So we get one condition that $l \geq \lfloor log_2{(n-lp)} \rfloor + 1$.Consider the example: $n-lp = 2^2 = (100)_2$. We can write this as $2^2 = 2^1 + 2^1 = 2^0 + 2^0 + 2^1 = 2^0 + 2^0 + 2^0 + 2^0$ In each subsequent representation we are breaking a power of $2$ down and adding one term in the representation. This shows us that we can represent $2^a$ in $1$ to $2^a$ terms, therefore we can represent $\sum 2^a$ in $\sum 1$ to $\sum 2^a$ terms. If we choose $a$ from the binary representation of $n - lp$, $\sum 2^a = n-lp$ and $\sum 1 = \text{sum of bits in}\ n-lp = \sigma$ (say). So $l$ must satisfy $\sigma \leq l \leq n-lp$. In GCC, __builtin_popcount(n-lp) can be used that gives the value of $\sigma$ (sum of bits).Now $n \leq 10^9$ and $p = 1000$, so number of bits in $n-lp$ should be the same as that in $n$. A satisfactory upper bound should be 34. Start from $l = 1$ to $34$, and check if $\sigma \leq l \leq n-lp$; if it's true, then you have your value of $l$.
•  » » » Thanks a lot bro :)
•  » » » 3 years ago, # ^ | ← Rev. 4 →   thau
•  » » » thanks bro..you explained really well
•  » » » can anyone explain how condition sigma <= l <= n-l*p arrive??
 » 3 years ago, # | ← Rev. 2 →   My approach for D:Let $f_{1}$ be a function such that $f_{1}(a)=b$ for $1 \le a \le 10^5$ where $b=p_{1}^{c_{1}}p_{2}^{c_{2}}...p_{m}^{c_{m}}$. Here $p_{i}$ is a prime and $c_{i}  » 3 years ago, # | ← Rev. 3 → Alternative solution to G: Consider the dumb$O(N^\alpha 3^N (\sum a_i)^2)$DP solution that just finds numbers that can be made from each subset. It's too slow. Add bitsets. It's still too slow. Consider a different$O(N^\alpha 3^N (\sum a_i)^2)$DP solution that goes in the reverse direction and calculates for each subset and number "if we can make this number using this subset, we win". It uses the results of the previous DP, because when we want to make$x$(or$xK, xK^2, \ldots$) using subset$i$and we can definitely make$y \le x$using a subset$j \subset i$, then we want to make$x-y$using the subset$i-j$. It's still too slow. Combine these two DPs in a meet-in-the-middle approach. Due to symmetry, we only need to consider cases where the subset$j$above is smaller than$i-j$, or equally large (with some tiebreaker like j < i-j). As soon as we find out that we both can make number$j$using subset$i$(first DP) and if we make it, we win (second DP), we can stop and use the first DP to construct a solution. The first DP now only needs to consider subsets with size$\le N/2$. The theoretical bound isn't much better, but in practice, we have much fewer states to consider both because there are less subsets and much fewer numbers to make using small subsets. The second DP only needs to consider the remaining subsets and it will either quickly find a solution or have very few states to process, so it's pretty fast either way. Intuitively: If the first DP finds many numbers, the chance that the second DP also finds many numbers (using those found by the first DP) and therefore that it quickly stumbles upon a solution is high. If the second DP finds few numbers, the first DP will have difficulty forming many numbers we want to make using large subsets. Small subsets should automatically consume little time, since they have fewer subsets and in the split$i \rightarrow (j, i-j)$described above, if the sets$j$and$i-j$are both processed by the first DP, we don't gain any states in the second DP. If$i$is large and$j$is kinda large, then$i-j$is much smaller than$i$and we already get a small state. On the other hand, when$i$is large and$j$is very small, then there are few numbers we can make using the subset$j$. Time complexity: 1200 ms. •  » » So someone cheesed this problem after all, huh. Nice job!With enough stress-testing I've managed to time out this solution very slightly with this test: 16 3 2 143 158 16 58 56 161 101 89 100 250 7 334 92 103 251  •  » » » 3 years ago, # ^ | ← Rev. 2 → 11Main observation: the loop "for each set bit in v1, shift v2" allows a massive speedup if v1 has just one set bit, which is very often the case for small subsets. (I already tried your hack.) •  » » Could anyone please explain "dumb" dp? I don't get what is the dp state there. •  » » » "Using a subset$S$of the given numbers, can we create a number$j$?" The values of the states are 0/1, there are$O(2^N \sum a_i)$of them. •  » » » » Thanks! How do you end up with$N^{\alpha}$in the complexity? •  » » » » » Choose$\alpha$arbitrarily. Then, there's always$N^\alpha$in any complexity. In this case, it's the exponential and sum that matter, so I didn't bother thinking what$\alpha$is. •  » » » » » » Alright, got it.  » I waited for like 10 MINUTES and there was still no editorial. Can anyone who can see the editorial for Div. 1 B to Div. 1 F consider posting the editorial on somewhere else for others' convenience? •  » » Still, Tutorial is not available ...  » I got TLE in div2 B2 when contest final consequence came out.But accepted it after contest with exactly the same codes.  » 3 years ago, # | ← Rev. 2 → Is this an alternative solution to problem F(Div. 1)?Again, solve it backwards.Consider elements in [L, X] can reach X in no more than K steps. Then, elements in [L', X] can reach X in no more than K + 1 steps, where L' = min_i{pre[i], L <= i <= X} where pre[i] = the last occurance of ch[i] before i(ch[] is the string in the input).Let's try to fix X. Then, the process above forms a tree structure, and what we need to compute is the sum on the path from X to the root.Consider how does the tree change when 1 is added to X. The parent of an interval [[the minimal Y such that min{pre[Y...X]} >= pre[X]], X] will be modified to pre[X] while the other vertices of the tree remain unchanged. Put the intervals with the same parent together and we only need basic operations in dynamic trees to fix it.Use Link Cut Tree or Euler Tour Tree(I'm not sure because I don't know ETT well) to solve the problem in O(n log n).  » In problem D, the prime factors of 360 will be (2^3 * 3^2 * 5^1)  » Can someone please explain the solution for DIV2 problem C  » For Div2 C can someone explain the case when P is negative answer for K still won't cross 31 •  » » Let$p < 0$, then the maximum value of$n - 31p$will be$10^9+31000$which can be represented with 30 binary digits (less than 31). It's obvious that all the numbers less than$10^9+31000$can also be represented with no more than 30 binary digits. So$k=31$will always be possible. •  » » » But whose telling you to stop at 31p for p<0 ,I can still keep on subtracting it •  » » » » We wish to find the minimum answer. So if we know that surely 31 is a possible answer, then we won't check for numbers greater than 31.  » In solution of D there is a small mistake. It is given that 360 = 2^3 * 2^2 *5 but it should be 360 = 2^3 * 3^2 * 5  » Could someone give me a elaborated explanation for problem Div2-C. The Edtiorial is so hard to grasp, especially the range of all$k$value. Thanks •  » » /* Let x = n-31*p There are two possibilities: 1. x >= 31 2. x < 31 For the first case, the maximum value of x is (1e9 + 31*1000) < (2^31). So, 31 <= x < 2^31. Hence, x can always be represented as exact 31 powers of 2's. However, there is a possibility that we can achieve the same with fewer. So, just iterate the value of k from 1 to 31 and check for the minimum value. For the second case, n < 31*(p+1). Since, n>0 and n<31*(p+1) , 31*(p+1) >0 . So, p > -1 or p >=0 (in the editorial there is a mistake, it should be p+1>0 not p+1<0). If (k>31) , -kp < -31p, So,n-k*p < n-31*p < 31 < k. Hence, n-k*p < k (not possible, as it has to be at least k). So, k <= 31. */  •  » » » Thank you very much! Excellent Explanation. •  » » » » But what happen if I let x = n-1e9*p? I am confused that: I want to represent$n-kp$not just$n-31p$. •  » » » » » The given case will come under one of the two cases mentioned •  » » » » » I got it, the reason we don't need a large k like that, we always can find the answer with k < 31. •  » » » » » » Exactly •  » » » » » » Can u please share some proof? I'm having a hard time understanding this. •  » » » Hence, n-k*p < k (not possible, as it has to be at least k). Can someone elaborate this statement? •  » » » So does that mean that if the answer were to exist for the second case it has to be for case k<=31? •  » » Hint : How to represent a number as exactly k powers of 2. •  » » » Hint cl  » Why does https://codeforces.com/contest/1225/submission/63542673 give TLE using g++, but works with MSVC??? •  » » 3 years ago, # ^ | ← Rev. 2 → I just know why the code got TLE using g++. Because there may be 1e5 test case. And for each test case, the value of k can be 1e6. Then the code will run Line 24 1e5*1e6 times. It got TLE here. •  » » » Thank you  » 3 years ago, # | ← Rev. 2 → In case(like me ) you are having a hard time implementing problem D tutorial (DIV 2) here is a implementation of the given approachhttps://codeforces.com/contest/1225/submission/63557938  » Can someone explain B2 to me.. •  » » Think in terms of sliding window u need to count minimum unique elements at a given window and keep sliding window till the end. See my submission •  » » » Thanks,doubt cleared....  » Typo in ur editorial of D -> factorisation of 360 should be 2^3 3^2 5^1 u mistyped 2 in place of 3  » Maybe it will be useful for users who will try to solve this task later. I've found that the following solution passes all authors' tests although it's incorrect: Let's consider DP[N][X][Y] — it is 1, if we can apply such consequence of operations to N-length prefix of an array so that last two numbers are X and Y, and 0 if we can't. Then DP[A][A] = DP[A][A] = 1, and for each i from 2 to N and X, Y from 1 to 2000 if DP[i — 1][X][Y] == 1 then DP[i][f(X+A[i])][Y] = DP[i][Y][f(X+A[i])] = DP[i][X][f(Y+A[i])] = DP[i][f(Y + A[i])][X] = DP[f(X+Y)][A[i]] = DP[A[i]][f(X+Y)] = 1. So in the end we just have to check if for some X, Y such that f(X+Y)=1 DP[i][X][Y] == 1, and restore the answer. The problem is that in general it's not true that f(f(a+b)+c) = f(a+f(b+c)). Test that breaks this solution: 8 4 39 39 51 13 38 66 98 86  » I found it interesting that my friend skip1978 solved div1 E using the random method. I wonder how high the correct rate of this method is.63493030 •  » » Very, very high at least without trying to design a countertest. It took me many hours of runtime to find a test where it fails — specifically, where the probability that a random sequence of operations works is low enough... although maybe I could buy processing power in the cloud and find more. And that's still just because it reshuffles$N$times. With some optimisations, finding a test where it fails would be near impossible because$N\$ is so low.
 » Can someone please tell me what is wrong with my prime factorization in this code for D : Code 1Also , after changing it I got ac Code 2Thanks in advance !
•  » » I think the code is wrong because of Line 16. The end value of x may be 2 when the begin value of x is 2. Then it will not think 2 has the prime factor 2. the code will be wrong if there is 2 in the input.
•  » » » Oh I see! Got ac in that too now. Thanks a lot! :D
 » Can Someone please explain D
 » I cant understand in D one thing. What if we have ones? This solution doesn"t work in this case.
•  » » 1 is just the empty list.
 » Can somebody tell me how to solve div2 B2 in O(n) as maintaining count with array will give TLE?
•  » » use map
•  » » » Then it will be O(nlogn)
•  » » » » 3 years ago, # ^ | ← Rev. 2 →   Use unordered_map, then it's O(n)
•  » » 3 years ago, # ^ | ← Rev. 2 →   I just figured out with help of my friend (shivang) to store accessed elements(O(n)) in a vector and then assign value 0 to those location present in vector. link to my submission:https://codeforces.com/contest/1247/submission/63850376
 » Can someone explain problem E to me?
 » In the task D how can we find factorization of every number in the sequence with sqrt(max ai) or log2(max ai) complexity?__
•  » » Perhaps it means sqrt(max a[i]) for each number separately so total of n*sqrt(max a[i]) (or nlog(max a[i]))
 » 3 years ago, # | ← Rev. 2 →   Can anyone tell me for the problem E and for following test case why output is 0.test case : 1 3 .R. output : 0why?
•  » » Because we can't reach the destination 1,3 by any allowed moves.
•  » » » understand