### awoo's blog

By awoo, history, 3 months ago, translation,

1452A - Robot Program

Idea: BledDest

Tutorial
Solution (BledDest)

1452B - Toy Blocks

Tutorial

1452C - Two Brackets

Idea: BledDest

Tutorial
Solution (pikmike)

Idea: BledDest

Tutorial
Solution (BledDest)

1452E - Two Editorials

Idea: BledDest

Tutorial
Solution (pikmike)

1452F - Divide Powers

Tutorial

1452G - Game On Tree

Tutorial
Solution 1 (pikmike)
Solution 2 (BledDest)

• +98

•  » » 3 months ago, # ^ |   -30 Who cares about rating, right?
 » 3 months ago, # |   +28 I think, in tutorial G, Alice and Bob had been swapped. It's Alice who has one starting vertex, and it's Bob who chases her, not vice versa.
•  » » 3 months ago, # ^ | ← Rev. 2 →   +10 Oh, indeed. I've been told the problem with them reversed and I haven't read the actual statement haha. Will fix in a sec, thanks.
 » 3 months ago, # |   0 Can anyone explain problem A's solution a bit more. I couldn't get it!
•  » » 3 months ago, # ^ |   -8 See let min(x,y)=aThen, we can first go to (a,a) in exactly a*2 steps. After that we just have to increment in one direction and the best way to do that is by first waiting then moving because it always takes comparatively less number of steps,i.e, 2.Also note we can always reach (a,a) in two possible ways by first moving right or left, so when we take steps from (a,a) we can always increment one step without waiting.So, answer is (a*2)+1+max(y-x-1)*2 when (y-x-1)>=0 else answer is simply (a*2) when x==yYou can look at my submission 98961942. Hope you find it helpful !
•  » » » 3 months ago, # ^ | ← Rev. 3 →   0 can we write second condition when x!=y as 2*max(x,y)-1 ?? if not please give some examples sorry i didn't read the editorial before
 » 3 months ago, # |   0 why i get tle in test case 20 of problem E my submission : 99017296logic
•  » » 3 months ago, # ^ |   +5 Comparator for sorting should follow strict weak ordering. If $a=b$ then it should always return false, otherwise it's undefined behavior.
•  » » » 3 months ago, # ^ |   0 thanks it work now .
•  » » » » 2 months ago, # ^ |   0 man 
 » 3 months ago, # | ← Rev. 3 →   +14 If anyone didn't understand B. Here is an easy solution-For solution to exist let us consider an element any index i. Then, for solution to exist we should be able to increment all values at all indexes to the max value in the array because we can't decrement the max value anyway. Thus, a[i]>=(a[max]-a1)+(a[max]-a2)......(a[max]-a[max]) or, a[i]>=a[max]*n-1-(S-a[i]) where S is total sum of all elements. or, S>=a[max]*n-1; So solution boils down to two cases-1. When Sa[max]*n-1 then (S-a[max]*n-1) should be equally divisible by (n-1), so just increment S until divisible by (n-1) and the answer is difference of the two.You can look at my submission here
•  » » 3 months ago, # ^ |   0 I had exactly same thought process, https://codeforces.com/contest/1452/submission/98940264.
•  » » » 3 months ago, # ^ |   +1 Yes, exactly we both used the same logic and so I thought it may help some people if they were thinking this logic but couldn't get it properly. Did I explain it clearly?
•  » » » » 3 months ago, # ^ |   0 yup!
•  » » » » 3 months ago, # ^ |   0 Thanks! really helpful
•  » » 3 months ago, # ^ |   0 very good solution!
•  » » 3 months ago, # ^ |   0 Thanks a ton! I was really having trouble understanding the editorial! :D
•  » » 3 months ago, # ^ |   0 I thought the same but couldn't implement it in the right manner ,I was thinking it as either all the elements have to be made equal to a[max] or equal to a[max]+1 but i couldn't get a clear understanding
•  » » » 3 months ago, # ^ |   0 I also realized it after the contest and couldn't solve in contest time.
•  » » 3 months ago, # ^ |   0 very well explained. I really wish if editorials explain it so well.
•  » » 6 weeks ago, # ^ |   0 Thanks, well explained.
 » 3 months ago, # |   -39 My code gives me wrong answer in test case 3 any one help Question no Bvoid solve() { ll n; cin>>n; ll a[n]; ll mx = 0 ; for (ll i= 0;i>a[i] ; mx = max(mx, a[i]); } if (n==2) { cout<<0< sum) { cout<<(abs(n-1)*i*(1ll) - sum)<
