poj 3233 Matrix Power Series http://poj.org/problem?id=3233
/************************************************************** Problem:poj 3233 User: youmi Language: C++ Result: Accepted Time:1735MS Memory:3880K ****************************************************************/ //#pragma comment(linker, "/STACK:1024000000,1024000000") //#include<bits/stdc++.h> #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <stack> #include <set> #include <sstream> #include <cmath> #include <queue> #include <deque> #include <string> #include <vector> #define zeros(a) memset(a,0,sizeof(a)) #define ones(a) memset(a,-1,sizeof(a)) #define sc(a) scanf("%d",&a) #define sc2(a,b) scanf("%d%d",&a,&b) #define sc3(a,b,c) scanf("%d%d%d",&a,&b,&c) #define scs(a) scanf("%s",a) #define sclld(a) scanf("%I64d",&a) #define pt(a) printf("%d\n",a) #define ptlld(a) printf("%I64d\n",a) #define rep0(i,n) for(int i=0;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define rep_1(i,n) for(int i=n;i>=1;i--) #define rep_0(i,n) for(int i=n-1;i>=0;i--) #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) #define lson (step<<1) #define rson (lson+1) #define esp 1e-6 #define oo 0x3fffffff #define TEST cout<<"*************************"<<endl using namespace std; typedef long long ll; int n,tot,mod; const int maxn=50; typedef struct T { int mat[maxn][maxn]; }matrix; matrix org,ans,unit; matrix operator+(matrix a,matrix b) { matrix c; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) c.mat[i][j]=(a.mat[i][j]+b.mat[i][j])%mod; return c; } matrix operator*(matrix a,matrix b) { matrix c; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { c.mat[i][j]=0; for(int k=1;k<=n;k++) c.mat[i][j]=(c.mat[i][j]+(a.mat[i][k]*b.mat[k][j])%mod)%mod; } return c; } matrix q_pow(int k) { matrix res=unit,p=org; while(k) { if(k&1) res=res*p; k>>=1; p=p*p; } return res; } matrix solve(int k) { if(k==1) return org; int half=k>>1; matrix temp=solve(half); matrix t; if(k&1) { t=q_pow(half+1); temp=t+temp+temp*t; } else { t=q_pow(half); temp=temp+temp*t; } return temp; } void init() { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { org.mat[i][j]%=mod; unit.mat[i][j]=(i==j); } } } int main() { //freopen("in.txt","r",stdin); while(~sc3(n,tot,mod)) { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&org.mat[i][j]); init(); ans=solve(tot); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) printf("%d%c",ans.mat[i][j],j==n?‘\n‘:‘ ‘); } } return 0; }
时间: 2024-11-05 19:01:28