Ashwanth.K's blog

By Ashwanth.K, history, 17 months ago, In English

I will account for good observations and ideas while solving problems in codeforces/CodeChef/atcoder . The proofs of the below statements will not be mentioned here; It's advised to do such proofs on your own for exercise.

  • Lets say I have a set $$$S$$$ consisting of integers, denote its $$$lcm(S) = L$$$, I add a new element $$$x$$$ to this set $$$S$$$ , Lets deonte the new set as $$$S'$$$,where $$$S' = union(S , x)$$$ and its $$$lcm(S') = L'$$$. Can we deduce a relation between $$$L$$$ and $$$L'$$$? We can observe $$$L = L'$$$ or $$$L' >= 2*L$$$.

  • We want to find two numbers in an array $$$A[]$$$ with maximum common prefix bits in binary representation. It's easy to show that those two numbers always occur as adjacent numbers in $$$sorted(A[])$$$
  • The number of distinct gcd prefixed/suffixed at an index in an array will never exceed $$$log(A_{max})$$$

  • Let's say I have a number $$$X$$$, And I apply modulo operation as many times as I wish, i.e $$$X = X \% {m_i}$$$ for some different values of $$${m_i}$$$. It can be shown that $$$X$$$ takes $$$log(X)$$$ distinct values until it reaches to $$$0$$$.

  • If $$$N$$$ times $$$abs()$$$ function appears at any problem, maybe bruteforcing all $$$2^N$$$ combinations of $$$+/-$$$ may give way to the solution sometimes. Especially when we need to compute a function of form $$$max(abs(...))$$$

  • Prefix Bitwise Or/And can take a maximum of $$$log(max(A[i]))$$$ values.
  • Nested totient function say $$$phi(phi(phi( ... (X) ... )))$$$ will eventually reach 1 in atmost $$$2log(X)$$$ nested functions. Useful for computing expressions like $$$(A^{(B^{(C^..)})})$$$ modulo $$$P$$$. (nested powers).
  • SOS dp may help to compute the number of $$$i$$$ such that $$$A[i]$$$ is a subset/superset/no bits common to a given mask $$$X$$$
  • Partial optimisation of SOS dp leading to $$$3^N$$$ complexity may pass for $$$N <=15$$$.
  • Whenever You want to maximize/minimize bitwise properties among some elements, consider iterating from the last bit and checking its possibility. This greedy assigning from the last bit will work.

  • Any counting problem, like counting pairs of elements/counting subarrays satisfying some property: If any common technique, like fixing the L pointer or 2pointer approach, does not work, try to divide and conquer. It may be easy to come up with a solution sometimes. https://codeforces.net/contest/1849/problem/E
  • The contribution Technique impresses me every time. Try this: https://atcoder.jp/contests/abc312/tasks/abc312_g. Hint: number of ways of choosing three nodes in the same tree path can be converted to summating all path lengths in the tree.

  • General Technique: For counting problems: try to fix some parameters and iterate on it. It may happen that fixing just one parameter may be difficult sometimes to count properly. So try to do casework and identify which cases can be easily calculated by fixing different parameters. Eg, let's say my problem can be divided into 2 cases, $$$case1$$$ and $$$case2$$$. It can happen that fixing parameter $$$A$$$ can be easy to count distinct answers for $$$case1$$$, and fixing another parameter $$$B$$$ can be easy to count distinct answers for $$$case2$$$. Try this: https://codeforces.net/contest/50/problem/E Hint: case1: rational, case2: irrational roots.

  • It's not wrong to complicate ur solution with a lazy segtree. Try this: https://codeforces.net/contest/1553/problem/F
  • Lets take an array $$$A$$$ and let $$$f(x)$$$ be number of subsequence with $$$Xor(subseq) = x$$$. It can be shown that $$$f(x)$$$ is always a power of 2. Strong result: f(x) = 0 or f(x) = f(x1) = f(x2) ...!= 0. More insights on this can be understood well by studying the xor basis technique.

  • $$$O(N^2)$$$ may give tle, but $$$O(N^2/64)$$$ will pass. clue: BITSETS
  • Maximum Manhattan distance between 2 points: convert every point $$$(x,y)$$$ to $$$(x',y') = (x-y,x+y)$$$. then $$$answer = max(max(x')-min(x') , max(y')-min(y')) $$$
  • If you find some observation or pattern problem, better bruteforce all possibilities to arrive at observing patterns quicker. https://codeforces.net/problemset/problem/1730/D, I tried brute-forcing all possibilities; it happened that I could figure out some common pattern everywhere, then I proceeded to put claims, and it happened that they were true.

  • Any array operation, like adding on an interval, adding $$$+x$$$ on some subsequence of the array, or any other variation, try to think its effect of operations on the prefix sum. This may give a clue to the answer. Eg, https://codeforces.net/contest/1556/problem/E

  • https://codeforces.net/problemset/problem/623/B If I remove a subarray of size < N from an array, then either the first element or the last element remains as it is...

  • $$$gcd(x,y) = gcd(x-y,y) = gcd(x,y-x)$$$ https://leetcode.com/contest/biweekly-contest-96/problems/check-if-point-is-reachable/
  • Idea: Intuitive Proof of Dijkstra problem. Consider a graph problem where I have to find the shortest distance from source to destination where all edges are undirected and weighted. Additional constraint, all costs of edges $$$ c_i = W $$$ where W is a positive integer. This modified problem with all equal weights can be solved with simple BFS.
  • Now consider the real problem of Dijkstra, where I have varying edge weights. I can add dummy nodes at the intermediate of those edges to make all edge weights equal. Example: If $$$w_i$$$ is the weight of the edge between $$$u$$$ and $$$v$$$.

  • Then I can add $$$w_i-1$$$ dummy nodes equidistant from $$$u$$$ and $$$v$$$ to make all edges cost $$$1$$$. Now, our problem has a BFS solution with complexity $$$sum(w_i)$$$. But I am concerned only with the original nodes' distance values in the graph.
  • I need not calculate my dummy node's distance. So we can fast-forward this BFS to calculate the distance of only the original nodes. This fast-forwarded version of BFS is simply DIJKSTRA.

  • Lets say I want to factorize $$$N$$$ , precompute all primes from 1 to $$$sqrt(N)$$$ and check for each prime. Complexity = $$$O(sqrt(N)/log(sqrt(N)))$$$ which is just $$$3500$$$ for $$$10^9$$$

  • Lets say U want to compute shortest distances on a graph with many edges (n^2) this would give tle ... but we could seek out some way to reduce the count of edges. lets say node u is conneted to all nodes (L...R) , then we can represent a segtree of these nodes and add edge from node u to only logn intervals. (L...R) can be covered by atmost 2logN intervals . So we can add edge from u to these intervals. Also we can edges from every node to its children in segtree with 0 cost. Running dijkstra on this segtree would solve the problem. https://codeforces.net/contest/786/problem/B practice problem.

  • Inclusion exclusion on masks: it's just enough to change + to — in SOS DP, everything is taken care.

  • Vote: I like it
  • +228
  • Vote: I do not like it

| Write comment?
»
17 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ASHWANTH_K (previous revision, new revision, compare).

»
17 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ASHWANTH_K (previous revision, new revision, compare).

»
17 months ago, # |
  Vote: I like it +4 Vote: I do not like it

Awesome content brother.

»
17 months ago, # |
  Vote: I like it +5 Vote: I do not like it

Can you also share the problems, where you made these observations?

  • »
    »
    17 months ago, # ^ |
    Rev. 2   Vote: I like it +1 Vote: I do not like it

    Problem for the 5th observation. (bruteforcing abs)

    Problem for the 7th observation. (converging phi)

    • »
      »
      »
      17 months ago, # ^ |
      Rev. 2   Vote: I like it +3 Vote: I do not like it

      Thanks alot, I was actually curious for the 5th one in particular

      Edit: Can't see the problem for the 5th one. Do I need to register for that? If yes, then can you please guide me how

    • »
      »
      »
      17 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I don't understand how 5th observation bruteforcing abs will help in that problem. Won't the complexity still be $$$N2^M$$$? Maybe I'm not understanding the technique properly.

      • »
        »
        »
        »
        17 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I don't see why the $$$N2^M$$$ operations could be the problem. My solution runs in $$$O(NM2^M)$$$ which to my surprise passes with full score.

  • »
    »
    17 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Also the third observation is used in this problem (COCI 2022-2023).

»
17 months ago, # |
  Vote: I like it +59 Vote: I do not like it

These are actually good! I would like to add a number of comments.

  • It is true that for $$$S\subset S'$$$ one has $$$\mathrm{lcm}(S') = \mathrm{lcm}(S)$$$ or $$$\mathrm{lcm}(S')\geq 2\mathrm{lcm}(S)$$$. But it is also true that $$$\mathrm{gcd}(S) = \mathrm{gcd}(S')$$$ or $$$\mathrm{lcm}(S)\geq 2\mathrm{gcd}(S')$$$. The reason is the same: either the union/intersection of the multisets of primes are the same, or they differ by at least one prime which is at least 2.

  • About the number of distinct prefix gcds: sometimes it is useful, for all left borders l, to consider all values of $$$\mathrm{gcd}(a_l, \ldots, a_r)$$$ (there are at most $$$\log{n}$$$ of them) and where they change. There is a technique of doing this in $$$n\log{n}$$$ time. For this one can move $$$l$$$ from right to left and maintain the stack (list, vector -- doesn't really matter because it has logarithmic size anyway) of all points of change as well as the values.

  • $$$x\mapsto x\mod{m_i}$$$ takes $$$\log$$$ different values, because each time it either doesn't change, or decreases at least twice (similarly to prefix gcd-s).

  • About $$$n$$$ abs-es, if it was not clear: one use case could be anything with the manhattan distance, for example. For example, if we have two 3d-points $$$(x_1, y_1, z_1)$$$ and $$$(x_2, y_2, z_2)$$$, then the manhattan distance between them doesn't exceed $$$d$$$ iff all $$$\pm(x_1 - x_2) \pm (y_1 - y_2) \pm (z_1 - z_2)$$$ don't exceed $$$d$$$.

  • I believe a sequence of $$$\varphi(n)$$$ takes at most $$$\log{n} + 1$$$ steps to come to $$$1$$$, because $$$\varphi$$$ of even number is at least twice smaller, and $$$\varphi$$$ of any number greater than two is even.

  • I remember a problem where $$$3^n$$$ passed for $$$n \leq 17$$$. I wouldn't be surprised if it would have passed even for $$$n = 18$$$, although it depends on the type of operations. I wouldn't want to do $$$729^3$$$ modulo operations, for example.

  • The last one is probably not that good. I would say there are fewer problems where divide-and-conquer helps than where it doesn't. I would also say that usually for counting pairs it helps to split the problem into two independent problems of counting elements instead of pairs, but this is vague + I cannot tell if this is a more popular case than yours.

»
17 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ASHWANTH_K (previous revision, new revision, compare).

»
17 months ago, # |
  Vote: I like it 0 Vote: I do not like it

ASHWANTH_K I suggest maintaining these observations in a google docs spreadsheet. It will be far easier to maintain, and also readable. You can post a view-only link here if you want cf users to be able to see it.

»
17 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ASHWANTH_K (previous revision, new revision, compare).

»
17 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ASHWANTH_K (previous revision, new revision, compare).

»
17 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Just a little comment on observation 2:

Feel free to reflect more on the relationship between this result by relating it to the bitwise trie, and then think about how it generalizes to regular tries and strings. This might make complicated things like the relationship between suffix arrays and suffix tree much easier to understand, and will often give you much easier and memory-friendly options than using tries in general.

Afterwards, feel free to analyze it in terms of general trees and how linearization (sorting the vertices in some order) lets you operate with the tree implicitly often without needing to ever build the tree explicitly. And also take a look at ‘virtual trees’ as a technique, it’s very elegant and often useful for DS problems.

»
17 months ago, # |
  Vote: I like it +18 Vote: I do not like it

Note that normally in CF you will get a measly x32 speedup for bitset. Because sizeof(unsigned long) on windows is only 32 even on 64 bit machine (on Linux with 64 bit machine it would be 64).

However, if you enable O3 and avx2 and some other bit manipulation instructions sets, you can get upto x256 speedup for some operations.

See demo here. For 256 length bitsets, x &= y is using YMMWORDs (256 bits), however x.count() is using popcnt on 4 QWORDs (64 bits) for some reason.

On some online judges with relatively new hardware (not CF unfortunately), you can turn on avx512f and get x512 as well.

»
17 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Another cute task concerning phi trick: 1797E - Li Hua and Array

»
17 months ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

I'm sorry if I'm wrong but in the second observation the right observation is suffix bits not prefix, right?

»
17 months ago, # |
  Vote: I like it 0 Vote: I do not like it

problem for third observation

»
17 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ASHWANTH_K (previous revision, new revision, compare).

»
17 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ASHWANTH_K (previous revision, new revision, compare).

»
17 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ASHWANTH_K (previous revision, new revision, compare).

»
16 months ago, # |
  Vote: I like it +5 Vote: I do not like it

Auto comment: topic has been updated by ASHWANTH_K (previous revision, new revision, compare).

»
16 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Problem for the 2nd observation

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ASHWANTH_K (previous revision, new revision, compare).

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ASHWANTH_K (previous revision, new revision, compare).