•  » » 3 months ago, # ^ |   +5 use spoiler please
•  » » 3 months ago, # ^ |   0 Try using 0ll, in std:: accumulate, otherwise it gives integer overflow.
 » 3 months ago, # |   0 Proof by Induction for problem D is great but can someone tell me if there is a mathematical proof for that?
•  » » 3 months ago, # ^ |   0 If we have a tower with power p, it will cover 2p-1 spots, thus the length of the subsegment covered by a tower is always odd.Now we need to find a way to divide a length of n into a number of odd length subsegments. This can be done via dynammic programming, which reduces to finding the nth Fibonacci number. In the end , just divide this by 2^n.
•  » » » 3 months ago, # ^ | ← Rev. 2 →   0 I'm sorry but I didn't need the proof for the solution to this problem. I was wondering how did that function get reduced to just finding the Fibonacci number for a particular input.(which was proved by Induction by the Editorialist)
•  » » » » 3 months ago, # ^ |   +19 f(n) = f(n-1) + f(n-3) + f(n-5) + .... f(n-2) = f(n-3) + f(n-5) + .... thus f(n) = f(n-1) + f(n-2) Is this what you were looking for?
•  » » » » » 3 months ago, # ^ |   0 Yeah exactly, thanks for putting it that way. Made it so easy to understand.
•  » » » » » » 3 months ago, # ^ |   0 i didnt get it, can you explain how that comment helped you? i m still confused
•  » » » » » » » 3 months ago, # ^ |   0 Just look at what he wrote:f(n)= f(n-1) + f(n-3) + f(n-5) + .... The above equation should be sufficient to tell you why is this the answer to the problem. You can occupy 1,3,5 ... positions at a time let us call it k and question is reframed as finding the solution to the remaining f(n-k) values which is what the equation above tells. Now, Just replace n by n-2 in the above equation and we get,f(n-2) = f(n-3) + f(n-5) + .... This is just the same as f(n) without the f(n-1) part.thus f(n) = f(n-1) + f(n-2)
•  » » » » » » » » 3 months ago, # ^ |   0 ok now i got it, thanks!
•  » » » » 3 months ago, # ^ |   +14 Here's the intuitive proof I came up with while upsolving.First let's solve it like a normal dp problem (forgetting about the fibonacci numbers). We sweep from left to right. Let $dp[i]$ mean the number of ways to cover only the first $i$ positions.How do our transitions work? First, $dp[0] = 1$ because there is one way to cover the first $0$ positions (don't have any towers at all). Now loop over all i's (from left to right).Consider the current $dp[i]$. We can cover some range with exactly one tower if and only if the length of the segment is odd (so we can put the tower in the midpoint). So $dp[i]$ is equal to the sum of all dp values where the index has a different parity than i (so $dp[i] = dp[i-1]+dp[i-3]+dp[i-5]...$). Though this would work, it runs in $O(n^2)$, so it needs to be optimized.The optimization is pretty simple. Because you don't care about the actual values of all previous dp's but just the parity. So instead of storing the whole dp array, just have it store 2 values, $dp[0]$ and $dp[1]$. $dp[0]$ stores the numbers of ways to cover an even prefix, and $dp[1]$ stores the number of ways to cover an odd prefix.You still loop from 1 to n but the transitions are now $dp[i \mod 2] = dp[i \mod 2] + dp[1-(i \mod 2)]$. This is exactly the Fibonacci recurrence.Hope this was helpful! Thanks!
•  » » » » » 3 months ago, # ^ |   +1 That's a really nice way to think about it. Thanks.
•  » » » » » 3 months ago, # ^ |   0 hey thanks for sharing your idea . I have implemented the same idea with bottom up DP. but i am not able to do the same with top down DP. Can you please help me with that ?
•  » » » » » 3 months ago, # ^ |   0 @PurpleCrayon how did you come up with the idea that dp[i] is equal to the sum of all dp values where the index has a different parity than i ??
•  » » » » » 3 months ago, # ^ |   0 Nice explanation!
•  » » » » » 3 months ago, # ^ |   0 "We can cover some range with exactly one tower if and only if the length of the segment is odd (so we can put the tower in the midpoint). So dp[i] is equal to the sum of all dp values where the index has a different parity than i." I understood the first statement that we can cover some range with exactly one tower if and only if the length of the segment is odd but i am not able to understand from this how you drew the second statement as conclusion. Can you please explain?
•  » » 3 months ago, # ^ |   +2 I had a different approach to this problem which is quite intuitive imo. We are effectively choosing a subset out of $n$ towers such that the sum of powers of those towers cover the entire range $1-n$.This is equivalent to number of odd integral solutions of the equation $p_1 + p_2 + ... + p_r = n$.This can be found out easily by converting it to the form $(2*x_1 + 1) + (2*x_2 + 1) + .. + (2*x_r + 1) = n$ and finding non negative integral solution for $x_1 , x_2 ,... x_r$. Multiply this with the probability of choosing $r$ towers = $1/{2^{n}}$ to get the required answer solution
•  » » » 3 months ago, # ^ |   0 Thank you very much! It's a great solution.
•  » » » 3 months ago, # ^ |   0 i am sorry, this might sound dumb but in you solution , ncr(d+x-1,x-1) here what are you actually choosing and from what
•  » » » » 3 months ago, # ^ |   0 number of non negative integral solutions of the equation $x_1 + x_2 + ... +x_r=d$ is $d+r-1\choose r-1$
 » 3 months ago, # | ← Rev. 2 →   +5 Why this Solution do not get TLE in problem G? I solved only with bfs/dfs. There is some property?
