(1757)HDU

#include<iostream>
#include<cstdio>
#include<string.h>
#include<cstring>
#include<string>
#include<stack>
#include<set>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>

#define LOCAL
#define ll long long
#define lll unsigned long long
#define MAX 1000009
#define eps 1e-8
#define INF 0xfffffff

using namespace std;
/*

题意:

想法:

*/
int k,mod;
int aa[10];
struct Matrix
{
    int  m[10][10];
};
Matrix mul(Matrix a,Matrix b)
{
    int i,j,k;
    Matrix 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])%mod;
            c.m[i][j] %= mod;
        }
    return c;
}

Matrix pow(Matrix a,Matrix b,int x)
{
    while (x)
    {
        if (x & 1)
            b = mul(b,a);
        x = x >> 1;
        a = mul(a,a);
    }
    return b;
}
int main()
{
    int f;
    //freopen("date.in","r",stdin);
    while(~scanf("%d%d",&k,&mod))
    {
        memset(aa,0,sizeof(aa));
        for(int i = 0;i<10;i++)
        {
            scanf("%d",&aa[i]);
        }
        //cout<<f<<endl;
        Matrix a,c;
        memset(a.m,0,sizeof(a.m));
        for(int i = 0;i<10;i++)
        a.m[0][i] = aa[i];

        a.m[1][0] = 1;
        a.m[2][1] = 1;
        a.m[3][2] = 1;
        a.m[4][3] = 1;
        a.m[5][4] = 1;
        a.m[6][5] = 1;
        a.m[7][6] = 1;
        a.m[8][7] = 1;
        a.m[9][8] = 1;

        c = pow(a,a,k-10);
        int res = 0;
        for(int i = 0;i<9;i++)
        {
            res+=(c.m[0][i]*(9-i))%mod;
            res = res%mod;
        }
        printf("%d\n",res);
    }
    return 0;
}

你可能感兴趣的:((1757)HDU)