hdu 1757 A Simple Math Problem_矩阵快速幂

题意:略

简单的矩阵快速幂就行了

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define LL long long 
#define N 10
int  m;
struct node{
	int mat[N][N];
	node operator *(const node &x){
		node tmp;
		memset(tmp.mat,0,sizeof(tmp.mat));
		for(int i=0;i<N;i++)
		for(int j=0;j<N;j++)
		for(int k=0;k<N;k++){
				tmp.mat[i][j]+=(mat[i][k]*x.mat[k][j])%m;
				tmp.mat[i][j]%=m;
		}
		return tmp;
	}
}cat,b;
node _pow(int n){
	int i;
	memset(b.mat,0,sizeof(b.mat));
	for(i=0;i<N;i++)
		b.mat[i][i]=1;
	while(n){
		if(n&1)		
			b=b*cat;
		cat=cat*cat;
		n>>=1;
	}
	return b;
}
int main(int argc, char** argv) {
	int sum,n;
	int i,j;
	
	while(scanf("%d%d",&n,&m)!=EOF){
		memset(cat.mat,0,sizeof(cat.mat));
		for(i=0;i<N-1;i++)
			cat.mat[i+1][i]=1;
		for(i=0;i<N;i++)
			scanf("%d",&cat.mat[0][i]);
		if(n<N){
			printf("%d\n",n%m);
			continue;
		}
		sum=0;
		cat=_pow(n-9);
		for(i=0;i<N;i++)
			sum+=cat.mat[0][i]*(9-i);
		printf("%d\n",sum%m);
	
	}
	return 0;
}


posted @ 2013-11-13 22:20  Teemo的技术blog  阅读(127)  评论(0编辑  收藏  举报