•  » » 3 months ago, # ^ | ← Rev. 3 →   +5 As in Tutorial, this solution is at most $O(nlog^2n)$
•  » » 3 months ago, # ^ |   0 this idea seems at first to be O(n^2) in the worst case. I don't know exactly why this works. but the best counter example i could thought of was a graph with O(x^2) vertices for which the number of total iterations of the dfs section is O(x^3). it means this algorithm is at least O(n sqrt(n)), which seems to run fine in time even for my counter example. maybe there is a way to prove that this is still the worst case complexity. it would be great, as this is such a simple solution.an instance of this type of graph is given next. in general, this graph has 1/2 (5 x — 2) (x + 1) vertices (x = 3 for this instance) and the dfs runs x^3+x^2 steps in total. I implemented a generator to test for biggers instances. if you draw it starting from bob's chips and then the vertices which are further away, you will probably understand the idea. input26 3 4 10 11 14 15 13 18 23 24 24 25 9 10 8 12 16 17 12 13 2 3 7 8 17 18 20 21 1 2 6 7 25 26 21 22 5 6 4 8 18 19 18 24 15 16 22 23 11 12 5 1 5 9 14 20
 » 3 months ago, # |   0 @BledDest Can you give more explanation how problem D can be solved. I didn't get the point how the probability can be considered as Fibonacci number. Please help me to understand.
 » 3 months ago, # |   0 In Problem D EditorialHow this statement is true?Covering all towns can be expressed as splitting n into the sum of several positive odd integersAnyone, Please Explain.
•  » » 3 months ago, # ^ |   +1 Each town that has power must power itself, and an equal number of towns either side of it.Suppose it powers K towns to its left, then it must also power K towns to its right. So in total it powers 2*K+1 towns.All the towers together power N towns, and the set is partitioned such that no town is powered twice. As such, N = sum (i = 1 to j) (2*Ki + 1), where there are j towns receiving power directly, and the ith town powers Ki either side of it.The numerator of the answer is the number of unique sets of j and K1,...,Kj such that the equality holds.
•  » » » 3 months ago, # ^ |   +3 Thanks a lot. I got it now.
 » 3 months ago, # | ← Rev. 2 →   0 What is the logic of taking max of the array in calculation of k for B?
•  » » 3 months ago, # ^ |   0 We need an end state where each of the n-1 boxes has exactly (TOTAL)/(n-1). Suppose we choose one of the non-max boxes to redistribute: then all the boxes must have at least the max starting value, since we cannot take any away. Therefore max*(n-1) is a lower bound for the total number.
•  » » » 3 months ago, # ^ |   0 yeah but why would there be atleast max in each box?
•  » » » » 3 months ago, # ^ |   0 Because if there is one box with max, we can't take any out of this box (if redistributing from any of the others). So that box must finish with at least max, and we require all the boxes to finish with the same number, so they must also have at least max.
•  » » » » » 3 months ago, # ^ |   0 Understood Thanks!
 » 3 months ago, # |   0 i successfully solved 1st problem by this=>  ll t; cin >> t; while (t--) { ll x, y; cin >> x >> y; ll z = abs(x - y); ll ans = (x + y) + ((z > 0) ? z - 1 : 0); print(ans); } but i am thinking can it be solvable using dp like other grid paths problem? i am new at dp so i am having a hard time to think about it.so can anyone help me with this if it is possible to solve using dp
