Product Oriented Recurrence

Source: Codeforces 1182E

  1. Read the problem and summarise the task requirements.

The  − 6 in the right-hand side is suspicious. It seems to not be a coincidence that the other three terms include  − 1,  − 2 and  − 3!

  1. Let gx = cxfx, and rewrite the recurrence using only terms of this new sequence.

We’ve solved lots of recurrences where terms are added, but here the terms are multiplied. Recall that the multiplicative structure of positive integers is fully described by the Fundamental Theorem of Arithmetic!

  1. Let hx, p be the largest power of prime p in gx, and infer a recurrence for hx, p in terms of hx − 1, p, hx − 2, p and hx − 3, p.

  2. If you knew hx, p modulo 109 + 7, could you recover gx?

Solving this recurrence iteratively for each x ≤ n would take O(n) time for each prime p, but n can go up to 1018.

  1. Design an algorithm to find the nth term of the sequence h⋅,p (or at least enough to reconstruct gx from it) in o(n) (sub-linear) time.

  2. Analyse the time complexity of your algorithm.
  3. Implement this algorithm in code.
  4. Submit your output for judging!