http://codeforces.com/gym/101341
Test instructions
- Given three square a,b,c, Q Ab=c is it set up?
- The scale of the square is up to 1000.
Ideas
- The time complexity for AB is n*n*n and will time out
- Left multiply by one row n column vector, time complexity reduced to n*n
"Accepted"
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <string>5#include <algorithm>6#include <cmath>7#include <queue>8 9 using namespacestd;TentypedefLong Longll; One Const intinf=0x3f3f3f3f; A Const intmaxn=1e3+2; - Constll mod=1e9+7; -ll a[maxn][maxn],b[maxn][maxn],c[maxn][maxn],r[2][maxn],ra[2][maxn],rab[2][maxn],rc[2][MAXN]; the intn,m; - - intMain () - { + while(~SCANF ("%d",&N)) - { + for(intI=1; i<=n;i++) A { at for(intk=1; k<=n;k++) - { -scanf"%lld",&a[i][k]); - } - } - for(intI=1; i<=n;i++) in { - for(intk=1; k<=n;k++) to { +scanf"%lld",&b[i][k]); - } the } * for(intI=1; i<=n;i++) $ {Panax Notoginseng for(intk=1; k<=n;k++) - { thescanf"%lld",&c[i][k]); + } A } the for(intI=1; i<=n;i++) + { -r[1][i]=rand ()% -+1; $ } $memset (RA,0,sizeof(RA)); -Memset (Rab,0,sizeof(RAB)); - for(intI=1; i<=n;i++) the { - for(intk=1; k<=n;k++)Wuyi { thera[1][i]= (ra[1][i]+r[1][K]*A[K][I]%MOD)%MoD; - } Wu } - for(intI=1; i<=n;i++) About { $ for(intk=1; k<=n;k++) - { -rab[1][i]= (rab[1][i]+ra[1][K]*B[K][I]%MOD)%MoD; - } A } + for(intI=1; i<=n;i++) the { - for(intk=1; k<=n;k++) $ { therc[1][i]= (rc[1][i]+r[1][K]*C[K][I]%MOD)%MoD; the } the } the intflag=1; - for(intI=1; i<=n;i++) in { the if(rab[1][i]!=rc[1][i]) the { Aboutflag=0; the Break; the } the } + if(flag) - { thePuts"YES");Bayi } the Else the { -Puts"NO"); - } the the } the return 0; the}
View Code
"Randomization algorithm" Codeforces Matrix God