HDU 1757 A Simple Math Problem

/*matrix a={ 

{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},

{1,1,1,1,1,1,1,1,1,1}, 

}; */

#include <cstdio>

#include <iostream>

#define maxn 15

#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

struct matrix{

    int v[maxn][maxn];

    void init(){memset(v,0,sizeof v);}

}a,b;

matrix mul(matrix a,matrix b,int l,int m,int n,int mod){

    matrix c; c.init();

    rep(i,l)rep(j,m)rep(k,n)c.v[i][j]=(c.v[i][j]+(a.v[i][k]*b.v[k][j])%mod)%mod;

    return c;

}

matrix power(matrix a,int l,int m,int n,int x,int mod){

    if(x==1)return a;

    matrix tmp=power(a,l,m,n,x>>1,mod);

    tmp=mul(tmp,tmp,l,m,n,mod);

    if(x&1)tmp=mul(tmp,a,l,m,n,mod);

    return tmp;

}

int main(){

    int m,n;

    while(~scanf("%d%d",&n,&m)){

        a.init();

        for(int i=0;i<=8;i++)a.v[i][i+1]=1;

        for(int i=9;~i;i--)scanf("%d",&a.v[9][i]);

        if(n<=9){printf("%d\n",n%m);continue;}

        for(int i=0;i<=9;i++)b.v[i][0]=i;

        a=power(a,10,10,10,n-9,m);

        b=mul(a,b,10,1,10,m);

        printf("%d\n",b.v[9][0]);

    }

    return 0;

}

你可能感兴趣的:(simple)