描述
传送门:hdu-1757
Lele now is thinking about a simple function $f(x)$.
And $a_i(0<=i<=9)$ can only be 0 or 1 .
Now, I will give $a_0 ~ a_9$ and two positive integers $k$ and $m$ ,and could you help Lele to caculate $f(k)%m$.
Input
The problem contains mutiple test cases.Please process to the end of file.
In each case, there will be two lines.
In the first line , there are two positive integers $k$ and $m$. $( k<2*10^9 , m < 10^5 )$
In the second line , there are ten integers represent $a_0 ~ a_9$.
Output
For each case, output $f(k) % m$ in one line.
Examples
intput
1
2
3
410 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0output
1
245
104
思路
- O(n)肯定会T,可以考虑矩阵快速幂求解。
- 由于题目给了递推式,所以很显然我们需要求一个$X$矩阵,使得这个矩阵满足
,易得。
将$F_{n}$按递推式展开,取对应系数即可,- 递推式是从10开始的,所以对与第$n$项,只需求$X^{n-9}$,所得矩阵再左乘一个$\begin{bmatrix}F_9 & F_8 & F_7 & F_6 & F_5 & F_4& F_3& F_2& F_1& F_0\end{bmatrix}^{-1}$,所得矩阵的第一个元素即为$F_i$
代码
1 |
|