题意:当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);
ai(0<=i<=9) 只能是0或者1 ,给出a0 ~ a9,k和m,计算f(k)%m(k<2*10^9 , m < 10^5)。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757
——>>构造矩阵,快速幂求解。
用excel弄了个~~
#include <cstdio> #include <cstring> using namespace std; const int maxn = 10 + 5; int k, mod, n; struct Mar{ //矩阵 int m[maxn][maxn]; Mar(){ memset(m, 0, sizeof(m)); } }; Mar operator + (Mar a, Mar b){ //矩阵+ Mar ret; for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) ret.m[i][j] = (a.m[i][j] + b.m[i][j]) % mod; return ret; } Mar operator * (Mar a, Mar b){ //矩阵* Mar ret; for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) for(int l = 0; l < n; l++) ret.m[i][j] = (ret.m[i][j] + a.m[i][l] * b.m[l][j]) % mod; return ret; } Mar pow_mod(Mar a, int n){ //矩阵快速幂 if(n == 1) return a; Mar x = pow_mod(a, n/2); x = x * x; if(n&1) x = x * a; return x; } void solve(){ Mar A; n = 10; int i, j, ret = 0; for(i = 0; i < n; i++) scanf("%d", &A.m[0][i]); if(k < 10) ret = k % mod; else{ for(i = 1, j = 0; i < n; i++, j++) A.m[i][j] = 1; A = pow_mod(A, k-9); for(i = 0, j = 9; i < n; i++, j--) ret = (ret + A.m[0][i] * j) % mod; } printf("%d\n", ret); } int main() { while(scanf("%d%d", &k, &mod) == 2) solve(); return 0; }