Under construction

Still being built. Things may change

Fibonacci Suffix Array

Time limit5sMemory limit512 MB

Summary
Given n, q, and m, the task finds the suffix array of the Fibonacci word fib_n at q query positions and prints each value modulo m.
Level

Hard9 of 10

Topics
String, Recursion, Math
Solved
No attempts yet

Problem

The Fibonacci words are defined as follows: fib_0=b,fib_1=a,fib_n=fib_n−1fib_n−2 for n≥2fib\_0=b, fib\_1=a, fib\_n=fib\_{n-1}fib\_{n-2} \text{ for } n \ge 2. fib_nfib\_n is the concatenation of fib_n−1fib\_{n-1} and fib_n−2fib\_{n-2}.

The first few Fibonacci words are bb, aa, abab, abaaba, abaababaab, abaababaabaababa, abaababaabaababaababaabaab, …\dots

A suffix array for a string ss of length nn is a permutation sasa of the integers from 11 to nn such that s\[sa_1..n],s\[sa_2..n],…,s\[sa_n..n]s\[sa\_1.. n], s\[sa\_2..n], \dots, s\[sa\_n..n] is the list of all non-empty suffixes of ss in lexicographical order.

Let sasa be the suffix array of fib_nfib\_n. Compute (sa_p_1 mod m),(sa_p_2 mod m),…,(sa_p_q mod m)(sa\_{p\_1} \bmod m), (sa\_{p\_2} \bmod m), \dots, (sa\_{p\_q} \bmod m).

Input

The input contains several test cases and ends at end-of-file. Each test case has the following format.

The first line contains three integers nn, qq, and mm.

The second line contains qq integers p_1,p_2,…,p_qp\_1, p\_2, \dots, p\_q.

Output

For each test case, print qq values (sa_p_1 mod m),(sa_p_2 mod m),…,(sa_p_q mod m)(sa\_{p\_1} \bmod m), (sa\_{p\_2} \bmod m), \dots, (sa\_{p\_q} \bmod m) separated by spaces.

Constraints

  • 1≤n≤10181 \le n \le 10^{18}
  • 1≤q≤1051 \le q \le 10^5
  • 1≤m≤2×1091 \le m \le 2 \times 10^9
  • 1≤p_i≤min⁡(1018,∣fib_n∣)1 \le p\_i \le \min(10^{18}, |fib\_n|)
  • The sum of qq over all test cases does not exceed 10610^6.

Examples1

  1. Example 1

    Input
    1 1 10
    1
    2 2 10
    1 2
    3 3 10
    1 2 3
    4 5 10
    1 2 3 4 5
    5 8 10
    1 2 3 4 5 6 7 8
    
    Expected output
    1
    1 2
    3 1 2
    3 4 1 5 2
    8 3 6 1 4 7 2 5