Substitution Sequence Range Counts
Time limit2sMemory limit128 MB
Count occurrences of 1, 2, 3 in a fixed interval of a sequence generated by simultaneous ternary substitution after N steps, without building the whole sequence.
- Level
Medium6 of 10
- Topics
- Recursion, Divide and conquer, Math, String
- Solved
- No attempts yet
Problem
At first, the sequence consists of a single number. Every second, all numbers in the sequence are replaced simultaneously by a sequence of length 3 according to these rules.
1becomes132.2becomes211.3becomes232.
You are given the initial number, an integer N, and 0-indexed positions Left and Right. After N seconds, count how many 1s, 2s, and 3s appear from position Left through position Right, inclusive.
Input
The first line contains the initial number. It is one of 1, 2, and 3.
The second line contains Left, the third line contains Right, and the fourth line contains N.
Output
Print three space-separated integers: the number of 1s, the number of 2s, and the number of 3s in the requested interval.
Constraints
- The initial number is one of
1,2, and3. 0 ≤ N ≤ 200 ≤ Left ≤ Right ≤ 3^N - 1- Both
LeftandRightare less than2,147,483,647.