### nubir345's blog

By nubir345, 5 weeks ago,

# Tricks

1. Sum-Xor property: $a+b = a \oplus b + 2(a\And b)$. Extended Version with two equations: $a+b=a\vert b + a\And b$ AND $a \oplus b = a\vert b - a\And b$ Problem 1 Problem 2
2. Upto $10^{12}$ there can be atmost $300$ non-prime numbers between any two consecutive prime numbers.
3. Any even number greater than $2$ can be split into two prime numbers. Problem1 Problem 2
4. Sometimes it is better to write a brute force / linear search solution because its overall complexity can be less. Problem 1
5. When $A \leq B$ then $\lfloor \frac{B-1}{A} \rfloor \leq N \leq \lceil \frac{B-1}{A} \rceil$ where $N$ is the number of multiples of A between any two multiples of B. Problem 1
6. Coordinate Compression Technique when value of numbers doesn't matter. It can be done with the help of mapping shortest number to $1$, next greater to $2$ and so on. Problem 1
7. Event method: When there is a problem in which two kinds of events are there (say $start$ and $end$ events), then you can give $-ve$ values to $start$ events and $+ve$ values to $end$ events, put them in a vector of pairs, sort them and then use as required. Problem 1 Problem 2
8. When applying binary search on $doubles$ / $floats$ just run a loop upto 100 times instead of comparing $l$ and $r$. It will make things easier.
9. For binary search you can also do binary lifting sort of thing, see this for more details. (I don't know how to add that code without messing up the list, that's why the link). Problem 1 Problem 2
10. Sometimes, it is useful to visualize array into a number of blocks to move towards a solution.

Also, if you know any tricks / methods that you want to share and are not in the blog then you can write in the comment section, I will add them to the blog.
I will update this blog if I come across any more general methods / tricks.

• +432

 » 5 weeks ago, # |   +319 If you know the proof of #3, please tell me.
•  » » 5 weeks ago, # ^ |   +28 I don't know the proof. It is not proved yet. But it is also not proved wrong (I think).
•  » » 5 weeks ago, # ^ | ← Rev. 3 →   -12 I used google and got this: https://arxiv.org/pdf/1811.02415.pdf and https://vixra.org/pdf/1702.0150v1.pdf
•  » » » 5 weeks ago, # ^ |   +37 By the way, it's a fine exercise. Find mistakes in the second one (the first one is significantly longer).
•  » » » » 5 weeks ago, # ^ |   +36 After 11 paragraphs of the second "proof", we just reached this impressive conclusion: "Therefore, we only need to prove that 2k, for every integer k > 1,is the sum of two primes."
•  » » » » » 5 weeks ago, # ^ |   0 Yup, that was the funniest part IMO.
•  » » » 5 weeks ago, # ^ |   +10 Those papers are from arxiv.. they are not peer reviewed papers..the proofs in those articles have either been proven to be false or else no has bothered to verify it being patently flawed.
•  » » » 5 weeks ago, # ^ |   +2 The proof called the goldbach conjecture. Many of mathematics are working hard to prove it. As a competitive programmer. The prove is simple green text "Accept".
•  » » 5 weeks ago, # ^ |   -6 There is no proof , only Goldbach Conjecture
•  » » 5 weeks ago, # ^ |   +133 I have discovered a truly marvelous proof of this, which this webpage is too narrow to contain.
•  » » » 5 weeks ago, # ^ |   0
•  » » 5 weeks ago, # ^ | ← Rev. 2 →   0 Why will I tell you if I can win one million dollars? :v Anyway you can use it in this problem.
•  » » 5 weeks ago, # ^ |   +116 The proof is the green text that says "Accepted".
•  » » » 5 weeks ago, # ^ |   -29 Depends on how strong are the system tests.
•  » » 5 weeks ago, # ^ | ← Rev. 3 →   +52 FWIW it's definitely been verified for numbers below $10^{18}$, so it's completely fine to use that property for any long longs (which is basically always the case for us).On this topic, there are some other similar conjectures. ARC 080 D makes use of some more "Goldbach-like" hypotheses.
•  » » 5 weeks ago, # ^ | ← Rev. 3 →   -31 We know that any prime number except 2 and 3 can be represented as 6*n+1 or 6*n-1. So, Just add two prime numbers: 1. (6*a+1) + (6*b+1) = (6*a+6*b) +2 //even number 2. (6*a+1) + (6*b-1) = 6*a+6*b //even number 3. (6*a-1) + (6*b-1) = (6*a+6*b) -2 //even number Let me know if I am wrong.
•  » » » 5 weeks ago, # ^ |   +13 You don't prove that every number can not be represented this way. This is Goldbach's conjecture which is an open problem.
•  » » » » 5 weeks ago, # ^ |   -11 https://primes.utm.edu/notes/faq/six.htmlWhat about these? We can represent a number in form of 6*n,6*n+1,6*n+2,6*n+3,6*n+4 and 6*n+5. So, considering a number prime it is one out of 6*n+1 or 6*n+5 because we can see other forms are divisible by 2 or 3.
•  » » » » » 5 weeks ago, # ^ |   0 What has this got to do with anything? Are you sire you understood the problem "statement".
•  » » » » » » 5 weeks ago, # ^ |   -8 Ok, My bad. It does not imply "every".Thanks
 » 5 weeks ago, # |   0 Trick 7 is my personal favourite
