rand
函数:生成随机数
srand(time(0))
: 生成随机数种子
Miller_rabin
思想
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
const int N = 1010;
int a[N][N], b[N][N], c[N][N], d[N][N];
int e[N][N], f[N][N];
int g[N][N], h[N][N];
void rand_zj(int d[N][N], int L)
{
for (int i = 1; i <= L; i ++)
d[1][i] = rand() % 2; // 随机数生成器
}
void copy_matrix(int to_copy[N][N], int copy[N][N], int n)
{
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
to_copy[i][j] = copy[i][j];
}
bool not_equal(int _1[N][N], int _2[N][N], int n)
{
for (int i = 1; i <= 1; i ++)
for (int j = 1; j <= n; j ++)
if (_1[i][j] != _2[i][j])
return true;
return false;
}
void mul_matrix(int a[N][N], int b[N][N], int n)
{
for (int i = 1; i <= 1; i ++)
for (int j = 1; j <= n; j ++)
h[i][j] = 0;
for (int i = 1; i <= 1; i ++)
for (int j = 1; j <= n; j ++)
for (int k = 1; k <= n; k ++)
h[i][j] += a[i][k] * b[k][j];
for (int i = 1; i <= 1; i ++)
for (int j = 1; j <= n; j ++)
a[i][j] = h[i][j];
}
bool df(int n)
{
for (int i = 1; i <= 8; i ++)
{
rand_zj(d, n);
copy_matrix(g, d, n);
mul_matrix(g, a, n);
mul_matrix(g, b, n);
copy_matrix(e, g, n);
copy_matrix(g, d, n);
mul_matrix(g, c, n);
copy_matrix(f, g, n);
/*
for (int i = 1; i <= n; i ++)
printf ("%d ", d[1][i]);
printf ("\n");
for (int i = 1; i <= n; i ++)
printf ("%d %d\n", e[1][i], f[1][i]);
puts("\n");
*/
if (not_equal(e, f, n))
return false;
}
return true;
}
int main()
{
srand(time(0));
int n;
while (scanf ("%d", &n) == 1)
{
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
scanf ("%d", &a[i][j]);
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
scanf ("%d", &b[i][j]);
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
scanf ("%d", &c[i][j]);
bool ans = df(n);
if (ans)
puts("Yes");
else
puts("No");
}
return 0;
}