Fibonacci Music
Time limit1sMemory limit512 MB
Build a digit string from Fibonacci numbers reduced mod M and answer queries for the N-th digit, with N up to 10^15.
- Level
Hard8 of 10
- Topics
- Math, Number theory, Prefix sum, Binary search
- Solved
- No attempts yet
Problem
Cheolsu, a composer, happened to see a piano piece built from the Fibonacci sequence on YouTube. The Fibonacci sequence satisfies , , and for all .
The piece was made by pressing the key corresponding to each digit of the Fibonacci sequence. If the piece uses the first 8 terms of the Fibonacci sequence, the keys are pressed 10 times in total, as follows.
1 → 1 → 2 → 3 → 5 → 8 → 1 → 3 → 2 → 1
Cheolsu decided to try this method himself. But the Fibonacci sequence grows so fast that it was hard for him to compute, since he is weak at addition.
So he changed the method a little. After choosing a number , he takes the remainder of each term of the Fibonacci sequence divided by , builds a new sequence from the individual digits of each number, and writes a piano piece from it.
For example, when , the new sequence is as follows.
{1, 1, 2, 3, 5, 8, 3, 1, …}
So Cheolsu presses keys in the order 1 → 1 → 2 → 3 → 5 → 8 → 3 → 1 → 4 → …
At this point, for a given , Cheolsu wondered about the number of the key pressed -th (that is, the -th term of the new sequence).
Given values of as queries, write a program that outputs the number of the key pressed -th for each query.
Input
The first line gives the integers and , separated by a space.
From the second line, lines follow, each giving the integer that represents a query.
Output
For each , output the number of the key pressed -th (the -th term of the new sequence) on one line, in the order the queries appear in the input, for a total of lines.