### TheScrasse's blog

By TheScrasse, history, 22 months ago,

Author: TheScrasse
Preparation: MyK_00L

Hint 1
Hint 2
Hint 3
Solution

Official solution: 107232596

1485B - Replace and Keep Sorted

Author: TheScrasse
Preparation: Keewrem

Hint 1
Hint 2
Hint 3
Hint 4
Solution

Official solution: 107232462

1485C - Floor and Mod

Authors: isaf27, TheScrasse
Preparation: Keewrem

Hint 1
Hint 2
Solution

Official solution: 107232416

1485D - Multiples and Power Differences

Author: TheScrasse
Preparation: MyK_00L

Hint 1
Hint 2
Hint 3
Solution

Official solution: 107232359

1485E - Move and Swap

Author: TheScrasse
Preparation: TheScrasse

Hint 1
Hint 2
Hint 3
Solution

Official solution: 107232216

1485F - Copy or Prefix Sum

Author: TheScrasse
Preparation: TheScrasse

Hint 1
Hint 2
Hint 3
Solution

Official solution: 107232144

• +263

| Write comment?
 » 22 months ago, # |   +21 Love editorials with hints! Also a good but not great contest!!
•  » » 22 months ago, # ^ | ← Rev. 2 →   -13 Video Editorial Problem D: Multiples and Power Differences
•  » » » 22 months ago, # ^ |   -10 Well explained
•  » » » 22 months ago, # ^ |   0 Thanks a lot don't know why people down-voted
•  » » » » 22 months ago, # ^ | ← Rev. 2 →   +3 I think because reply was off topic to parent comment. It was good if same comment was made separately.
•  » » 22 months ago, # ^ |   +7 Why not?
 » 22 months ago, # |   0 Video editorial for C Floor and Mod: https://youtu.be/ap-q_wQn7BE Solution in O(sqrt(x)).
 » 22 months ago, # |   0 Can anyone explain the hint A problem to me
•  » » 22 months ago, # ^ | ← Rev. 2 →   +5 The idea is to first increase b as much as you want then perform operation 1. The reason is :- Lets say you divide first and then increase b, our a will be floor(a/b) and b will be b+1. If we increase b first then finally a will be floor(a/(b+1)) and b will be b+1. You can similarly prove for more number of operations. So once you know that we have to increment b first, the question is by how much. You will see that if we start with b and increment b till it reaches b+k ( k steps ), you will now need to integer divide a by b+k until a != 0. Lets call the number of steps to do so to be 'l'. So total number of steps will be l+k. I observed that we dont need to go very far away as that would increase k by very much but l wont be affected that much. So I started with b and went until b+100 and took the minimum value of l+k for every b in this range. P.S : Sorry I dont know how to use mathematical symbols
•  » » » 22 months ago, # ^ |   0 Sorry ,but i dont get,how do u know to increase b only to a 100????
•  » » » » 22 months ago, # ^ |   +16 Its pure intuition,as you can see in the editorial, it turns out that we dont even need to increase above 30. To get b->b+k we need k steps and to get a->0 we need O(log(a)) steps where base is b+k, we can observe that as b+k increases, the function log(a) doesnt decrease very fast and thats why I thought we dont need to increase b by a lot as that would increase total number of steps more than what log(a) would decrease and the net effect would be that we would require more steps.
•  » » » » » 22 months ago, # ^ |   -35 I also didnt get how 30 comes...and how should I even realize that...:( Bad Problem
•  » » » » » » 22 months ago, # ^ | ← Rev. 2 →   +8 I approached in this way: Let's not keep track of count of type2 operation before doing type1 operation. Code#include using namespace std; #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define input_output freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); int fun(int x, int y){ int cnt = 0; while(x > 0){ x /= y; cnt++; } return cnt; } void solve(){ int ans = 0, x, y; cin>>x>>y; if(y == 1) y += 1, ans++; while(fun(x,y) >= fun(x, y+1) + 1){ y = y+1; ans++; } ans += fun(x, y); cout<>t; while(t--){ solve(); } return 0; } 
•  » » » » » » 22 months ago, # ^ |   +40 You cant understand solution --> bad problem.Amazing logic
•  » » » » » » » 22 months ago, # ^ |   0 XD...was waiting for this comment to appear...no not understanding the editorial is not the reason to consider it a bad problem...
•  » » » » » » » » 22 months ago, # ^ |   0 OK, understand you :)
•  » » » » » 22 months ago, # ^ | ← Rev. 2 →   0 we need almost 30 operations when it s 2 I agree. but how is it related to incrementing b by only 30 times and checking the minimum possibility scenario of each incremented b. why not more than 30 increments of b?
•  » » » » » » 22 months ago, # ^ | ← Rev. 2 →   +14 Because if you use 30 steps for incrementing then you need one more step for dividing, making it totally 31 steps. Instead of that you could have.simply used 30 steps all in dividing, which would have brought a down to 0 already.
•  » » » » 22 months ago, # ^ |   +8 We can rewrite the question as min(x-b+(ln(a)/(ln(x))) for all x>=b. Of course, there is some rounding off business, which we do at last. So consider this fnc, if we differentiate this function, we get 1-(ln(a)/x), so we get a minimum at x=ln(a), but it may be also possible that ln(a) is less than b, so if we take the worst possible ln(a) it won't cross ln(10^9), which comes around 25. So we can iterate x from b to ln(a)+5, and obtain a minimum value. Well, this analysis is rough. So b we can increase up to up to 25 or so, but the editorial gives a better limit.
•  » » » » » 22 months ago, # ^ |   0 You differentiated it incorrectly:$d_x \frac{ln(a)}{\ln(x)} = - \frac{\ln(a)}{x \ln^2(x)}$Therefore after differentiation you must get the equation$1 - \frac{\ln(a)}{x \ln^2(x)} = 0$The upper limit for $a$ is $10^9$. Therefore we arrive to$x \ln^2(x) = 9 \ln(10)$The left part is strictly less than $0$ for $x \leqslant 6$ and it becomes positive for $x \geqslant 7$, where $x \in N$.As the minimum of $f(x) = x - b + \frac{\ln(a)}{\ln(x)}$ is somewhere between $6$ and $7$ we suffice with at most $6$ iterations provided that we start from $b = 1$ (ending up at $7$).Otherwise we need to iterate upwards only until $b >= 7$. So that if you start with $b = 10$ you do zero iterations!
•  » » » 22 months ago, # ^ |   0 You can type $...$ to insert math, where "..." is latex math expression. Common examples: $a_{1} + a^{2}$ --> $a_{1} + a^{2}$$\frac{a}{b}$ --> $\frac{a}{b}$
•  » » 22 months ago, # ^ |   0 It wants to say that we should increment $b$ first as much we want and then do the divide operations . Because that way number of division operations would be less.
 » 22 months ago, # | ← Rev. 2 →   +1 Why solution of problem E's complexity is $O(n\log n)$?upd:now it's ok
 » 22 months ago, # |   +41 Good contest but I think first 4 problems were all heavily based on Maths (except B).Would have been great contest if atleast one was from another topic making problemset more balanced.
•  » » 22 months ago, # ^ | ← Rev. 2 →   0 B,C,D involved math but some ideas outside math was also required . Like binary search in C (not necessary though) , prefix sum in B , chess coloring in D. Also though i didn't read E,F they are tagged as DP and data structures.
 » 22 months ago, # |   +7 Thanks for the interesting problems and the fast editorial!
 » 22 months ago, # |   0 Very nice problems. Every problem had something to offer at least till D(didn't see E and F). I wish I could upvote more for the nice editorial with hints. Looking forward to more contests from the authors.
 » 22 months ago, # |   0 I solved A but don't know why it worked , now i am reading editorial of A, lol
•  » » 22 months ago, # ^ |   0 lol
•  » » 22 months ago, # ^ |   0 I don't know why are you commenting, lol
•  » » » 22 months ago, # ^ | ← Rev. 2 →   0 what are you doing here even you didn't participated this contest , that's why you can not understand why i comment here.lol
•  » » » » 22 months ago, # ^ |   0 Newbies just spam everywhere!
•  » » » » » 22 months ago, # ^ |   0 pupils think they are very smart,lol
•  » » » » » » 22 months ago, # ^ |   -13 No!!
 » 22 months ago, # |   0 Thanks for fast editorial
 » 22 months ago, # | ← Rev. 2 →   -8 I love math forces.
 » 22 months ago, # |   0 Can someone please tell why this code for problem C is giving TLE, inspite of O(sqrt(x)) solution? Code: 107231229
