Gym - 101341I Matrix God
2017-05-08 18:09
302 查看
题意:
给出 n * n 的 A , B ,
C 三个矩阵,问 A * B 是否等于 C
思路:
n 太大,不能暴力。O(n^3)
我们可以将矩阵变化压缩一下。转化为O(n^2)。
代码:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <ctime>
using namespace std;
const int maxn=1e3+100;
const int mod=1e9+7;
int n;
int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn],c1[maxn],b1[maxn];
void rd(int a[][maxn]){
for (int i=0; i<n; ++i) {
for (int j=0; j<n; ++j) {
scanf("%d",&a[i][j]);
}
}
}
void mul(int a[][maxn],int d[]){
int tmp[maxn];
for (int i=0; i<n; ++i) {
tmp[i]=0;
for(int j=0;j<n;++j)
tmp[i]=(tmp[i]+1ll*a[i][j]*d[j])%mod;
}
for (int i=0; i<n; ++i) {
d[i]=tmp[i];
}
}
bool pd(int a[],int b[]){
for (int i=0; i<n; ++i) {
if (a[i]!=b[i]) {
return false;
}
}
return true;
}
int main() {
srand((unsigned)time(NULL));
scanf("%d",&n);
for (int i=0; i<n; ++i)
c1[i]=b1[i]=rand();
rd(a);rd(b);rd(c);
mul(b, b1);mul(c, c1);
mul(a, b1);
if (pd(b1,c1)) {
puts("YES");
}
else puts("NO");
return 0;
}
给出 n * n 的 A , B ,
C 三个矩阵,问 A * B 是否等于 C
思路:
n 太大,不能暴力。O(n^3)
我们可以将矩阵变化压缩一下。转化为O(n^2)。
代码:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <ctime>
using namespace std;
const int maxn=1e3+100;
const int mod=1e9+7;
int n;
int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn],c1[maxn],b1[maxn];
void rd(int a[][maxn]){
for (int i=0; i<n; ++i) {
for (int j=0; j<n; ++j) {
scanf("%d",&a[i][j]);
}
}
}
void mul(int a[][maxn],int d[]){
int tmp[maxn];
for (int i=0; i<n; ++i) {
tmp[i]=0;
for(int j=0;j<n;++j)
tmp[i]=(tmp[i]+1ll*a[i][j]*d[j])%mod;
}
for (int i=0; i<n; ++i) {
d[i]=tmp[i];
}
}
bool pd(int a[],int b[]){
for (int i=0; i<n; ++i) {
if (a[i]!=b[i]) {
return false;
}
}
return true;
}
int main() {
srand((unsigned)time(NULL));
scanf("%d",&n);
for (int i=0; i<n; ++i)
c1[i]=b1[i]=rand();
rd(a);rd(b);rd(c);
mul(b, b1);mul(c, c1);
mul(a, b1);
if (pd(b1,c1)) {
puts("YES");
}
else puts("NO");
return 0;
}
相关文章推荐
- GYM 100488 G.Change-making Problem(贪心)
- Gym - 101243H Non-random numbers【构造+数学】
- Gym 101243 (2016-2017 ACM Central Region of Russia Quarterfinal Programming Contest)
- GYM 100712 H.Bridges(边双连通分量)
- map和结构体的简单运用Gym - 100883C
- 神奇递归Gym - 100883F
- GYM 101102 D.Rectangles(单调栈)
- 在centos 07上安装gym
- GYM 101149 K.Revenge of the Dragon(机智)
- Codeforces Gym 100269E Energy Tycoon 贪心
- GYM 100971 B.Derangement(水~)
- GYM 101086 C.Everything(字典树)
- GYM 101061 B.RGB plants(dp+矩阵快速幂)
- GYM 100694 I.Goat in the Field(水~)
- Gym 101142C CodeCoder vs TopForces (搜索)
- Gym - 100676F Palindrome —— 并查集
- Codeforces Gym 100623F Problem F. Fenwick Tree
- Gym - 101205D Fibonacci Words【kmp】
- Gym 101308A Asteroids 三维凸包 求重心
- Gym - 101147J Whistle's New Car 树上差分