A Simple Math Problem
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 8 Accepted Submission(s) : 7
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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.
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.
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 a0 ~ a9.
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.
Output
For each case, output f(k) % m in one line.
Sample Input
10 9999 1 1 1 1 1 1 1 1 1 1 20 500 1 0 1 0 1 0 1 0 1 0
Sample Output
45 104
Author
Source
2007省赛集训队练习赛(6)_linle专场
![生气](http://static.blog.csdn.net/xheditor/xheditor_emot/default/mad.gif)
后来仔细想想后,发觉可以构造出更好的矩阵,与是就过了
![吐舌头](http://static.blog.csdn.net/xheditor/xheditor_emot/default/tongue.gif)
{f[0],f[1],f[2],f[3],f[4],f[5],f[6],f[7],f[8],f[9]}*A={f[10],f[11],f[12],f[13],f[14],f[15],f[16],f[17],f[18],f[19]}
只要构造出10*10矩阵A再用上矩阵快速幂就可以交上AC代码鸟~
~~AC不易,且行且珍惜
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define inf 9999999
const int MAXN = 10;
int M[MAXN][MAXN];
int f[MAXN];
int a[MAXN];
int m;
struct Matrix{
int M[MAXN][MAXN];
}initm,unitm;
void initM(){//构造矩阵,这一步很重要
int i, j,k;
for (i = 0; i < 10; i++){
for (j = 0; j < 10; j++)
initm.M[i][j] = 0;
}
for (i = 0; i < 10; i++){
for (j = 0; j < 10-i;j++)
initm.M[9-j][i] = a[j+i];
}
for (i = 1; i < 10; i++){
for (k = 0; k < i;k++)
for (j = 0; j < 10; j++)
initm.M[j][i] = (initm.M[j][i]+a[k]*initm.M[j][i - k-1]%m)%m;
}
/*
for(i=0;i<10;i++){
for(j=0;j<10;j++){
printf("%d ",initm.M[i][j]);
}
printf("\n");
}*/
}
Matrix mul(Matrix a, Matrix b){ //让两个矩阵相乘
Matrix c;
int i, j,k;
for (i = 0; i<10; i++){
for (j = 0; j<10; j++){
c.M[i][j] = 0;
for (k = 0; k<10; k++){
c.M[i][j] = (c.M[i][j] + a.M[i][k] * b.M[k][j] % m) % m;
}
c.M[i][j] %= m;
}
}
return c;
}
int MatrixFastM(Matrix p,Matrix q,int k,int r){ //矩阵快速幂
int k1=k;
while (k>0){
if (k & 1){
p = mul(p, q);
}
q = mul(q, q);
k >>= 1;
}
int nm = r - 10 * k1;
/*int i,j;
printf("p.M%d\n",nm);
for(i=0;i<10;i++){
for(j=0;j<10;j++){
printf("%d ",p.M[i][j]);
}
printf("\n");
}*/
return p.M[0][nm];
}
int main()
{
int k;
int i, j;
freopen("in.txt", "r", stdin);
while (~scanf("%d%d", &k, &m)){
memset(unitm.M, 0, sizeof(unitm.M));
for (i = 0; i < 10; i++){
unitm.M[0][i] = i;
}
for (i = 0; i < 10; i++){
scanf("%d", &a[i]);
}
initM();
int res = MatrixFastM(unitm, initm, k / 10,k);
printf("%d\n", res%m);
}
return 0;
}