•  » » 22 months ago, # ^ |   +1 Because in works in O(min(x, y) — sqrt(x, y)) >= O(sqrt(x, y))
•  » » 22 months ago, # ^ |   +1 Is it O(sqrt(x))? Because I think you are iterating from sqrt(x) to x, which I think is O(x).
 » 22 months ago, # | ← Rev. 2 →   +93 I see what you did there ;) Problem — Floor and Mod
•  » » 22 months ago, # ^ |   0 Is this dejaVu ? Cause I already saw this in the contest blog ig.
•  » » » 22 months ago, # ^ |   0 I earlier posted it there but for some reason it got removed.
•  » » 22 months ago, # ^ |   0 ROFL !! Keen observer !!
•  » » 22 months ago, # ^ |   0 I see that your upvote count is nearing 69 hope it stays there :rofl:
 » 22 months ago, # |   0 The contest was really enjoyable! Thanks for the fast editorials!
 » 22 months ago, # |   0 was a good contest .. good work authors ...and editorial with hints is really a good idea :)
 » 22 months ago, # |   0 I used the same logic in div2A. Can someone please help me where I went wrong?Link to submission: https://codeforces.com/contest/1485/submission/107217438
•  » » 22 months ago, # ^ |   +6 I guess it's because double are devil. On case 5 test 3 your l should be 3 but its just à little bit less because of approximation your flag isn't activated, rewrite without double and log and it should work
•  » » » 22 months ago, # ^ |   0 Thanks, got it. I never knew this happened with log. Could you explain why exactly does this happen?PS. It's so annoying to get WA for such a silly mistake, could've been a specialist for the first time mayb :( .
 » 22 months ago, # |   0 Why does first solution in F has complexity O(N^2*log N)? Don't we have O(N^2) elements in map in worst case (if almost all subsegments have different sums) and get complexity O(N^3*log N)?
•  » » 22 months ago, # ^ |   +3 The possible different sums for each length are $O(n)$, because they only depend on the rightmost $i$ such that $b_i = \sum_{k=1}^{i} a_k$.
•  » » » 22 months ago, # ^ |   0 It was too easy) Thank you!
 » 22 months ago, # |   0 In the editorial of problem A, it is given that even if we check the value of b up to 6 it will work. can someone explain why ?
•  » » 22 months ago, # ^ |   0 Not a formal proof, but here's an intuitive way to explain it: log(1e9)/log(b+1)+1
•  » » » 22 months ago, # ^ |   0 I did not understand how it proves that, can you explain it?
•  » » » » 22 months ago, # ^ |   +3 The number of moves needed without increasing $b$, is $\frac{log(a)}{log(b)}$So, if $\frac{log(a)}{log(b+1)}+1>\frac{log(a)}{log(b)}$, it means it's not convenient to increase $b$.For all $a$ up to $10^9$, this is true for every $b >= 6$, so it is never convenient to increase $b$ if it is $>=6$
•  » » » » » 22 months ago, # ^ |   0 For all $a$ up to $10^9$, this is true for every $b>=6$, so it is never convenient to increase $b$ if it is $>=6$ We can say this only by verifying or there is some short way to get the above conclusion using the formula in your comment ?
•  » » » » » » 22 months ago, # ^ | ← Rev. 5 →   +3 Yeah, you can solve for $b$ in that inequality. Also, that inequality is just $log_{b + 1}a + 1 < log_ba$.
•  » » » » » » » 22 months ago, # ^ |   0 Ok, so i think we need to first put the value of $a$ and then solve for $b$ .But to justify for all $a<=1e9$ using this formula , we need to iterate for all values of $a$.
•  » » 22 months ago, # ^ |   0 It's trivial to prove that the answer is <= 31 because you can solve it with +1 to make it 2 and divide it by 2 30 times. So test it up until 31 if that makes you happy.
 » 22 months ago, # | ← Rev. 2 →   0 I understood the O(n^2logn) solution of problem F. But I don't get how to get rid of first layer of dp. Specifically in the editorial — Let's try to get rid of the first layer of the dp. It turns out that the operations required are....can anyone explain this ?
 » 22 months ago, # |   +15 Here are video solutions to all problems, a challenge version of E, and a lesson on how to misread problems (of course, the last two things are totally unrelated)
 » 22 months ago, # |   +4 An easier solution for A with dp if you don't know how to prove your greedy solution, here. Dp state is represented by $a$ & $b$. At every stage we have two choices: 1) divide $a$ by $b$. 2) increase $b$ by 1. Now take the minimum of these two at every state. Also in this way we can see that $b$ can go till 1e9, but we can observe that $a$ will be zero if we divide it once with atmost 13 consecutive numbers in worst case. So we can stop if $b$ goes beyond.(for safety i put it 20 instead of 13 in code).
 » 22 months ago, # | ← Rev. 3 →   0 I got WA in Problem A because I used a log() function :(( for (int i = b; i < b + 50; i++) ans = min(ans, floor(log(a) / log(i)) + i - b); When values are for e.g. a = 18^7 and b = 18, log(a) / log (b) returns 6.9999999999994... something. Does anyone know a better way to use log() and floor() to bypass this issue?
