hdu1757

链接:点击打开链接

题意: 当x<10时f(x) = x.当x>=10,f(x)=a0*f(x-1)+a1*f(x-2)+a2*f(x-3)+ …… +a9*f(x-10),求f(k)%m

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
struct node{
    long long m[10][10];
};
long long m;
node P={0,0,0,0,0,0,0,0,0,0,
        1,0,0,0,0,0,0,0,0,0,
        0,1,0,0,0,0,0,0,0,0,
        0,0,1,0,0,0,0,0,0,0,
        0,0,0,1,0,0,0,0,0,0,
        0,0,0,0,1,0,0,0,0,0,
        0,0,0,0,0,1,0,0,0,0,
        0,0,0,0,0,0,1,0,0,0,
        0,0,0,0,0,0,0,1,0,0,
        0,0,0,0,0,0,0,0,1,0};               //根据表达式构造出矩阵
node I={1,0,0,0,0,0,0,0,0,0,
        0,1,0,0,0,0,0,0,0,0,
        0,0,1,0,0,0,0,0,0,0,
        0,0,0,1,0,0,0,0,0,0,
        0,0,0,0,1,0,0,0,0,0,
        0,0,0,0,0,1,0,0,0,0,
        0,0,0,0,0,0,1,0,0,0,
        0,0,0,0,0,0,0,1,0,0,
        0,0,0,0,0,0,0,0,1,0,
        0,0,0,0,0,0,0,0,0,1,};
node mul(node a,node b){
    int i,j,k;
    node c;
    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]+=(a.m[i][k]*b.m[k][j])%m;
        c.m[i][j]%=m;
    }
    return c;
}
node quickmod(long long n){
    node a,b;
    a=P;b=I;
    while(n){
        if(n&1)
        b=mul(b,a);
        n/=2;
        a=mul(a,a);
    }
    return b;
}                                           //矩阵快速幂模板
long long a[20];
int main(){
    long long k,i,sum;
    node temp;
    while(cin>>k>>m){
        for(i=0;i<10;i++)
        cin>>a[i];
        if(k<10){
            cout<<k%m<<endl;
            continue;
        }                                   //小于10的时候单独判断
        for(i=0;i<10;i++)
        P.m[0][i]=a[i];
        temp=quickmod(k-9);
        sum=0;
        for(i=0;i<10;i++)
        sum+=(temp.m[0][i]*(9-i))%m;
        sum%=m;
        cout<<sum<<endl;
    }
    return 0;
}



你可能感兴趣的:(hdu1757)