Product Oriented Recurrence
Source: Codeforces 1182E
- Read the problem and summarise the task requirements.
- Try to keep your response to a couple of sentences.
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!
- Let gx = cxfx, and rewrite the recurrence using only terms of this new sequence.
- If you knew gx modulo 109 + 7, could you recover fx?
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!
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.
- If you knew hx, p modulo 109 + 7, could you recover gx?
- The answer is no! But if we change the question slightly, the answer becomes yes.
Solving this recurrence iteratively for each x ≤ n would take O(n) time for each prime p, but n can go up to 1018.
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.
- Analyse the time complexity of your algorithm.
- Many things to account for:
- Finding the primes.
- Note that you can’t sieve all the way to 109!
- Instead find those that are relevant to each test case.
- How many relevant primes could there be?
- Finding the base cases for each prime.
- Finding the nth term of each h⋅,p recurrence.
- Recovering gn.
- Recovering hn.
- Estimate the running time of your algorithm.
- Implement this algorithm in code.
- Reflector:
- What potential pitfalls can you think of?
- Write some tests to check for these pitfalls.
- Other group members:
- Write a program which solves the problem.
- Subroutines and unit tests are particularly highly recommended due to the modular nature of the algorithm.
- Run your tests and debug if necessary.
- Submit your output for judging!