codeforces gym 101341 I Matrix God

1 篇文章 0 订阅
1 篇文章 0 订阅

题解:

大致题意为给定 n ∗ n n*n nn的矩阵A,B,C,判断矩阵C是否为 A ∗ B A*B AB。n的范围是1e3。
显然无法用 n 3 n^3 n3的矩阵乘法暴力求解实现。因此我们想到如何降低维度,发现矩阵乘法的复杂度与矩阵的形状有关,如果是一个 1 ∗ n 1*n 1n的矩阵乘,那么结果就是 n 2 n^2 n2的解出。
于是想到能不能将等式左边同时乘上 1 ∗ n 1*n 1n的D矩阵,即 D ∗ A ∗ B D*A*B DAB= D ∗ C D*C DC。于是我们用茅台随机出D矩阵,进行check操作即可(实验发现一次随机即可得出答案)

代码:

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <random>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
#define PB push_back
#define MP make_pair
#define INF 1073741824
#define inf 1152921504606846976
#define pi 3.14159265358979323846
//#pragma comment(linker,"/STACK:10240000,10240000")
mt19937 rand_(time(0));
const int N=3e5+7,M=2e6;
const long long mod=1e9+7;
inline int read(){int ret=0;char ch=getchar();bool f=1;for(;!isdigit(ch);ch=getchar()) f^=!(ch^'-');for(;isdigit(ch);ch=getchar()) ret=(ret<<1)+(ret<<3)+ch-48;return f?ret:-ret;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}//ÄæÔª
//int head[N],NEXT[M],ver[M],tot;void link(int u,int v){ver[++tot]=v;NEXT[tot]=head[u];head[u]=tot;}

ll a[1200][1200],b[1200][1200],c[1200][1200],d[3][1200];
ll res1[3][1200],res2[3][1200],tmp[3][1200];
int n;
void mul(ll aa[][1200],ll bb[][1200]){
    for(int i=1;i<=n;i++){
        tmp[1][i]=0;
        for(int k=1;k<=n;k++){
            tmp[1][i]+=aa[1][k]*bb[k][i]%mod;
            tmp[1][i]%=mod;
        }
    }
    for(int i=1;i<=n;i++){
        aa[1][i]=tmp[1][i];
    }
}
bool judge(){
    for(int i=1;i<=n;i++){
        if(res1[1][i]!=res2[1][i]) return false;
    }
    return true;
}
int main(){
    //freopen("i.txt","r",stdin);
    //ios::sync_with_stdio(false);
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            scanf("%lld",&a[i][j]);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            scanf("%lld",&b[i][j]);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            scanf("%lld",&c[i][j]);
        }
    }
    bool mark=true;
    for(int t=1;t<=1;t++){
        for(int i=1;i<=n;i++){
            d[1][i]=rand_()%mod;
            res1[1][i]=d[1][i];
            res2[1][i]=d[1][i];
        }
        mul(res1,a);
        mul(res1,b);
        mul(res2,c);
        if(!judge()){
            mark=false;
            break;
        }
    }
    if(mark) puts("YES");
    else puts("NO");
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您提供的链接是Codeforces的一个问题,问题编号为104377。Codeforces是一个知名的在线编程竞赛平台,经常举办各种编程比赛和训练。GymCodeforces的一个扩展包,用于组织私人比赛和训练。您提供的链接指向了一个问题的页面,但具体的问题内容和描述无法通过链接获取。如果您有具体的问题或需要了解关于Codeforces Gym的更多信息,请提供更详细的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [http://codeforces.com/gym/100623/attachments E题](https://blog.csdn.net/weixin_30820077/article/details/99723867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [http://codeforces.com/gym/100623/attachments H题](https://blog.csdn.net/weixin_38166726/article/details/99723856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [CodeforcesPP:Codeforces扩展包](https://download.csdn.net/download/weixin_42101164/18409501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值