[BZOJ4688]One-Dimensional

One-Dimensional

题解

n这么小,t又比较大,很明显的一道矩阵加速板题。

dp方程式它都给你了,通过这个方程式建一个矩阵跑跑快速幂就行了。

大概像这样\begin{bmatrix} b & & & ... & 0\\ c & a & & ...& 0\\ 0 & b & a & ... & 0\\ 0 & c & b & ... & 0\\ ... & ... & ... & ... & ...\\ 0 & 0 & 0 & ... & a \\ 0 & 0 & 0 & ... & b \end{bmatrix}

源码

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
using namespace std;
typedef long long LL;
#define gc() getchar()
template<typename _T>
void read(_T &x){
	_T f=1;x=0;char s=gc();
	while(s>'9'||s<'0'){if(s=='-')f=-1;s=gc();}
	while(s>='0'&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=gc();}
	x*=f;
}
int nn,mo,a,b,c,t;
struct martix{
	int n,m;
	int c[55][55];
	martix(){n=m=0;memset(c,0,sizeof(c));}
	martix operator * (const martix& a){
		martix r;r.n=n;r.m=a.m;
		for(int i=1;i<=r.n;i++)				
			for(int k=1;k<=m;k++)
				for(int j=1;j<=r.m;j++)
					r.c[i][j]=(r.c[i][j]+c[i][k]*a.c[k][j])%mo;
		return r;
	}
}A,B;
signed main(){
	while(scanf("%d %d %d %d %d %d",&nn,&mo,&a,&b,&c,&t)!=EOF&&mo){
		memset(A.c,0,sizeof(A.c));A.n=1;A.m=nn;
		memset(B.c,0,sizeof(B.c));B.n=B.m=nn;
		for(int i=1;i<=nn;i++){
			read(A.c[1][i]);
			if(i>1)B.c[i-1][i]=a;
			B.c[i][i]=b;
			if(i<nn)B.c[i+1][i]=c;
		}
		while(t){
			if(t&1)A=A*B;
			B=B*B;t>>=1;
		}
		for(int i=1;i<=nn;i++)printf("%d ",A.c[1][i]);
		puts("");
	}
	return 0;
}

谢谢!!!

发布了117 篇原创文章 · 获赞 154 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Tan_tan_tann/article/details/104627885
one