您的位置:首页 > 其它

hdoj 1757 A Simple Math Problem

2016-09-28 23:18 337 查看

A Simple Math Problem

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)


[align=left]Problem Description[/align]
Lele now is thinking about a simple function f(x).

If x < 10 f(x) = x.
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
And ai(0<=i<=9) can only be 0 or 1 .

Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.

[align=left]Input[/align]
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 a0 ~ a9.

[align=left]Output[/align]
For each case, output f(k) % m in one line.

[align=left]Sample Input[/align]

10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0

[align=left]Sample Output[/align]

45
104

[align=left]Author[/align]
linle
思路:裸的矩阵快速幂,首先要构造矩阵
[fn] [XXX] [fn-1]
[fn-1] = [XXX] * [fn-2]
[fn-2] [XXX] [fn-3]
…… …… ……
思考一下就可以得到

[fn] [a0 a1 a2] [fn-1]
[fn-1] = [1 0 0] * [fn-2]

[fn-2] [ 0 1 0 ] [fn-3]
…… …… ……
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: