QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#359108#7612. Matrix Inversezyz07WA 518ms74776kbC++172.8kb2024-03-20 13:00:032024-03-20 13:00:04

Judging History

你现在查看的是最新测评结果

  • [2024-03-20 13:00:04]
  • 评测
  • 测评结果:WA
  • 用时:518ms
  • 内存:74776kb
  • [2024-03-20 13:00:03]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define For(Ti,Ta,Tb) for(auto Ti=(Ta);Ti<=(Tb);++Ti)
#define Dec(Ti,Ta,Tb) for(auto Ti=(Ta);Ti>=(Tb);--Ti)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
using ll=long long;
using ull=unsigned long long;
mt19937 eng(random_device{}());
const int N=3005,Mod=1e9+7;
void reduce(ll& x){
	x=(x<0?x+Mod:x);
}
ll power(ll x,ll y){
	ll r=1;
	for(;y;y>>=1,x=x*x%Mod){
		if(y&1) r=r*x%Mod;
	}
	return r;
}
using Mat=unsigned[N][N];
int n,ok1[N],ok2[N],id[N],pos[N];
void rmul(Mat& A,ll* v){
	static ll v_[N];
	memset(v_,0,sizeof(v_));
	For(j,1,n){
		For(i,1,n){
			v_[i]+=A[i][j]*v[j];
		}
		if(!(j&7)||j==n){
			For(i,1,n){
				v_[i]%=Mod;
			}
		}
	}
	memcpy(v,v_,sizeof(v_));
}
void mul(ll* v,Mat& A){
	static ll v_[N];
	memset(v_,0,sizeof(v_));
	For(i,1,n){
		For(j,1,n){
			v_[j]+=v[i]*A[i][j];
		}
		if(!(i&7)||i==n){
			For(j,1,n){
				v_[j]%=Mod;
			}
		}
	}
	memcpy(v,v_,sizeof(v_));
}
Mat A,C,C_;
ll v1[N],v2[N],M[N][15];
void gauss(int R,int C){
	for(int r=1,c=1;r<=R&&c<=C;){
		if(!M[r][c]){
			For(i,r+1,R){
				if(M[i][c]){
					swap(M[r],M[i]);
					break;
				}
			}
		}
		if(!M[r][c]){
			++c;
			continue;
		}
		ll inv=power(M[r][c],Mod-2);
		For(i,c,C+1){
			(M[r][i]*=inv)%=Mod;
		}
		For(i,r+1,R){
			Dec(j,C+1,c){
				reduce(M[i][j]-=M[i][c]*M[r][j]%Mod);
			}
		}
		++r;
	}
	Dec(i,C,1){
		For(j,i+1,C){
			reduce(M[i][C+1]-=M[j][C+1]*M[i][j]%Mod);
		}
	}
}
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	#ifdef LOCAL
	assert(freopen("G.in","r",stdin));
	assert(freopen("G.out","w",stdout));
	#endif
	cin>>n;
	For(i,1,n){
		For(j,1,n){
			cin>>A[i][j];
		}
	}
	For(i,1,n){
		For(j,1,n){
			cin>>C[i][j];
			C_[j][i]=C[i][j];
		}
	}
	For($,1,2){
		For(i,1,n){
			v2[i]=v1[i]=eng()%Mod;
		}
		rmul(A,v1);
		rmul(C,v1);
		For(i,1,n){
			ok1[i]|=(v1[i]!=v2[i]);
		}
	}
	For($,1,2){
		For(i,1,n){
			v2[i]=v1[i]=eng()%Mod;
		}
		mul(v1,A);
		mul(v1,C);
		For(i,1,n){
			ok2[i]|=(v1[i]!=v2[i]);
		}
	}
	int cnt=0;
	For(i,1,n){
		if(ok1[i]){
			id[i]=++cnt;
			pos[cnt]=i;
		}
	}
	vector<tuple<int,int,ll>> ans;
	For(j,1,n){
		if(!ok2[j]) continue;
		memset(M,0,sizeof(M));
		For(i,1,n){
			unsigned __int128 val=(i==j);
			For(k,1,n){
				val+=ull(A[i][k])*C_[j][k];
			}
			For(k,1,cnt){
				val-=ull(A[i][pos[k]])*C[pos[k]][j];
				M[i][k]=A[i][pos[k]];
			}
			M[i][cnt+1]=(Mod-val%Mod)%Mod;
		}
		gauss(n,cnt);
		For(i,1,n){
			if(ok1[i]){
				ll val=M[id[i]][cnt+1];
				if(val!=C[i][j]){
					ans.emplace_back(i,j,val);
				}
			}
		}
	}
	sort(range(ans));
	cout<<ans.size()<<'\n';
	for(auto [i,j,x]:ans){
		cout<<i<<' '<<j<<' '<<x<<'\n';
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3664kb

input:

1
953176428
107682094

output:

0

result:

ok single line: '0'

Test #2:

score: 0
Accepted
time: 499ms
memory: 74344kb

input:

1995
586309310 548144807 578573993 437893403 641164340 712256053 172321263 108058526 768610920 123320669 762746291 856047593 979279376 29067913 309867338 292286426 45124325 239705174 675003623 213743652 620561338 116308277 695369179 669459894 682522334 846995555 159510341 999359657 645579085 7499563...

output:

2
827 238 84815305
1466 499 206940592

result:

ok 3 lines

Test #3:

score: 0
Accepted
time: 518ms
memory: 74520kb

input:

1995
436890614 28924575 276129332 63568266 576410175 399540058 591733285 531509939 637241038 596750662 811926780 760228238 317196903 751498201 993802643 102539089 382116597 233386377 974332817 495280100 575832855 616941506 297856263 216480938 638907269 434126707 499611855 764625526 51141033 64624519...

output:

3
315 590 222982023
421 1912 523778307
745 1803 328256562

result:

ok 4 lines

Test #4:

score: 0
Accepted
time: 487ms
memory: 74412kb

input:

1998
583111238 684686962 60000552 833067795 399706437 80311170 511421309 126675237 578609031 629890589 4721597 505178877 965431576 488092987 110903821 856966035 934194793 831090190 93501498 982251231 221658950 561834845 801921306 125139448 771849922 610370373 625334897 671223646 927123592 441019972 ...

output:

4
21 1273 160152585
700 1573 576757184
1674 1165 958703366
1860 1550 451190886

result:

ok 5 lines

Test #5:

score: 0
Accepted
time: 490ms
memory: 74524kb

input:

2000
717395008 183448326 460843970 942614578 540060179 334668801 284127311 635920935 518435676 579369810 852254297 342132392 390366615 141010330 256825376 585810764 253867889 483289117 141421931 467578626 750184736 801127935 917825514 702243210 954747981 910219404 311930180 11494244 915417963 820983...

output:

5
30 992 620409139
585 130 391404065
910 139 921101622
1292 496 462362602
1579 1776 163902318

result:

ok 6 lines

Test #6:

score: 0
Accepted
time: 495ms
memory: 74504kb

input:

2000
620840546 358023079 309405838 45500223 855686733 508959744 51181469 968972877 537705762 129000719 720633908 884983092 862573877 806580605 252024754 680227415 839963816 519838198 171117282 55679929 677376107 529124099 733179585 20941462 208815360 171000233 981430676 830346879 857681989 863823572...

output:

6
708 1628 497911789
714 1597 924600947
1105 1248 334964897
1209 1517 696980563
1386 1495 446362422
1427 979 599811696

result:

ok 7 lines

Test #7:

score: 0
Accepted
time: 491ms
memory: 74440kb

input:

1999
199111744 347283243 902808247 73043926 77805820 474451787 854137221 262193129 508272500 493604512 479354501 851331944 65939325 676555110 301252826 583818606 235970147 545653095 120576387 825367169 249335412 365652469 200134563 609796865 481733518 594700892 34132902 906919419 939234722 888820985...

output:

7
43 586 272473841
116 1477 158203105
122 1169 761579075
477 1913 749351268
592 583 329485518
662 1400 622650552
1563 148 498180819

result:

ok 8 lines

Test #8:

score: 0
Accepted
time: 491ms
memory: 74512kb

input:

1999
95525499 999471184 616044299 270968642 734163712 806403756 187471687 954208629 738712543 303515182 65192240 341396506 976491262 49950446 564351227 957716298 984366409 189359888 6212233 992086125 956271984 926015141 961718572 628613990 96751949 438018145 639769590 234289696 391888974 135327482 5...

output:

8
279 1620 990329039
882 1321 138573187
1060 836 505053059
1155 214 213425345
1525 906 706361973
1553 569 257967325
1601 1141 127915131
1898 738 215879720

result:

ok 9 lines

Test #9:

score: 0
Accepted
time: 508ms
memory: 74336kb

input:

1996
283446729 904496697 172085277 783069511 818458698 756950687 658910284 99042559 674918562 839616643 448339652 245867480 780073469 684157018 540143866 106204044 613514272 978909042 463995202 654242309 233752461 442383227 477386126 286799884 347610798 141921338 587706626 348419066 962374154 132965...

output:

9
309 1146 916944155
993 1418 998808956
1136 629 476558426
1232 181 439191299
1268 169 491439673
1314 1282 443126632
1415 447 825877434
1541 1779 159693126
1971 1624 228388510

result:

ok 10 lines

Test #10:

score: 0
Accepted
time: 493ms
memory: 74564kb

input:

2000
805314295 257118023 408649013 403270425 561279436 651061472 951531903 607216144 450523182 22974988 674008912 524751540 430060765 578271056 821812595 450493650 896882866 256881145 794394659 634756412 525452196 456872277 895856586 30892092 953830689 157566519 434684214 457301058 431434093 3366527...

output:

10
86 1649 33550286
277 1565 703044124
280 410 767223588
1104 2000 542808584
1165 1837 79761216
1198 360 199585712
1555 178 236137043
1785 1339 983174708
1869 827 135692552
1905 106 851820078

result:

ok 11 lines

Test #11:

score: 0
Accepted
time: 489ms
memory: 74776kb

input:

2000
817831410 837938066 689510456 728988871 925783369 338613646 248460715 797908768 377220868 479079342 718258246 570192472 123025052 702404754 46773852 774152140 719909131 276906493 812767887 453766159 835272763 672918600 773064126 85235335 702812671 696620460 835547337 762745753 759411320 8083624...

output:

11
1 1476 149884243
374 1642 562186259
561 1959 143239748
631 973 100680542
888 1538 726522334
977 526 154377214
1252 262 533011147
1327 1409 392931391
1516 1778 182730233
1782 369 67351156
1855 441 263735571

result:

ok 12 lines

Test #12:

score: -100
Wrong Answer
time: 1ms
memory: 4016kb

input:

1
320856661
676593229

output:

1
1 1 85026155

result:

wrong answer 2nd lines differ - expected: '1 1 914973852', found: '1 1 85026155'