Bzoj 2396. 神奇的矩阵

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值