•  » » 3 months ago, # ^ |   0 I have solved using dp. The idea is that the points on the diagonal(assume it is {x,x}) will always take x+x = 2*x. Other points can be reached from diagonal by this sequence. At diagonal -> Move -> Stop -> Move -> Stop #include #define ll long long const unsigned int mod = 1000000007; using namespace std; int main() { int t; cin>>t; // Store the steps taken to reach diagonals vector dp(10001,0); for(int i=1;i<=10000;i++) dp[i] = 2*i; while(t--) { int x,y; cin>>x>>y; if(x==y) { cout<y) { int ans = dp[y] + (x-y)*2 - 1; cout<
 » 3 months ago, # |   +3 IN PROBLEM B Why (a.sum() + n — 2) / (n — 1) and not (a.sum()) / (n — 1) i didn't get it
•  » » 3 months ago, # ^ |   0 the formula to get ceil of(x/y) is (x+(y-1))/y
•  » » » 3 months ago, # ^ |   0 Okk. But when I write ceil function, ceil(x/y), it throws an error, but writing (x+y-1)/y goes successful.
•  » » » » 3 months ago, # ^ |   0 Maybe you forgot to cast long double/float/double type to this fraction. For example: ceil(5 / 2) = ceil(2) = 2, but ceil(5.0 / 2) = ceil(2.5) = 3.
•  » » » » 3 months ago, # ^ |   0 But actually you should never try to use floating point numbers then use ceil, always opt to integer operations if possible.
 » 3 months ago, # | ← Rev. 2 →   -13 Why does this give same results, in modular exponentiation? ll powe(ll x, ll y){ x = x%mod, y=y%(mod-1); ll ans = 1; while(y>0){ if (y&1){ ans = (1ll * x * ans)%mod; } y>>=1; x = (1ll * x * x)%mod; } return ans; } than this ll powe(ll x, ll y){ x = x%mod; ll ans = 1; while(y>0){ if (y&1){ ans = (1ll * x * ans)%mod; } y>>=1; x = (1ll * x * x)%mod; } return ans; } The only difference is we do y=y%(mod-1) in the first function.Edit: figured it out its fermat's theorem, thanks everyone
•  » » 3 months ago, # ^ |   +1 Why don't people use spoiler while commenting there code?
•  » » 3 months ago, # ^ |   0 Isn't your question obvious, let say we want to compute 2^123097521478997542 mod m, then what do you think will be faster computing 2^123097521478997542 mod m or first compute y mod m-1 which will be smaller than equal to y, then compute power?
•  » » 3 months ago, # ^ |   0 The answer is obvious, but I remember there was a problem in which the input size it self exceeds mod-1 and everyone who did pow=pow%(mod-1) in advance got WA, be careful.
•  » » » 3 months ago, # ^ |   0 I'm sorry community, I framed it wrong, I actually meant why both lead to same answer.A friend of mine later told me its Little Fermat's theorem.This was my first comment in codeforces, wasn't aware about the comment standards we need to maintain, are there any resources, on how questions be asked and all?
 » 3 months ago, # |   0 In D problem, if there are 5 cities, the number of ways to break it into sum of odd positive integer is (3)1,3,1 which shows that there are 3 ways,Can anyone help me in understanding how is it the nth fibonacci ie (5)?
 » 3 months ago, # | ← Rev. 2 →   0 Why solutions with complexity O(nmk) can get AC for problem E? The example are below: submission 1 and submission 2
•  » » 3 months ago, # ^ | ← Rev. 2 →   0 upd: submission 2 has been hacked with the result of WA. But no one could made it TLE during the hacking phase.
 » 3 months ago, # |   +6 Can someone explain problem E's solution a bit more.Thanks in advance! xD!!
 » 3 months ago, # |   0 in the editorial of problem b the first para [ceil(sum/n-1)] is the condition when the nephew chooses the box containing the maximum block, right?
•  » » 3 months ago, # ^ |   0 Yes it is the block with max toys after you have added toys.
 » 3 months ago, # |   0 Problem E,why the solution is right?I spend a long time understanding the code,but can't prove it's correctness.The tutorial seems to ignore this.
•  » » 3 months ago, # ^ |   0 you may think that algorithm is incorrect because of prefix and suffix and that one of them may contain some element which should have been in the other one but it doesn't matter because answer for cases like this will not be the final answer anyway because we are considering all possibilities and final answer will be the case where the conditions are satisfied.
•  » » » 3 months ago, # ^ |   0 oh!miaoa~zhenbuchuo!thank you for your reply!
 » 3 months ago, # |   0 Why i get tle for B?submission : 99050069
•  » » 3 months ago, # ^ |   0 whatever error you meet,you can see the data,the data you tle has sum=5e9,and n-1=4,and ma=1e9,your ma should add many times before you can break,so you tle
 » 3 months ago, # |   0 There is another nice approach to Problem D using combinatorics.
 » 3 months ago, # | ← Rev. 3 →   0 For the problem D how can we find the number of ways in which N can represented as sum of odd integers as mentioned in the editorial ??: To be specific using the following method It can be calculated with dynamic programming with prefix sums. `
•  » » 3 months ago, # ^ |   0 I would like to know which method is this?
 » 3 months ago, # |   0 can anybody explain me ...in the problem toy blocks , why are you adding (n-2) to the array sum ? i.e. 4th line in fun main() function
•  » » 3 months ago, # ^ |   0 ceil function, that is to get the smallest greater integer.
•  » » » 3 months ago, # ^ |   0 So Can't we use directly ceil() function available in header cmath ?
•  » » » » 3 months ago, # ^ |   0 Since these are integers, ceil function wont work here.
 » 3 months ago, # |   0 And everyone ignored the pun in editorial of B. Not I but you.
 » 3 months ago, # |   0 Can anyone help me on the modular division given in solution of the problem d?Specifically why he has used 'mod-2' during the divide process?? Any document is also helpful...
 » 3 months ago, # |   0 cin >> a >> b; cout << a + b + abs(a — b) — (a ^ b ? 1 : 0) << endl;
•  » » 6 weeks ago, # ^ |   0 Please don't add codes like this
 » 3 months ago, # |   0 Why the divide function in problem D is mul(x, binpow(y, MOD — 2)) ? I understand the idea behind mul() and binpow(), but why they can perform the divide operation?
•  » » 3 months ago, # ^ |   0 It uses Fermat's little theorem to calculate Modular multiplicative inverse.
•  » » » 3 months ago, # ^ |   0 Thanks, now I know this. :3
 » 3 months ago, # |   0 I'm a bit confused about problem B's description. For example, the tests case 0,3,0. Why can't the answer be 1? You put one block into bin 1, then the kid puts 1 block from bin 2 into 1 and 2 blocks from bin 2 into bin 3. You're left with an even number of blocks.Is it because the kid is the one that is choosing a random box and you have to make sure no matter what he chooses, that he is happy?Thanks :)
•  » » 6 days ago, # ^ |   0 Yes. The kid can choose any box he wants and we have to make sure no matter which box he chooses it is possible to make the rest of the elements equal.
 » 3 months ago, # |   0 Can anyone please tell me what's wrong in my solution for problem C. Submission : 99756850
•  » » 3 months ago, # ^ |   0 Consider this case: )()( your solution will return 0 although the answer is 1; and cuz find() is O(n), it also will get TLE in huge cases.
•  » » » 3 months ago, # ^ |   0 Thanx[user:AmirrzwM]
 » 3 months ago, # | ← Rev. 2 →   0 nothing!
•  » » 3 months ago, # ^ |   0 So it is not accepting it!! What should I do??AmirrzwM
 » 3 months ago, # |   0 I have a $O(nm)$ solution for problem E. Basically, we do scanline. For each participant, we brute force over the left author, and see when can we have the left author as a winner for that participant, and then we do the same for right author. It's really cool IMO. 100792298
 » 2 months ago, # |   0 About question D, why didn't standard code reduce the fraction, or didn't need it at all.
•  » » 2 months ago, # ^ | ← Rev. 2 →   0 Sorry， I've proved that the same answer will be obtained after reduction.