•  » » 22 months ago, # ^ | ← Rev. 2 →   0 temp = (log(a) / log(i)); while(pow(i,temp) <= a ) temp++;ans = min(ans, temp + i — b);
•  » » 22 months ago, # ^ |   0 floor(log(a)/log(b) + 1e-11) should work
 » 22 months ago, # |   +3 what is greedy solution in problem A and how to prove the last statement in editorial : If we notice that it is never convenient to increase $b$ over $6$, we can also achieve a solution with better complexity.
•  » » 22 months ago, # ^ |   +9 $a = 10^9, b = 1$$2^{30} > a, steps = 31 3^{19} > a, steps = 21 4^{15} > a, steps = 18 5^{13} > a, steps = 17 6^{12} > a, steps = 17 7^{11} > a, steps = 17 8^{10} > a, steps = 17 9^{10} > a, steps = 18 10^{10} > a, steps = 19 •  » » » 22 months ago, # ^ | 0 thanks , example can help to visualize . How to prove it formally ? Like for every a,b <= 1e9 ? •  » » » » 22 months ago, # ^ | ← Rev. 3 → +3 1). a = 10^9, b \in [1, 10^9]As you can see before, the distribution converges at 5 and diverges from 9 for b = 1 the same holds for any b cuz the difference stays the same.2). a \in [1, 10^9], b = 1Likewise, \exists x \mid log_2a + 1 > log_3a + 2 > \dots > log_xa + x - 1 < log_{x + 1}a + x < \dots.And I can state the same from above, the distribution converges at x and diverges from x + 1 for b = 1 the same holds for any b cuz the difference stays the same.Thus, 6 is the upper-bound for this problem since a can be at most 10^9. P.S. The upper-bound could vary accordingly to a.  » 22 months ago, # | +12 For F the editorial definition is slightly different from what I thought. Define dp[i][j] as the number of ways of using i numbers and getting j = CURRENT_SUM — sum[1....i]. Under this definition, taking a[i] = b[i] makes the next j be the same as j so if we do nothing we already do the transition. For the other transition, we make j = a[i] — sum[1...i] so we take the total number of ways we have so far and exclude the number of ways such that the sum is the same in this transition or simply override dp[i][j] with the total number of ways in i-1 as the official solution does. In the end the code is equal but this way of thinking seems slightly cleaner (in my opinion)  » 22 months ago, # | +17 Very nice problems!! Sad on missing out on Master due to FST in problem B :(  » 22 months ago, # | 0 Hi, I think that I might have a better solution for problem div2A. 107216266 I can't say what the complexity of my submission is with total certainty but I think that maybe it is better than that of the proposed solution. Basically, I try all possible divisors in increasing order while keeping track of the previous answer. When it gets to the point where the current solution is worse than the previous one, then it stops the loop and returns the previous answer.I don't have a formal mathematical proof to state why this works, but it does. It would be great if someone could explain what the complexity of my code is :) •  » » 22 months ago, # ^ | 0 Your solution is pretty much the same as the official solution, the complexity is O(log(n)), but it can be done in O(1) if you use the builtin log function (paying attention to floating point errors).The reason why in the editorial we say our complexity is O(log^2(n)) is because we did not prove that the number of increases needed is O(1) (it's not, it's prob something like O(log(log(n)), 6 for these constrains, but if b is greater than that, than you don't need to increase at all), for simplicity we just proved it is O(log(n)) •  » » » 22 months ago, # ^ | 0 Great! thanks •  » » 22 months ago, # ^ | ← Rev. 3 → 0 A quick proof of correctness is to note that the number of steps required is the pointwise floor of a convex function. f(x) = \lfloor g(x) \rfloor \text{, where } g(x) = x-b + 1 + \frac{\ln a}{\ln x} g'(x) = 1 - \frac{\ln a}{x \cdot (\ln x)^2}$$g'$ is obviously increasing, so that $f(x)$ is non-increasing for $x \cdot (\ln x)^2 \leq \ln a$ and non-decreasing for $x \cdot (\ln x)^2 \leq \ln a$. This also means that the optimal value of $x$ is of order $\frac{\log a}{(\log \log a)^2}$.
 » 22 months ago, # |   0 Great Contest, 1st, 2nd, and 4th questions were purely math-based. They should include problems from other topics as well.
 » 22 months ago, # | ← Rev. 2 →   +4 all you have to do in Djust get the lcm for the numbers from 1 to 16and get for every number from the 1 to 16 the multiple to it and call it x and the different between the lcm and x have 4'th sqrt (yeaaaaaaaaah all that just in 1 minute brute force in your local pc)color the grid as a chess boardreplace the black cells by the LCMand replace the white cells by the answer that you got from the brute forcedo you need a proof?ok i don't give a sh....i am just upset because I did not try in D in the contest :(
•  » » 22 months ago, # ^ |   0 How do you write a brute force efficiently?
•  » » » 22 months ago, # ^ | ← Rev. 2 →   0 all that will just to get the answer for every number and take it by your hands
•  » » » » 22 months ago, # ^ |   0 Nvm, I misunderstood. I thought that you got the idea by bruteforcing the entire solution.By the way, you don't even need to brute force values on white cells, you can just put $720720 + a_{i, j}^4$.
•  » » » » » 22 months ago, # ^ | ← Rev. 2 →   0 yp, i was in brain lag edit: i was thinking that the next number is lcm * aij^4 instead of lcm + aij^4 :(
 » 22 months ago, # |   0 Great problems+fast editorial with hints are the best combo.
 » 22 months ago, # | ← Rev. 2 →   +1 For C I have good solution it don't requires math just logical binary search. See think of the graph of quotients plotting on (b) axis. For calcuating the peak of quotients basic observation will be for random number x on b-axis having remainders [a>=(x+1)(x-1)]=>>(a=x^2-1) =>>a+1=x^2.This equation comes from X=(Y.r)+r is now x=(y+1)r. So min value of(sqrt(a+1),b). Cool I got peak ! This is digram.. Now iterate from right end using binary search locate the location where quotient is still the same. And subtract both the points multiplying by number of quotients. In this keep way iterating to peak ((sqrt(a+1),b).
 » 22 months ago, # | ← Rev. 2 →   0 Can anyone tell me why this code for E gets WA on test 3? I followed the (alternate) strategy given in the editorial.submissionEDIT : very dumb errorHere is the corrected implementation of the editorial approach, if it helps.Corrected code
 » 22 months ago, # | ← Rev. 2 →   +15 I have a different explanation for problem F , though the final approach is almost the same.At each point we have two choices :1) Make $a[i] = b[i]$2) Make $a[i] = b[i]-$(prefix_sum up to $i-1$ elements in a[]).So answer could have been $2^n$ except when both choices gives the same array. When both the choices give the same array , prefix_sum of $i-1$ elements should be 0. This way I got 2 tasks , (1) Find the number of distinct arrays for first i elements : dp1 , (2) Find the number of distinct arrays for first i elements having sum 0 : dp2.Then we directly have, $dp1_i = (2*dp1_{i-1}) - dp2_{i-1}$.To calculate $dp2_i$, we have one more observation that, when we do operation of type (1), $b[i]$ adds to the previous prefix_sum and for type (2), prefix_sum becomes $b[i]$. This tells us that, prefix sum of i elements in a[] will be a subarray of b ending at i. So $dp2_i$ considers the smallest subarray of b ending at $i-1$ and having sum 0 (This is a standard task using hashmap). Let's say $[x,i-1]$. Then we take a unique subarray of $x-1$ elements, do a type 2 operation for x and do type 1 operation for elements in range $(x+1,i-1)$. This gives $dp2_i = dp1_{x-1}$.
 » 22 months ago, # |   0 In problem C, I'm not getting how we're getting the no. of special pairs just by counting the possible no. of b, because can't there be different a's for a fixed b and a fixed k? If so, then how are they being considered in our answer?