•  » » 5 weeks ago, # ^ |   0 Suggest a problem plox
•  » » » 5 weeks ago, # ^ |   0 You can try this problem.
•  » » » 5 weeks ago, # ^ |   0
•  » » 5 weeks ago, # ^ |   0 I don't know, in Russia it is very popular and we call it "scanning line" or "scanline".
•  » » » 5 weeks ago, # ^ |   +14 Hm yea I think it's also called sweep line algorithm? At least that's what is chapter 30 of the cses book.
•  » » » » 5 weeks ago, # ^ |   0 Yes, that's the English name.
•  » » » » » 2 weeks ago, # ^ |   0 Can you share some easy problems that make use of this algorithm?
•  » » » » » » 2 weeks ago, # ^ |   0
•  » » » » » » » 2 weeks ago, # ^ |   0 It seems like the problems are in russian
•  » » » » » » » » 2 weeks ago, # ^ |   0 You can use this service to get the text from the picture and then you can translate it with this or Google translate.https://translate.yandex.ru/
 » 5 weeks ago, # |   0 nice
 » 5 weeks ago, # | ← Rev. 2 →   +15 This trick is quite well known probably but still maybe it is useful for beginners: Ways of initializing a global array :memset(a,0,sizeof(a)) ; // initialize with 0 memset(a,-1,sizeof(a)); // initializing with -1 memset(a,0xc0,sizeof(a)); // initializing with negative infinity memset(a,0x3f,sizeof(a)); // initializing with positive infinity
•  » » 5 weeks ago, # ^ |   0 I didn't know about the negative infinity and positive infinity, I guess I will put it in the blog.
•  » » » 5 weeks ago, # ^ | ← Rev. 2 →   +3 but be careful, if you have something like  const int INF = [something];  then INF = 0x3f3f3f3f, not INF = 1e9 I prefer std::fill(), because you can just fill the array using any arbitrary values that you want, despite being slower
 » 5 weeks ago, # |   +33 For $7$, you can also do this: for start values, store $2*start$, and for end values, store $2*end+1$. If it's even, it will be a start value, else an end value. And start values will always come before end values.
•  » » 5 weeks ago, # ^ |   +3 Nice.
 » 5 weeks ago, # |   +12 I think 6 is coordinate compression
 » 5 weeks ago, # | ← Rev. 2 →   0 extended versions of trick 1 can be obtained from this a+b = a|b + a&b and a xor b = a|b — a&b ,for eg trick 1 can be obtained by subtracting eqn 2 from eqn 1.(btw sorry for not formatting it properly.)
 » 5 weeks ago, # |   +10 It would be great if everybody could share a few of the peculiar tricks they know
 » 5 weeks ago, # |   0 Can you provide links of some problems related to every tricks. It will be very helpful.
 » 5 weeks ago, # |   +4 For trick 1, you can use it on this problem.A different trick is that for queries, you can sometimes split it into 2 types of queries (when query is greater than sqrt(n) and less than sqrt(n)) so instead of using O(n) time you use O(sqrt(n)). You can use it in these problems 1 2.
•  » » 5 weeks ago, # ^ |   0 how do we solve this problem? I saw few submissions of this problem, there was dp involved. Can you explain?
 » 5 weeks ago, # |   0 Be careful using unordered_map , it can give TLE or your solution may get hacked , see the blogpost by neal about this link also unordered_map doesn't let you use complex data type like pair,int . to make it work see the blog by arpa about it link
•  » » 5 weeks ago, # ^ |   0 With custom hash, you can configure it so that it allows unordered_map, int> mp; 
 » 5 weeks ago, # |   0 Time to add the blog to favorite and read it after eternity.
 » 5 weeks ago, # |   0 Any problems related to 7?
