A Simple Math Problem
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1838 Accepted Submission(s): 1053
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
linle
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 using namespace std; 6 7 const int N= 13; 8 9 struct node 10 { 11 __int64 mat[N][N]; 12 }hxl; 13 int A[13],k,m; 14 15 void make_first(node *cur) 16 { 17 int i,j; 18 for(i=1;i<=10;i++) 19 for(j=1;j<=10;j++) 20 if(i==j) 21 cur->mat[i][j]=1; 22 else cur->mat[i][j]=0; 23 } 24 25 struct node cheng(node cur,node now) 26 { 27 node ww; 28 int i,j,k; 29 memset(ww.mat,0,sizeof(ww.mat)); 30 for(i=1;i<=10;i++) 31 for(k=1;k<=10;k++) 32 if(cur.mat[i][k]) 33 { 34 for(j=1;j<=10;j++) 35 if(now.mat[k][j]) 36 { 37 ww.mat[i][j]+=cur.mat[i][k]*now.mat[k][j]; 38 if(ww.mat[i][j]>=m) 39 ww.mat[i][j]%=m; 40 } 41 } 42 return ww; 43 } 44 45 void power_sum2(int n) 46 { 47 __int64 sum=0; 48 int i; 49 node cur,now=hxl; 50 make_first(&cur); 51 while(n) 52 { 53 if(n&1) 54 { 55 cur=cheng(cur,now); 56 } 57 n=n>>1; 58 now=cheng(now,now); 59 } 60 for(i=1;i<=10;i++) 61 sum= (sum+(10-i)*cur.mat[1][i])%m; 62 printf("%I64d ",sum); 63 64 } 65 66 void make_ini() 67 { 68 int i,j; 69 if(k<=9) 70 { 71 printf("%d ",k); 72 return; 73 } 74 memset(hxl.mat,0,sizeof(hxl.mat)); 75 for(i=1;i<=10;i++) 76 hxl.mat[1][i]=A[i]; 77 78 for(i=2;i<=10;i++)// 初始化 79 for(j=1;j<=10;j++) 80 if(i-j==1) 81 hxl.mat[i][j]=1; 82 83 power_sum2(k-9); 84 } 85 86 int main() 87 { 88 int i; 89 while(scanf("%d%d",&k,&m)>0) 90 { 91 for(i=1;i<=10;i++) 92 scanf("%d",&A[i]); 93 make_ini(); 94 // cs(); 95 } 96 return 0; 97 }
Source
Recommend
lcy