•  » » 22 months ago, # ^ |   0 $a = kb + k$.
•  » » » 22 months ago, # ^ |   0 In EDITORIAL Therefore, for any fixed k>0, the number of special pairs (a≤x; b≤y) is max(0,min(y,x/k−1)−k). Why we have subtracted k from min(y,x/k−1) and taken maximum of both to count special pairs???
•  » » » » 22 months ago, # ^ | ← Rev. 3 →   +5 Let's look at the four inequalities required for solving this question: b > k (derived in the editorial) 1 <= b <= y (given in the question) 1 <= k(b + 1) <= x (derived in the editorial)Modifying equation 3, we get 1/k — 1 <= b <= x/k — 1 1 <= k < b (derived in the editorial) (Why k != 0? If k = 0, then a = k*(b + 1) = 0 but a >= 1, so the minimum value of k is 1)Now from equation 1, we know that the minimum value of b is greater than k and from equation 2 we know that the upper limit of b is y.So we get the following inequality if we merge equation 1, 2 and 4k < b <= min(y, x/k — 1)Now we got an open interval at the lower limit and closed interval at the upper limit, so the number of elements in the interval is Upper Limit — Lower Limt = min(y, x/k — 1) — k.Now the value of k can exceed the value of min(y, x/k — 1) since 1 <= k <= sqrt(x) and in this case, we will get negative number of elements in our interval which is not possible. So if we get negative number of elements, in such cases it means we have 0 elements. That is why we must take max(0, upper limit — lower limit).Example: If y = 2, x = 36, k = 6min(2, 36/6 — 1) — 6 = min(2, 5) — 6 = 2 — 6 = —4.
•  » » » » » 22 months ago, # ^ | ← Rev. 3 →   0 .
•  » » » » » » 22 months ago, # ^ |   +1 a mod b = k, that is, k is the remainder when we divide a by b.Now applying Euclid's Division Lemma we can write a = bq + k, where q = $\lfloor{\frac{a}{b}} \rfloor$ = a mod b = k (Given in question)Since k is the remainder, we 0 <= k < b by Euclid's Division Lemma or b > k.
•  » » » » » » » 16 months ago, # ^ |   0 Can you please explain why k is always less than or equal to sqrt(x)
•  » » » » » » » » 16 months ago, # ^ | ← Rev. 2 →   0 $0$ $<=$ $k$ $<$ $b$ (By Euclid's Division Lemma)Multiply both sides by $k$.$k*k$ $<$ $k*b$$=>$ $k^2$ $<$ $k*b + k$ (Since $k$ is non — negative, the RHS sum can remain same or increase but never decrease)But $k*b + k$ $=$ $a$ $<=$ $x$ (Given in the question).Hence $k^2$ $<$ $x$ or $k$ $<$ $\sqrt{x}$.Now here we need to take equality in case $x$ is not a perfect square else you can drop the equality. This is because in case of non square number, we will be missing 1 number.
•  » » » » » 21 month(s) ago, # ^ |   0 By that logic we can use equations 2 and 4 to get :elements in the interval is Upper Limit — Lower Limit = min(y,x/k-1) — max(1/k-1,1)) that should work right ?
 » 22 months ago, # |   0 I tried to calculate $Dp_i$ from bottom to top in problem E, but got wa on test 3. It works like this: bool cmp(int k,int b){return a[k]
 » 22 months ago, # |   +3 In Problem C, in the final expression, max(0,min(y,x/k−1)−k), Why are we subtracting k from min(y, x/k-1). Can someone please explain?
•  » » 22 months ago, # ^ |   0 x/k gives a count of numbers which can be our b but we need a remainder equal to k, that's why we decrease it by 1 (think?), if it is greater than y, we can take any number up to y as our b, but wait.... we can't take any b which is less than or equal to our remainder, therefore we again subtract k from it.
 » 22 months ago, # |   0 Can anyone tell why this binary search solution for A failed?
