- C24yechenxi's blog
POJ 3318
- @ 2026-7-14 22:36:34
AC代码:
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define LL long long
#define ULL unsigned long long
using namespace std;
//typedef long long LL;
const int N = 505;
const int mod = 100000000;
int n;
/*void mul(LL a[][N+1], LL b[][N+1], LL c[][N+1]) {
static LL tmp[N+1][N+1];
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
tmp[i][j] = 0;
}
}
for(int k = 1;k <= n;k++)
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
tmp[i][j] = (ULL)(tmp[i][j] + a[i][k] * b[k][j]);
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
c[i][j] = tmp[i][j];//cpy
}*/
/*void fpow(LL a[][N+1], LL k, LL res[][N+1]) {
for(int i = 1;i <= N;i++) {
for(int j = 1;j <= N;j++){
//TODO
res[i][j] = 0;
}
}
for(int i = 1;i <= N;i++) {
res[i][i] = 1;
}
LL tmb[N+1][N+1];
for(int i = 1;i <= N;i++) {
for(int j = 1;j <= N;j++) {
tmb[i][j] = a[i][j];
}
}
while(k) {
if(k & 1) mul(res, tmb, res);
k >>= 1;
mul(tmb, tmb, tmb);
}
}*/
/*bool cp(LL x[N+1][N+1], LL y[N+1][N+1]){
//bool dick = 1;
for(int i = 1;i <= n;i++) {
for(int j = 1;j <= n;j++) {
if(x[i][j] != y[i][j]){
return 0;
}
}
}
return 1;
}*/
void mul_vec(LL m[][N+1], LL vec[], LL out[]) {
//memset(out, 0, sizeof(LL) * (N+1));
fill(out, out + N + 1, 0);
for(int i = 1;i <= n;i++)
for(int k = 1;k <= n;k++)
out[i] += (ULL)(m[i][k]) * (vec[k]);
}
LL aa[N+1][N+1], bb[N+1][N+1], cc[N+1][N+1];
LL pre_[N+1][N+1];
LL x[N+1], L[N + 1], R[N+1], rr[N+1];
bool check() {
for(int i = 1;i <= n;i++) {
ULL val = (ULL)rand() * rand();
rr[i] = val;
}
//memset(x,0,sizeof(x));
//memset(L, 0, sizeof(L));
//memset(R, 0, sizeof(R));
mul_vec(bb, rr, x);
mul_vec(aa, x, L);
mul_vec(cc, rr, R);
bool is_d = 1;
for(int i = 1;i <= n;i++){
if(L[i] != R[i]){
is_d = 0;
break;
}
}
return is_d;
}
int main() {
srand(time(0));
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
while(cin >> n){
/*memset(aa, 0, sizeof(aa));
memset(bb, 0, sizeof(bb));
memset(cc, 0, sizeof(cc));
memset(pre_, 0, sizeof(pre_));*/
for(int i = 1;i <= n;i ++) {
for(int j = 1;j <= n;j ++) {
cin >> aa[i][j];
}
}
for(int i = 1;i <= n;i ++) {
for(int j = 1;j <= n;j ++) {
cin >> bb[i][j];
}
}
for(int i = 1;i <= n;i ++) {
for(int j = 1;j <= n;j ++) {
cin >> cc[i][j];
}
}
/*for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
pre_[i][j] = 0;
mul(aa, bb, pre_);
bool ans = cp(pre_, cc);
if(ans) cout << "YES\n";
else cout << "NO\n";*/
bool ans = 1;
for(int t = 0;t < 15;t++) {
if(!check()){
ans = 0;
break;
}
}
if(ans) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
WA代码:
#include <iostream>
#include <string.h>//Different Point 1#
#include <stdlib.h>
#include <time.h>
#define LL long long
using namespace std;
//typedef long long LL;
const int N = 505;
const int mod = 2009;//Different Point 2#
int n;
void mul(LL a[][N+1], LL b[][N+1], LL c[][N+1]) {//Diffierent Point 3#:AC代码已注释无关函数模板;
static LL tmp[N+1][N+1];
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
tmp[i][j] = 0;
}
}
for(int k = 1;k <= n;k++)
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
tmp[i][j] = (tmp[i][j] + a[i][k] * b[k][j]);
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
c[i][j] = tmp[i][j];//cpy
}
void fpow(LL a[][N+1], LL k, LL res[][N+1]) {
for(int i = 1;i <= N;i++) {
for(int j = 1;j <= N;j++){
//TODO
res[i][j] = 0;
}
}
for(int i = 1;i <= N;i++) {
res[i][i] = 1;
}
LL tmb[N+1][N+1];
for(int i = 1;i <= N;i++) {
for(int j = 1;j <= N;j++) {
tmb[i][j] = a[i][j];
}
}
while(k) {
if(k & 1) mul(res, tmb, res);
k >>= 1;
mul(tmb, tmb, tmb);
}
}
bool cp(LL x[N+1][N+1], LL y[N+1][N+1]){
//bool dick = 1;
for(int i = 1;i <= n;i++) {
for(int j = 1;j <= n;j++) {
if(x[i][j] != y[i][j]){
return 0;
}
}
}
return 1;
}//注释结束
void mul_vec(LL m[][N+1], LL vec[], LL out[]) {
memset(out, 0, sizeof(LL) * (N+1));//Diffierent Point 4#:换memset为fill;
for(int i = 1;i <= n;i++)
for(int k = 1;k <= n;k++)
out[i] = (out[i] + (m[i][k] % mod) * (vec[k] % mod) ) % mod + mod;//Diffierent Point 5#:增加ULL自然溢出,删除取模操作
}
LL aa[N+1][N+1], bb[N+1][N+1], cc[N+1][N+1];
LL pre_[N+1][N+1];
bool check() {
LL rr[N+1];
for(int i = 1;i <= n;i++) {
rr[i] = 1LL * rand() * rand() % 200000 - 100000;
}
//Diffierent Point 6#:将x, y, z设为全局数组
LL x[N+1];
memset(x,0,sizeof(x));
mul_vec(bb, rr, x);
LL L[N + 1];
memset(L, 0, sizeof(L));
mul_vec(aa, x, L);
LL R[N+1];
memset(R, 0, sizeof(R));
mul_vec(cc, rr, R);
bool is_d = 1;
for(int i = 1;i <= n;i++){
if(L[i] != R[i]){
is_d = 0;
break;
}
}
return is_d;
}
int main() {
srand(time(0));
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
while(cin >> n){
/*memset(aa, 0, sizeof(aa));
memset(bb, 0, sizeof(bb));
memset(cc, 0, sizeof(cc));
memset(pre_, 0, sizeof(pre_));*/
for(int i = 1;i <= n;i ++) {
for(int j = 1;j <= n;j ++) {
cin >> aa[i][j];
}
}
for(int i = 1;i <= n;i ++) {
for(int j = 1;j <= n;j ++) {
cin >> bb[i][j];
}
}
for(int i = 1;i <= n;i ++) {
for(int j = 1;j <= n;j ++) {
cin >> cc[i][j];
}
}
/*for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
pre_[i][j] = 0;
mul(aa, bb, pre_);
bool ans = cp(pre_, cc);
if(ans) cout << "YES\n";
else cout << "NO\n";*/
bool ans = 1;
for(int t = 0;t < 10;t++) {//Diffierent Point 7#:哈希增加至15;
if(!check()){
ans = 0;
break;
}
}
if(ans) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}