•  » » 5 weeks ago, # ^ |   +5
•  » » » 5 weeks ago, # ^ |   0 problem G is for rule 7 , thanks a lot bro for giving the sample questions
•  » » 5 weeks ago, # ^ |   0 I think there are two problems in the CSES problem set in the searching and sorting section.
 » 5 weeks ago, # |   +10 Can we make this blog like every coder shares their one trick which is not mentioned in the post. It will be a great learning experience for all of us
•  » » 5 weeks ago, # ^ |   0 Yes, If it is nice I will add it.
 » 5 weeks ago, # |   -33 LOL stop treating cp as a game of remembering tricks...
•  » » 5 weeks ago, # ^ |   +10 LOL Read the heading... This blog is for personal use. Everyone has a way of doing things.
•  » » » 5 weeks ago, # ^ |   -25 LOL
•  » » 5 weeks ago, # ^ |   +16 That’s what practice is. See https://codeforces.com/blog/entry/82893?#comment-700332
•  » » » 5 weeks ago, # ^ |   -6 HAHA i think you misinterpreted what that comment was about
•  » » » » 5 weeks ago, # ^ |   +10 No, I don’t think I did. But I’ll also stop feeding the troll behind the fake account. (:
•  » » » 5 weeks ago, # ^ |   -13 Hm I'm curious, how do you deal with adhoc problems then?
 » 5 weeks ago, # |   0 For trick #8, I usually loop up to 200 times just to be safe. :P
•  » » 5 weeks ago, # ^ |   0 I heard this somewhere that, if we wanted to find your name in the sorted list of names of people around the globe, it would take no more than 64 operations/iterations for binary search to figure it out. I think that 100 times is more than safe unless the epislon is really small like -1e18 or smth.
•  » » » 5 weeks ago, # ^ |   -8 That statistic is probably right because $2^{64} > 7 \texttt{ billion}$. I think that binary search would take at most $33 - 34$ iterations because $log_2{7000000000} \sim 33$
 » 5 weeks ago, # |   0 can you explain 5 one, i mean what is the operator being used?
•  » » 5 weeks ago, # ^ |   0 Do you mean floor and ceil operator? or anything else?
•  » » » 5 weeks ago, # ^ |   0 oh ok got it
•  » » » 5 weeks ago, # ^ |   0 Hey,when i open my profile it is showing that this page is temporarily blocked by the administrator,do you know anything related to it.
•  » » » » 5 weeks ago, # ^ |   0 Yeah, there were some problems in rating changes in the latest contest. That's why.
•  » » » » » 5 weeks ago, # ^ |   0 oh ok thanks
 » 5 weeks ago, # |   +1 Oh, man!! Point-8 is the best I was getting TLE for some weird reason but this trick got me accepted.
•  » » 5 weeks ago, # ^ |   +2 With doubles, you should do while(abs(l - r) > EPS) instead of while (l < r).
•  » » » 5 weeks ago, # ^ |   0 How small should you set EPS?
•  » » » » 5 weeks ago, # ^ |   0 I usually set it as 1e-8 sometimes 1e-9.
•  » » » 5 weeks ago, # ^ |   0 This approach also works but I think you should go with 100 iterations it's just safe.
 » 5 weeks ago, # |   +3 Hey bro @nubir345 can u please give the question according to the trick just below each trick , would be very easy for others , the questions given below in comments .
 » 5 weeks ago, # |   +16 When I started with CP, I struggled with Binary search a lot. Nowadays I use these rules-of-thumb for BS.Say we are doing BS on some variable x. And There is some function bool check(int x) which is monotonic.And check returns $[False, False, ..., False, True, ..., True]$ for different values of x and 0 <= x <= 1e9 Codebool check(int x){ } int low = 0, high = 1e9; //Here check(low) should be False and check(high) should be True while(high-low>1){ int mid = (l+r)/2; if(check(mid))high = mid; else low = mid; } // Now high will be the first value of x which check(x) is True 
•  » » 5 weeks ago, # ^ |   +10 Check how Errichto Link makes use of an extra variable in BS to store the answer. I think that makes it further easy and avoid corner case mistakes.
 » 5 weeks ago, # | ← Rev. 2 →   +48 Also, if you know any tricks / methods that you want to share and are not in the blog then you can write in the comment section, I will add them to the blog. I'm sure there have been tutorials etc about this trick but since people are talking about binary search, I want to add this. I like to use iterative binary search that looks like this: int ans = 0; for (int k = 1 << MAX_LG; k != 0; k /= 2) { if (!has_some_property(ans + k)) { ans += k; } } This assumes 0 doesn't have "some property". In the end, ans will be the largest integer that doesn't have "some property".Using this, I have been able to avoid guessing about one-off errors for 6 years already. It is short to write, intuitive and generalizes well to floats and bigints. I'm not sure exactly what your "trick 8" accomplishes, but I suspect iterative binary search also makes that unnecessary.