•  » » 22 months ago, # ^ |   0 Are you sure that the f is a decreasing function ? I think that it is a first decreasing and then increasing function. Ternary Search works for this kind of functions in O(log(n)).SubmissionBut I can't prove that f is is a unimodal function !
•  » » » 22 months ago, # ^ |   0 I got it my solution is wrong because f is monotonic.
•  » » » » 22 months ago, # ^ |   0 You mean not monotonic ?
•  » » » » » 22 months ago, # ^ |   0 No monotonic, i.e it is nondecreasing first then nonincreasing. If f(mid) = f(mid + 1) before the minima then I would get the wrong ans.
•  » » » » » » 22 months ago, # ^ |   0 I think your definition of monotonic is wrong. Google this.
•  » » » » » » » 22 months ago, # ^ | ← Rev. 3 →   0 Yes, it is monotonic before the minima and after the minima.
•  » » » » 22 months ago, # ^ | ← Rev. 2 →   0
•  » » » » » 22 months ago, # ^ |   0 That's ternary search, not binary search, because you're looking at the slopes of adjacent elements. I'm not sure how to formally prove that the function is unimodal, but it's pretty easy to see intuitively.
•  » » » » » » 22 months ago, # ^ | ← Rev. 3 →   0 Yep, but I split the search space into halves at each iteration. Also, the function is not unimodal.
•  » » » » » » » 22 months ago, # ^ | ← Rev. 2 →   0 It's still called ternary search, even though you're splitting the search space in half. See cp-algorithms for more detail. That's actually how I code ternary search if I'm not working with doubles (I actually did this solution in contest, so you can check my submission). Binary search only works with monotonic functions, but ternary search works with any unimodal function.
•  » » » » » » » » 22 months ago, # ^ | ← Rev. 2 →   0 It is Binary Search. Yep, F is monotonic.
•  » » » » » » » » » 22 months ago, # ^ | ← Rev. 2 →   0 That example, is different, as you're not really searching on a function. You can read about monotonicity on boolean functions (functions used in binary search) here. If f was monotonic, it would mean that looking at a single value, it would be possible to determine whether to go to the right or to the left. Ternary search can essentially be viewed as binary search on the slopes of the function, instead of the values itself. That's what differentiates them. If you look at the slopes of unimodal function, and define $g(x) = f(x)-f(x-1)$, you can see that some prefix of a unimodal function will have some sign, and the suffix will have the opposite sign, which makes $g(x)$ monotonic, but $f(x)$ unimodal.Btw, a function is unimodal if it first increases, then decreases (or vice versa). See here for more details.
•  » » » » » » » » » 22 months ago, # ^ | ← Rev. 5 →   0 A common definition is as follows: a function f(x) is a unimodal function if for some value m, it is monotonically increasing for x ≤ m and monotonically decreasing for x ≥ m. In that case, the maximum value of f(x) is f(m) and there are no other local maxima. Yeah, I said f is not unimodal since, you can see four local minima here.Arguing if my approach is binary search or ternary search is self-contradictory. I should've mentioned it Divide and conquer in general.
•  » » » » » » » » » 22 months ago, # ^ |   0 Those local minima you mentioned are also the global minima, so it still satisfies unimodality (it's decreasing instead of strictly decreasing for that segment). Because they are in the same contiguous segment, I think general consensus is that they are still global minima. At this point, we're really arguing about if it's convention to call a function which is decreasing but not strictly decreasing monotonically decreasing, and I guess it doesn't really matter. In general for ternary search/binary search purposes, I would call it unimodal/monotonic because it's still possible to binary search/ternary search over the functions.
 » 22 months ago, # |   0 Shouldnt k <= root(x) for problem C be k < root(x)? Since if k^2 = x implies b=k-1 which is less than k
 » 22 months ago, # |   0 I have a question: In problem C's editorial, once "b" and "k" is fixed, is the value of "a" determined and unique?
•  » » 22 months ago, # ^ |   0 Yes
 » 22 months ago, # | ← Rev. 6 →   0 How to prove the complexity for the O(n^2logn)-algorithm for prob F?Thanks
•  » » 22 months ago, # ^ |   0 $dp_i$ contains at most $1$ more entry than $dp_{i-1}$.
 » 22 months ago, # | ← Rev. 2 →   0 In problem 1485A - Add and Divide, BFS was fast and memory-efficient enough to get accepted. 107307717
 » 22 months ago, # | ← Rev. 2 →   0 Can someone explain problem B editorial when x is in-between al and ar, this exactly "There are (ar−al+1)−(r−l+1) such values of x."
