bzoj2396: 神奇的矩阵(矩阵乘法+随机化)
这题n三方显然会GG...
运用矩阵乘法的性质A*B*R=A*(B*R)=C*R,于是随机化出一个一列的R,就可以把复杂度降低成n方...大概率是不会错的
#include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<algorithm> #define ll long long using namespace std; const int maxn=510,inf=1e9; typedef int mtx[maxn][maxn]; int n,m,x,y,z,tot; mtx f,a,b,c; void read(int &k) { int f=1;k=0;char c=getchar(); while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar(); while(c<='9'&&c>='0')k=k*10+c-'0',c=getchar(); k*=f; } void mul(mtx &a,mtx b) { mtx c;memset(c,0,sizeof(c)); for(int i=1;i<=n;i++) for(int j=1;j<=1;j++) for(int k=1;k<=n;k++) c[i][j]+=a[i][k]*b[k][j]; memcpy(a,c,sizeof(c)); } int main() { srand(19260817); for(int i=1;i<=500;i++)f[i][1]=1+rand()%1000; while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) read(a[i][j]); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) read(b[i][j]); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) read(c[i][j]); mul(b,f);mul(c,f);mul(a,b); int flag=0; for(int i=1;i<=n;i++) if(a[i][1]!=c[i][1]) { flag=1; break; } printf("%s\n",flag?"No":"Yes"); } return 0; }View Code
相关推荐
zhoujiyu 2020-06-28
xiaoxue 2020-05-02
seasongirl 2020-02-02
sasac 2020-09-25
huangjie0 2020-09-25
cloudking000 2020-09-11
xiaoxiaokeke 2020-07-28
mingyunxiaohai 2020-07-28
honghao0 2020-07-27
风吹夏天 2020-07-26
夕加加 2020-07-20
CallmeZhe 2020-06-29
清风徐来水波不兴 2020-06-16
Happyunlimited 2020-06-15
wanff0 2020-06-14
cuiguanjun 2020-06-13
啸林 2020-06-12
jiayuqicz 2020-06-09
章鱼之家 2020-06-08