Блог пользователя nubir345

Автор nubir345, 6 лет назад, По-английски

This blog is for personal use. You can check it out if you want.

As many people are checking out this blog, I thought of adding problems for some of the tricks.

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.
  11. $$$gcd(F_n,F_m)=F_{gcd(n,m)}$$$, where $$$F_x$$$ is the $$$x_{th}$$$ fibonacci numbers and first two terms are $$$0,1$$$. Problem 1
  12. In ternary search, always keep in mind that when $$$r=l+2$$$ then $$$mid1=l$$$ and $$$mid2=r$$$ or we can say that $$$l$$$ & $$$r$$$ will remain same $$$i.e.$$$ $$$r=l+2$$$ and the condition $$$l+1$$$ will never get checked, so you have to check it explicitly. see this problem

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.

  • Проголосовать: нравится
  • +472
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +319 Проголосовать: не нравится

If you know the proof of #3, please tell me.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Trick 7 is my personal favourite

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

nice

»
6 лет назад, скрыть # |
← 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

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +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.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +12 Проголосовать: не нравится

I think 6 is coordinate compression

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

It would be great if everybody could share a few of the peculiar tricks they know

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can you provide links of some problems related to every tricks. It will be very helpful.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +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.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 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,int>,int . to make it work see the blog by arpa about it link

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Time to add the blog to favorite and read it after eternity.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Any problems related to 7?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +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

»
6 лет назад, скрыть # |
 
Проголосовать: нравится -33 Проголосовать: не нравится

LOL stop treating cp as a game of remembering tricks...

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For trick #8, I usually loop up to 200 times just to be safe. :P

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can you explain 5 one, i mean what is the operator being used?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Oh, man!! Point-8 is the best I was getting TLE for some weird reason but this trick got me accepted.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +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 .

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +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

Code
»
6 лет назад, скрыть # |
← 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.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +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;
}
»
6 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

For trick 1, you can add this great problem.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +24 Проголосовать: не нравится

Given a strictly increasing array $$$a_1 \lt a_2 \lt a_3 \lt .... \lt a_n$$$
Do this transformation $$$a_i := a_i - i$$$
Then the array becomes non- decreasing $$$a_1 \lt = a_2 \lt = a_3 \lt = .... \lt = a_n$$$
Fairly simple trick but quite useful sometimes :)

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Guys I'm not getting the point 8. Can anyone help me with that?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

please keep updating this blog it is very helpful

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +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$$$).

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +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.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +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.

  • »
    »
    6 лет назад, скрыть # ^ |
    ← 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)

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится +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).

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      Thanks a lot.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +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.me/contest/1350/problem/C

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Wow, this blog is quite useful for me. Upvoted :)

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +17 Проголосовать: не нравится

$$$F[gcd(x,y)]=gcd(F[x],F[y])$$$

$$$F$$$ is the Fibonacci sequence that $$$F(0)=0$$$, $$$F(1)=1$$$, $$$F(n)=F(n-1)+F(n-2)$$$ for $$$n \ge 2 $$$

»
6 лет назад, скрыть # |
← Rev. 5  
Проголосовать: нравится +1 Проголосовать: не нравится

You can also add one more property.

gcd ( x , y ) = gcd ( x-y , y );

This results in, gcd ( x , y , z , w ,.....) = gcd ( x-y , y , z , w,....)

Also,

for any a, b, if b > a, a%b = a. Additionally, if a ≥ b > [a/2] , a%b = a - b.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For Trick # 2 you can add this Problem

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Using point 2 can we state that for checking if n is prime we just need to check if n is not divisible by any number from 2 to 300 rather than sqrt(n) if n<=1e12?

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Here I got one trick , If n>1, then there is always at least one odd prime number x satisfying n < x < 2n

You can check this problem 1178D - Prime Graph

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

This has been very helpful, thank you so much!

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Another recent problem that can be solved using trick 1: 1556D - Take a Guess