poj 3233 Matrix Power Series

poj 3233 Matrix Power Series


如果是2333就神作勒= =
其实是SBT啊
$A1+A2+A3+...+Ak=(A1+A2+A3+...+A)\times(1+A^{k'+1}) (2|k) \( \)A1+A2+A3+...+Ak=(A1+A2+A3+...+A)\times(1+A{k'+1})+Ak (k\equiv 1\pmod{2})$
递归一下

// It is made by XZZ
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define rep(a,b,c) for(rg int a=b;a<=c;a++)
#define drep(a,b,c) for(rg int a=b;a>=c;a--)
#define erep(a,b) for(rg int a=fir[b];a;a=nxt[a])
#define il inline
#define rg register
#define vd void
il int gi(){
    rg int x=0;bool flg=0;rg char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')flg=1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return flg?-x:x;
}
int n,k,mod;
il vd add(int&a,const int&b){a+=b;if(a>mod)a-=mod;}
struct _{
	int dt[31][31];
	_(){memset(dt,0,sizeof dt);rep(i,1,n)dt[i][i]=1;}
	_(int a){memset(dt,0,sizeof dt);}
};
il _ operator +(_ a,const _&b){rep(i,1,n)rep(j,1,n)add(a.dt[i][j],b.dt[i][j]);return a;}
il vd operator +=(_&a,const _&b){rep(i,1,n)rep(j,1,n)add(a.dt[i][j],b.dt[i][j]);}
il _ operator *(const _&a,const _&b){
	_ c;
	rep(i,1,n)rep(j,1,n){
		c.dt[i][j]=0;
		rep(l,1,n)add(c.dt[i][j],a.dt[i][l]*b.dt[l][j]%mod);
	}
	return c;
}
il vd operator *=(_&a,const _&b){a=a*b;}
il _ operator ^(_ a,int b){
	_ ret;
	while(b){
		if(b&1)ret*=a;
		a*=a;b>>=1;
	}
	return ret;
}
il _ solve(_ a,int k){
	if(k==1)return a;
	if(k==2)return a+a*a;
	_ ls=solve(a,k>>1);
	ls=ls+ls*(a^(k>>1));
	if(k&1)ls+=a^k;
	return ls;
}
il vd work(){
	_ s,ans(0);
	rep(i,1,n)rep(j,1,n)s.dt[i][j]=gi();
	ans=solve(s,k);
	rep(i,1,n)rep(j,1,n)printf("%d%c",ans.dt[i][j]%mod," \n"[j==n]);
}
main(){
	while(scanf("%d%d%d",&n,&k,&mod)==3)work();
	return 0;
}
posted @ 2017-10-10 20:41  菜狗xzz  阅读(225)  评论(0编辑  收藏  举报