Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
无限WA。。求救。。#include<stdio.h> #include<string.h> #define N 31 int m1,n1,k1; struct Matrix { int n; int m[N][N]; Matrix operator * (const Matrix& M) { Matrix temp; temp.n = M.n; int i,j,sum,u; memset(temp.m,0,sizeof(temp.m)); for(i = 0;i < n;i++) { for(j = 0;j < n;j++) { sum = 0; for(u = 0;u < n;u++) sum = (sum + m[i][u] * M.m[u][j] % m1) % m1; temp.m[i][j] = sum; } } return temp; } Matrix operator + (const Matrix& M) { Matrix temp; temp.n = M.n; int i,j; for(i = 0;i < n;i++) for(j = 0;j < n;j++) temp.m[i][j] = (m[i][j] + M.m[i][j]) % m1; return temp; } }k; Matrix pow(Matrix& M,int t) { Matrix temp; if(t == 1) return M; temp = M * M; if(t & 1) return pow(temp,t / 2) * temp; return pow(temp,t / 2); } Matrix sum(Matrix& M,int t) { if(t == 1) return M; Matrix temp,temp1; temp = pow(M,t / 2); temp1 = sum(M,t / 2); if(t & 1) return temp * temp1 + temp1 + temp * temp * M ; return temp * temp1 + temp1; } int main() { while(scanf("%d %d %d",&n1,&k1,&m1) != EOF) { int i,j; k.n = n1; for(i = 0;i < n1;i++) for(j = 0;j < n1;j++) { scanf("%d",&k.m[i][j]); k.m[i][j] %= m1; } Matrix temp; temp.n = n1; temp = sum(k,k1); for(i = 0;i < n1;i++) { for(j = 0;j < n1;j++) printf("%3d",temp.m[i][j]); printf("\n"); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator