Editorial for VM 14 Bài 09 - Nhân ma trận
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của ladpro98
#include <bits/stdc++.h> const int N = 1001; const int lim = 5; using namespace std; struct mat2D { char mat[N][N]; }; struct mat1D { char mat[N]; }; mat1D bb, cc, d; mat2D a, b, c; int n; char s[N]; mat1D operator * (mat2D a, mat1D b) { mat1D c; int i, j; for(i = 0; i < n; i++) c.mat[i] = 0; for(i = 0; i < n; i++) for(j = 0; j < n; j++) c.mat[i] = (c.mat[i] + a.mat[i][j] * b.mat[j]) % 10; return c; } bool ok() { //return true if bb = cc for(int i = 0; i < n; i++) if (bb.mat[i] != cc.mat[i]) return false; return true; } int main() { int t, i, j, cnt; bool same; char ch; srand(time(NULL)); scanf("%d\n", &t); while (t--) { scanf("%d\n", &n); for(i = 0; i < n; i++) { scanf("%s\n", s); for(j = 0; j < n; j++) a.mat[i][j] = s[j] - '0'; } for(i = 0; i < n; i++) { scanf("%s\n", s); for(j = 0; j < n; j++) b.mat[i][j] = s[j] - '0'; } for(i = 0; i < n; i++) { scanf("%s\n", s); for(j = 0; j < n; j++) c.mat[i][j] = s[j] - '0'; } same = true; for(cnt = 0; cnt < lim; cnt++) { for(i = 0; i < n; i++) d.mat[i] = rand() % 10; bb = b * d; bb = a * bb; cc = c * d; //A * B = C <=> A * (B * D) = C * D if (!ok()) { same = false; break; } } if (same) printf("YES\n"); else printf("NO\n"); } return 0; }
Code mẫu của RR
#include <sstream> #include <iomanip> #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <map> #include <stack> #include <queue> #include <string> #include <deque> #include <complex> #define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++) #define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--) #define REP(i,a) for(int i=0,_a=(a); i<_a; i++) #define FORN(i,a,b) for(int i=(a),_b=(b);i<_b;i++) #define DOWN(i,a,b) for(int i=a,_b=(b);i>=_b;i--) #define SET(a,v) memset(a,v,sizeof(a)) #define sqr(x) ((x)*(x)) #define ll long long #define F first #define S second #define PB push_back #define MP make_pair #define DEBUG(x) { cout << #x << " = "; cout << x << endl; } #define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; } #define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; } using namespace std; //Buffer reading int INP,AM,REACHEOF; const int BUFSIZE = (1<<12) + 17; char BUF[BUFSIZE+1], *inp=BUF; #define GETCHAR(INP) { \ if(!*inp && !REACHEOF) { \ memset(BUF,0,sizeof BUF);\ int inpzzz = fread(BUF,1,BUFSIZE,stdin);\ if (inpzzz != BUFSIZE) REACHEOF = true;\ inp=BUF; \ } \ INP=*inp++; \ } #define DIG(a) (((a)>='0')&&((a)<='9')) #define GN(j) { \ AM=0;\ GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\ if (INP=='-') {AM=1;GETCHAR(INP);} \ j=INP-'0'; GETCHAR(INP); \ while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \ if (AM) j=-j;\ } //End of buffer reading const long double PI = acos((long double) -1.0); const int MN = 1011; const int MOD = 10; int n, a[MN][MN], b[MN][MN], c[MN][MN], t[MN], x[MN], y[MN], z[MN]; bool check() { REP(turn,5) { FOR(i,1,n) t[i] = rand() % MOD; FOR(i,1,n) { x[i] = 0; FOR(j,1,n) x[i] = (x[i] + b[i][j] * t[j]) % MOD; } FOR(i,1,n) { y[i] = 0; FOR(j,1,n) y[i] = (y[i] + a[i][j] * x[j]) % MOD; } FOR(i,1,n) { z[i] = 0; FOR(j,1,n) z[i] = (z[i] + c[i][j] * t[j]) % MOD; } FOR(i,1,n) if (z[i] != y[i]) return false; } return true; } char tmp[1011]; int main() { int ntest; scanf("%d", &ntest); while (ntest--) { scanf("%d\n", &n); FOR(i,1,n) { gets(tmp); FOR(j,1,n) a[i][j] = tmp[j-1] - '0'; } FOR(i,1,n) { gets(tmp); FOR(j,1,n) b[i][j] = tmp[j-1] - '0'; } FOR(i,1,n) { gets(tmp); FOR(j,1,n) c[i][j] = tmp[j-1] - '0'; } if (check()) puts("YES"); else puts("NO"); } return 0; }
Code mẫu của skyvn97
#include<cstdio> #include<cstdlib> #include<vector> #define MAX 1111 #define FOR(i,a,b) for (int i=(a);i<=(b);i=i+1) #define REP(i,n) for (int i=0;i<(n);i=i+1) using namespace std; struct matrix { int m,n; vector<vector<int> > d; matrix() { m=n=0; } matrix(int m,int n) { this->m=m; this->n=n; d.assign(m,vector<int>(n,0)); } matrix operator * (const matrix &a) const { int x=m; int y=n; int z=a.n; matrix res=matrix(x,z); REP(i,x) REP(j,z) REP(k,y) res.d[i][j]=(res.d[i][j]+d[i][k]*a.d[k][j])%10; return (res); } bool operator == (const matrix &a) const { REP(i,m) REP(j,n) if (d[i][j]!=a.d[i][j]) return (false); return (true); } }; char a[MAX][MAX],b[MAX][MAX],c[MAX][MAX]; int n; matrix A,B,C; inline int nextInt(void) { int x; scanf("%d",&x); return (x); } void init(void) { n=nextInt(); REP(i,n) scanf("%s",a[i]); REP(i,n) scanf("%s",b[i]); REP(i,n) scanf("%s",c[i]); A=matrix(n,n); B=matrix(n,n); C=matrix(n,n); REP(i,n) REP(j,n) { A.d[i][j]=a[i][j]-48; B.d[i][j]=b[i][j]-48; C.d[i][j]=c[i][j]-48; } } bool check(void) { matrix R=matrix(n,1); REP(i,n) R.d[i][0]=rand()&1; return (A*(B*R)==C*R); } void process(void) { REP(zz,7) if (!check()) { printf("NO\n"); return; } printf("YES\n"); } int main(void) { int t=nextInt(); srand(t); REP(zz,t) { init(); process(); } return 0; }
Comments