•  » » 5 weeks ago, # ^ |   +11 Isn't this Binary Lifting?
•  » » » 5 weeks ago, # ^ |   0 I've only ever heard the term "binary lifting" used on trees (problems like "find the $k$-th ancestor" or LCA). But yes, both of those are essentially binary search.
•  » » » » 5 weeks ago, # ^ |   +31 I don't agree with this. You can do binary jumps instead of binary search but I wouldn't call them the same thing.
•  » » 5 weeks ago, # ^ |   0 how does this works in case of floats?
•  » » » 5 weeks ago, # ^ |   +2 for (double k = MAX_N; k > EPS; k /= 2)
•  » » 2 weeks ago, # ^ |   0 hi, could you explain how we can use this binary search solve a problem like, "What is the smallest number whose square is strictly larger that a given number N?" Thanks.
•  » » » 2 weeks ago, # ^ |   0 Find the largest one that isn't and add 1.
•  » » » » 2 weeks ago, # ^ |   0 Since you have been using this for 6 years, have you never run into a binary search problem(not insanely hard) where doing some sort of easy trick(like the +1 thing here) is not applicable? I'm asking because I really like this approach but am kinda scared that if the question is complex enough, i will simply not be able to find the trick so that i can apply this method(obviously in those scenarios going back to the original implementation is always an option).
•  » » » » » 2 weeks ago, # ^ |   +1 Obviously all kinds of things can happen in insanely hard problems. But no, this can (almost?) always be used in place of binary search. The way I see it, there are 4 use cases for binary search: find the largest number with property $P$; find the smallest number with property $P$; find the largest number without property $P$; find the smallest number without property $P$ and all of these can be handled by this implementation.
 » 5 weeks ago, # |   +41 How to get the inversion of just one number? long long get_inv(long long x) { if (x <= 1) return 1; return (p - p / x) * get_inv(p % x) % p; } 
•  » » 5 weeks ago, # ^ |   +8 By having memorization, it can be linear $O(n)$ to get all inversion in $[1 \dots n]$ (with high constant for modulo ofcourse)But if just need to find inversion of one number without using power function, what will be the complexity of the code ? I testest for one thousand random integer number (approximately $10^9$) and I think it is about $O(2 * log(n))$ am I correct ?
•  » » » 5 weeks ago, # ^ | ← Rev. 2 →   +13 Open problem. But it's really fast!
•  » » 5 weeks ago, # ^ |   +3 What is the difference between your method and the one people usually use (using binary exponentiation)?
•  » » » 5 weeks ago, # ^ |   0 Faster and easier (maybe :) ).
•  » » » » 5 weeks ago, # ^ |   0 I found this on cp-algorithms. Apparently, it can only be used if x is less than p.
•  » » » » » 5 weeks ago, # ^ |   +3 inv(x) = inv(x % p)
•  » » » » » » 5 weeks ago, # ^ |   0 Thanks for clarifying, I've just known about that. By the way, there is a slight difference between your code and cp-algorithms' code, your code computes the p - p / x first, before multiplying the result with get_inv(p % x); while cp-algorithms' code computes p / x * get_inv(p % x) % p first, then subtracting the result from p. How can both of them produce the same result?
•  » » » » » » » 5 weeks ago, # ^ | ← Rev. 2 →   +16 (p - a) * b % p = (p * b - a * b) % p = (-a * b) % p = p - a * b % p
•  » » » 5 weeks ago, # ^ | ← Rev. 2 →   +3 If you add memorization to that function, in theory to say the complexity by this way will be Linear (with high constant for taking modulo), while using binary exponentiation is $O(n log n)$ (with lighter constant)
 » 5 weeks ago, # |   +3 For trick 1, you can add this great problem.
 » 5 weeks ago, # |   +24 Given a strictly increasing array $a_1 < a_2 < a_3 < .... < a_n$Do this transformation $a_i := a_i - i$Then the array becomes non- decreasing $a_1 <= a_2 <= a_3 <= .... <= a_n$Fairly simple trick but quite useful sometimes :)
•  » » 5 weeks ago, # ^ |   0 Can you give link to any problem that use this trick?
•  » » » 5 weeks ago, # ^ |   0
•  » » 5 weeks ago, # ^ |   0 perhaps i didn't understood this ,but is this applicable to every strictly increasing array? can you show some examples.
•  » » » 5 weeks ago, # ^ |   0 $1, 2, 3, 5, 7$ becomes $0, 0, 0, 1, 2$
•  » » » » 5 weeks ago, # ^ |   0 what about 2 5 9 23 ?
•  » » » » » 5 weeks ago, # ^ |   0 Strictly increasing is a subset of non-decreasing.
•  » » » » » » 5 weeks ago, # ^ |   -16 ok , i thought that it will make the array non decreasing surely, it can make an array non decreasing but we cant be sure.(non decreasing in the sense that some element are equal)
•  » » » » » » 5 weeks ago, # ^ | ← Rev. 2 →   0 [Deleted]
•  » » » 5 weeks ago, # ^ |   0 cause if a
 » 5 weeks ago, # |   0 Guys I'm not getting the point 8. Can anyone help me with that?
•  » » 5 weeks ago, # ^ | ← Rev. 2 →   0 Sometimes precision errors lead to weird behaviour in the while (left <= right) loop in binary search.
 » 5 weeks ago, # |   0 Can anyone please help me with Problem 2 in 1st point? Thank you!
 » 5 weeks ago, # |   0 please keep updating this blog it is very helpful
 » 5 weeks ago, # |   +5 for $2$ and $3$, every number up to ${10}^{12}$ accepts a goldbach decomposition with one of the numbers less than or equal to ${10}^6$ (I'm not sure if the upper limit is ${10}^5$).
 » 5 weeks ago, # |   +1 Problems for 9) 1354D - Multiset 1404C - Fixed Point Removal
 » 5 weeks ago, # |   +3 The second problem in #3 is not related to the Goldbach conjecture.They use that every even positive integer is a difference of two primes.If you have any implication between these two conjectures, it would be a big breakthrough in number theory. See e.g. this paper.
 » 5 weeks ago, # |   +3 Can anyone help me with problem 1 of point 1 — Xor sum on Atcoder. I failed to solve it but could not find any English editorial for the problem. I found a recursive formula online $f(n) = f(n/2)+f((n-1)/2) + f((n-2)/2)$However, the explanation is in Chinese and GG translate only makes things messier.
•  » » 5 weeks ago, # ^ | ← Rev. 2 →   +4 For each u,v<=n,a xor b=u,a+b=v,consider that if a,b are both even number,then a xor b=u/2,a/2+b/2=v/2,and u/2,v/2<=n/2,so there is f(n/2).If a is even and b is odd(or b is even and a is odd),then a/2 xor (b-1)/2=(u-1)/2,a/2+(b-1)/2=(v-1)/2,so there is f((n-1)/2).For the same reason,if a,b are both odd,then (a-1)/2 xor (b-1)/2=u/2,(a-1)/2+(b-1)/2=(v-2)/2,u/2<=(v-2)/2(because the reason of xor is always no more than sum)<=(n-2)/2,so there is f((n-2)/2).That's why f(n)=f(n/2)+f((n-1)/2)+f((n-2)/2).(It's fun that i'm also chinese and i hate messy GG translate too.However,i have to use GG translate to write this explanation lol)
•  » » » 5 weeks ago, # ^ |   +3 And we can also prove these 3 cases are mutually exclusive(i mean that,each u,v is exactly in one case):For each u,v(easy to see they are both even or both odd),if u,v are both odd,then it's in case2(an odd and an even in a,b).And for the case u,v are both even,if (v-u)/2 is even,means that a+b dont produce a carry on the last bit,then it's in case1(a,b are both even).For the same reason,if (v-u)/2 is odd,means that a+b produce a carry on the last bit,it's in case3(a,b are both odd).
•  » » » » 5 weeks ago, # ^ |   0 It really helped thanks a lot
•  » » » 5 weeks ago, # ^ |   0 Thanks a lot.
 » 5 weeks ago, # |   0 (Problem specific trick) If x%a < (2*x)%a then x < a <= 2*x Otherwise x < 2*x < a . Problem Link : https://codeforces.com/contest/1104/problem/D
 » 5 weeks ago, # |   +13 lcm(a, gcd(b, c)) = gcd(lcm(a, b), lcm(a, c)) -- (1) gcd(a, lcm(b, c)) = lcm(gcd(a, b), gcd(a, c)) -- (2) https://codeforces.com/contest/1350/problem/C
 » 5 weeks ago, # |   0 How did you use method 7 in the problem 1? I used the method of difference array and prefix sum.
•  » » 5 weeks ago, # ^ |   0
 » 2 weeks ago, # |   0 Wow, this blog is quite useful for me. Upvoted :)