HDU 1757(数论,矩阵)

 

A Simple Math Problem

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 440    Accepted Submission(s): 253

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.
 

 

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.
 

 

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

此题矩阵的幂求解要用到快速求幂~ 有点类似求斐波那契数列!
#include <iostream> using namespace std; #define ll __int64 struct Mat { int martix[10][10]; }; ll n,m; Mat init,q,tp,res; Mat Mul(Mat a,Mat b) { int i,j,k; Mat c; for(i=0;i<10;i++) { for(j=0;j<10;j++) { c.martix[i][j]=0; for(k=0;k<10;k++) { c.martix[i][j]+=(a.martix[i][k]*b.martix[k][j])%m; c.martix[i][j]%=m; } } } return c; } void mat_pow(ll x) { tp=q; q=res; while(x) { if(x&1) q=Mul(q,tp); tp=Mul(tp,tp); x=x>>1; } } int main() { int i,j,sum; memset(init.martix,0,sizeof(init.martix)); init.martix[1][0]=1; init.martix[2][1]=1; init.martix[3][2]=1; init.martix[4][3]=1; init.martix[5][4]=1; init.martix[6][5]=1; init.martix[7][6]=1; init.martix[8][7]=1; init.martix[9][8]=1; for(i=0;i<10;i++) { for(j=0;j<10;j++) { if(i==j) res.martix[i][j]=1; else res.martix[i][j]=0; } } while (scanf("%I64d%I64d",&n,&m)!=EOF) { q=init; for(i=0;i<10;i++) scanf("%d",&q.martix[0][i]); if(n>=10) { sum=0; mat_pow(n-9); for(i=0;i<10;i++) sum+=q.martix[0][i]*(9-i); printf("%d/n",sum%m); } else printf("%d/n",n%m); } return 0; }

 

你可能感兴趣的:(HDU 1757(数论,矩阵))