hdu1757

1918 단어
링크:클릭하여 링크 열기
제목: 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;
}



좋은 웹페이지 즐겨찾기