【题目大意】:
If x < 10 f(x) = x.
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
And ai(0<=i<=9) can only be 0 or 1 .
先给出ai,和x,m,求f(x)%m
【解题思路】:
根据题目意思构造矩阵,跑矩阵乘法即可
【代码】:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
using namespace std;
#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define linf 1LL<<60
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long
struct Mart{
int mat[105][105];
};
Mart mm;
int n;
Mart y;
int k,m,i;
int sum;
Mart mart_multiply(Mart a,Mart b){
Mart c;
for(int i=1; i<=10; i++)
for(int j=1; j<=10; j++){
c.mat[i][j]=0;
for(int k=1; k<=10; k++)
c.mat[i][j]=(c.mat[i][j]%m+a.mat[i][k]*b.mat[k][j]%m)%m;
}
return c;
}
void copy(Mart p){
for (int i=1; i<=10; i++)
for (int j=1; j<=10; j++)
y.mat[i][j]=p.mat[i][j];
}
void solve_mart_power(int np){
Mart p,q;
for(int i=1; i<=10; i++)
for(int j=1; j<=10; j++){
p.mat[i][j]=mm.mat[i][j];
if(i==j) q.mat[i][j]=1.0;
else q.mat[i][j]=0.0;
}
if(np==0) copy(q);
else {
while(np!=1){
if(np&1){
np--;
q=mart_multiply(p,q);
}
else{
np=np/2;
p=mart_multiply(p,p);
}
}
p=mart_multiply(p,q);
copy(p);
}
return ;
}
int main() {
for (int i=1; i<=10; i++){mm.mat[i+1][i]=1; mm.mat[1][i]=1;}
while (~scanf("%d%d",&k,&m)) {
for (int i=1; i<=10; i++)
scanf("%d", &mm.mat[1][i]);
if (k<10) {
printf("%d/n",k%m);
}
else {
solve_mart_power(k-9);
sum=0;
for (int i=1; i<=10; i++)
sum+=y.mat[1][i]*(10-i);
printf("%d\n", sum % m);
}
}
return 0;
}