•  » » 22 months ago, # ^ |   0 There are $a_r - a_l + 1$ integers between $a_l$ and $a_r$. Since $x \neq a_i$, you have to subtract the $r-l+1$ integers already in the array, in the range $[l, r]$.
 » 22 months ago, # |   0
 » 22 months ago, # | ← Rev. 2 →   0 Hello. I've been trying to get "accepted" on problem C in Python3 but I get TLE while implementing the solution suggested here.Here is the link to my submission: 107347518I googled a bit and it turns out that the time limits don't seem to be language-specific, i.e. the same algo implementation might be passing in C but not in Python. link: https://codeforces.com/blog/entry/45228Is this the case? Are some problems not meant to be solved as long as you don't write in the language intended or is something wrong with the submission I posted?Thanks
•  » » 22 months ago, # ^ | ← Rev. 2 →   0 Check the following update to your code.107361634The same update ran in PyPy 3.7 more than an order of magnitude faster than it did in Python 3.9.107361941
•  » » » 22 months ago, # ^ |   0 Thanks for the update!Yep, the code passes now at 1965ms and we have whole 35ms to spare!
•  » » » » 22 months ago, # ^ |   0 Yes, the speed difference between the Python interpreter and the PyPy compiler in running the same code is obvious.
 » 22 months ago, # |   0 There is a O(N) solution for F. Note that this solution uses a map. The solution is as follows -Let f[i] denote the number of hybrid arrays of length i. Let g[i] denote the number of hybrid arrays of length i and sum of the array is 0. Given that we have a[0], a[1], a[2] . . . a[i-1] we have two options for the ith position — 1) Place b[i] 2) Place b[i] — (a[0] + a[1] + a[2] + . . . . a[i-1]) Now there will be over-counting when b[i] = b[i] — (a[0] + a[1] + a[2] + . . . . a[i-1]), or a[0] + a[1] + a[2] + . . . . a[i-1] = 0. Thus f[i] = f[i-1]*2 — g[i-1].Now how to find g[i]? Observations — 1) The sum of the hyprid array 'a' of length i will always be a suffix of array 'b' ending at i. 2) Let j <= i be the maximum index where sum(b[j] . . . b[i]) = 0. Then for the sum of array 'a' to be equal to sum(b[j] . . . b[i]) = 0, we need two things — 2.1) a[j] = b[j] — sum(a[0] . . .a[j-1]) 2.2) For all j < k <= i, a[k] = b[k]. The array a[0]. . . a[j-1] can be anything we dont care. From 1 and 2 we can say g[i] = f[j-1]. 'j' can be found for every 'i' by maintaining map of prefix sums. 107307490
•  » » 22 months ago, # ^ |   +10 This is exactly the official solution, and it's $O(n\log n)$ because of the map.
 » 22 months ago, # |   +1 Hello, can anybody explain me div2-C, I'm not able to understand how is k<=sqrt(x)...Thanks in advance :)
 » 22 months ago, # |   0 in problem C (floor and mod) max(0, min(0, x/k — 1) — k). Why do we subtract k from min(0, x/k — 1)?
•  » » 22 months ago, # ^ |   0 $b > k$.
 » 19 months ago, # | ← Rev. 2 →   0 I'm a bit confused with where the factor of 2 comes from in B. Does someone have an explanation for this? Specifically, why it's $2((a_r-a_l+1) - (r-l+1))$. Thanks!
•  » » 19 months ago, # ^ |   +8 I think I just understood it. I'll comment it here just in case someone else had the same question:consider the array [1, 2, 4, 5] for k = 5. Let $x = 3$. This mean that we could either replace 2 or 4. Similarly, $\forall x$ such that $a_l < x < a_r$ and $x \not= a_i$, it is the case that there will be two options for us to choose. The expression $(a_r-a_l + 1 - (r-l+1))$ counts the number of such $x$s. We just have to multiply that by two since for each such $x$, there are two options.
 » 2 weeks ago, # | ← Rev. 2 →   0 Explanation of B could have been much easier.You can replace all the missing values withing range [1, arr[right]). You can replace all the missing values withing range (arr[left], k].And answer is the summation of these two values.