http://acm.hdu.edu.cn/showproblem.php?pid=4920
给定两个n阶矩阵,求矩阵相乘后模3.
直接搞肯定会超时
特殊处理1和2的情况
实际上是水过的.....
貌似bitset这样的可搞
http://blog.csdn.net/keshuai19940722/article/details/38391913
#include <cstdlib> #include <iostream> using namespace std; int a[801][801],b[801][801],c[801][801],b1[801][801]; int main() { int i,j,k,n,temp,ans; while ((scanf("%d",&n))!= EOF ) { for (i=1;i<=n;i++) for (j=1;j<=n;j++) { scanf("%d",&temp); a[i][j]=temp%3; } for (i=1;i<=n;i++) for (j=1;j<=n;j++) { scanf("%d",&temp); b[i][j]=temp%3; b1[i][j]=(temp*2)%3; } /* for (i=1;i<=n;i++) for(j=1;j<=n;j++) { ans = 0; for(k=1;k<=n;k++) ans+=a[i][k]*b[k][j]; ans = ans % 3; printf("%d ",ans); if (j==n) printf("\n"); }*/ memset(c,0,sizeof(c)); for (i=1;i<=n;i++) for (j=1;j<=n;j++) { if (a[i][j]==1) for (k=1;k<=n;k++) c[i][k]+=b[j][k]; else if (a[i][j]==2) for (k=1;k<=n;k++) c[i][k]+=b1[j][k]; } for (i=1;i<=n;i++) { for (j=1;j<=n;j++) if (j!=n) printf("%d ",c[i][j]%3); else printf("%d",c[i][j]%3); printf("\n"); } } return 0; }