首页 > 代码库 > Hdu 1757A Simple Math Problem矩阵

Hdu 1757A Simple Math Problem矩阵

构造个矩阵搞下就行了

a0 a1 a2 a3 a4 a5 a6 a7 a8 a9

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

#include <cstdio>#include <algorithm>#include <iostream>#include <string.h>typedef long long LL;using namespace std;LL  vis[100];LL  M;struct Matrix{    LL  m[12][12];};LL  n;Matrix Mul(Matrix a, Matrix b){    Matrix ans;    for (LL i = 0; i < 10; i++){        for (LL j = 0; j < 10; j++){            ans.m[i][j] = 0;            for (LL k = 0; k < 10; k++){                ans.m[i][j] += a.m[i][k] * b.m[k][j];                ans.m[i][j] %= M;            }        }    }    return ans;}Matrix quick(Matrix a, LL b){    Matrix ans;    for (LL i = 0; i < 10; i++)    for (LL j = 0; j < 10; j++)        ans.m[i][j] = (i == j);    while (b){        if (b & 1) ans = Mul(ans, a);        a = Mul(a, a);        b >>= 1;    }    return ans;}Matrix init(){    for (LL i = 0; i < 10; i++)        scanf("%d", &vis[i]);    Matrix ans;    for (LL i = 0; i < 10; i++)    for (LL j = 0; j < 10; j++)        ans.m[i][j] = 0;    for (LL i = 0; i < 10; i++)        ans.m[0][i] = vis[i];    for (LL i = 1; i < 10; i++){        ans.m[i][i - 1] = 1;    }    return ans;}void gao(){    LL t = n - 9;    Matrix ans = init();    ans = quick(ans, t);    LL sum = 0;    for (LL i = 0; i < 10; i++){        sum += ans.m[0][i] * (9 - i);        sum %= M;    }    cout << sum << endl;}int main(){    while (cin >> n >> M){        if (n < 10){            cout << n << endl;            continue;        }        gao();    }    return 0;}

 

Hdu 1757A Simple Math Problem矩阵