QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#640185#9448. Product MatrixzhouhuanyiWA 2445ms485292kbC++147.0kb2024-10-14 08:50:522024-10-14 08:50:54

Judging History

This is the latest submission verdict.

  • [2024-10-14 08:50:54]
  • Judged
  • Verdict: WA
  • Time: 2445ms
  • Memory: 485292kb
  • [2024-10-14 08:50:52]
  • Submitted

answer

#include<iostream>
#include<cstdio>
#include<vector>
#define N 1000000
#define Z 6
#define K 20
#define M 1048576
#define g 3
#define mod 1000000007
#define mod1 167772161
#define mod2 469762049
#define mod3 998244353
#define delta1 29562547
#define delta2 115990628
#define delta3 575867115
using namespace std;
const __int128 sl=(__int128)(mod1)*mod2*mod3;
const int inv2=(mod+1)>>1;
int read()
{
	char c=0;
	int sum=0;
	while (c<'0'||c>'9') c=getchar();
	while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
	return sum;
}
int fast_pow(int a,int b)
{
	int res=1,mul=a;
	while (b)
	{
		if (b&1) res=1ll*res*mul%mod;
		mul=1ll*mul*mul%mod,b>>=1;
	}
	return res;
}
int fast_pows(int a,int b,int p)
{
	int res=1,mul=a;
	while (b)
	{
		if (b&1) res=1ll*res*mul%p;
		mul=1ll*mul*mul%p,b>>=1;
	}
	return res;
}
void Adder(int &x,int d)
{
	x=x+d>=mod?x+d-mod:x+d;
	return;
}
void Adder2(int &x,int d)
{
	x=x+d<0?x+d+mod:x+d;
	return;
}
int MD(int x)
{
	return x>=mod?x-mod:x;
}
int MD2(int x)
{
	return x<0?x+mod:x;
}
int MDS1(int x)
{
	return x>=mod1?x-mod1:x;
}
int MDS2(int x)
{
	return x>=mod2?x-mod2:x;
}
int MDS3(int x)
{
	return x>=mod3?x-mod3:x;
}
int MDS4(int x)
{
	return x<0?x+mod1:x;
}
int MDS5(int x)
{
	return x<0?x+mod2:x;
}
int MDS6(int x)
{
	return x<0?x+mod3:x;
}
int n,m,length,ta[Z+1][Z+1],tb[Z+1][Z+1],num[M+1],rev[M+1],pw[N+1],invpw[N+1],spw[N+1],sdelta[N+1],res[N+1],ans[N+1];
struct reads
{
	int d1,d2,d3;
};
reads zero,tA[M+1],tB[M+1],tC[M+1],tD[M+1],wn[M+1],wn2[M+1],W[M+1],sinv[M+1];
reads operator + (reads a,reads b)
{
	return (reads){MDS1(a.d1+b.d1),MDS2(a.d2+b.d2),MDS3(a.d3+b.d3)};
}
reads operator - (reads a,reads b)
{
	return (reads){MDS4(a.d1-b.d1),MDS5(a.d2-b.d2),MDS6(a.d3-b.d3)};
}
reads operator * (reads a,reads b)
{
	return (reads){1ll*a.d1*b.d1%mod1,1ll*a.d2*b.d2%mod2,1ll*a.d3*b.d3%mod3};
}
int calc(reads x)
{
	return (((__int128)(1ll*x.d1*delta1%mod1)*mod2*mod3+(__int128)(1ll*x.d2*delta2%mod2)*mod1*mod3+(__int128)(1ll*x.d3*delta3%mod3)*mod1*mod2)%sl)%mod;
}
struct reads2
{
	unsigned long long d1,d2,d3;
};
reads2 tdelta[M+1];
reads2 operator * (reads2 a,reads b)
{
	return (reads2){a.d1*b.d1,a.d2*b.d2,a.d3*b.d3};
}
reads2 operator + (reads2 a,reads2 b)
{
	return (reads2){a.d1+b.d1,a.d2+b.d2,a.d3+b.d3};
}
reads2 get_mod(reads2 a)
{
	return (reads2){a.d1%mod1,a.d2%mod2,a.d3%mod3};
}
void NTT_3m(int limit,reads *s,int type)
{
	reads2 sw;
	for (int i=1;i<limit;++i) rev[i]=(rev[i>>1]>>1)|((i&1)?limit>>1:0);
	for (int i=0;i<limit;++i) tdelta[rev[i]]=(reads2){s[i].d1,s[i].d2,s[i].d3};
	if (type==1)
	{
		for (int i=2,d;i<=limit;i<<=1)
		{
			d=num[M/i];
			for (int j=0;j<(i>>1);++j) W[j]=wn[j<<d];
			for (int j=0;j+i-1<limit;j+=i)
				for (int k=j;k<(j|(i>>1));++k)
					sw=get_mod(tdelta[k|(i>>1)]*W[k-j]),tdelta[k|(i>>1)]=tdelta[k]+(reads2){mod1-sw.d1,mod2-sw.d2,mod3-sw.d3},tdelta[k]=tdelta[k]+sw;
			if (i>=(1<<17)||i==limit)
			{
				for (int j=0;j<=limit;++j) tdelta[j]=get_mod(tdelta[j]);
			}
		}
		for (int i=0;i<limit;++i) s[i]=(reads){tdelta[i].d1,tdelta[i].d2,tdelta[i].d3};
	}
	else
	{
		for (int i=2,d;i<=limit;i<<=1)
		{
			d=num[M/i];
			for (int j=0;j<(i>>1);++j) W[j]=wn2[j<<d];
			for (int j=0;j+i-1<limit;j+=i)
				for (int k=j;k<(j|(i>>1));++k)
					sw=get_mod(tdelta[k|(i>>1)]*W[k-j]),tdelta[k|(i>>1)]=tdelta[k]+(reads2){mod1-sw.d1,mod2-sw.d2,mod3-sw.d3},tdelta[k]=tdelta[k]+sw;
			if (i>=(1<<17)||i==limit)
			{
				for (int j=0;j<=limit;++j) tdelta[j]=get_mod(tdelta[j]);
			}
		}
		for (int i=0;i<=limit;++i) tdelta[i]=get_mod(tdelta[i]*sinv[limit]),s[i]=(reads){tdelta[i].d1,tdelta[i].d2,tdelta[i].d3};
	}
	return;
}
vector<int>operator + (vector<int>a,vector<int>b)
{
	if (a.size()<b.size()) swap(a,b);
	for (int i=0;i<b.size();++i) Adder(a[i],b[i]);
	return a;
}
struct matrix
{
	int d[Z+1][Z+1];
};
matrix c,e,zro,delta[N+1],A[N+1],B[N+1];
matrix operator * (matrix a,matrix b)
{
	c=zro;
	for (int k=1;k<=n;++k)
		for (int i=1;i<=n;++i)
			for (int j=1;j<=n;++j)
				Adder(c.d[i][j],1ll*a.d[i][k]*b.d[k][j]%mod);
	return c;
}
struct rds
{
	vector<int>a;
	vector<int>b;
};
rds operator * (rds x,rds y)
{
	rds z;
	z.a.resize(x.a.size()+y.a.size()-1),z.b.resize(x.a.size()+y.b.size()-1);
	if (min(x.a.size(),y.a.size())<=50)
	{
		for (int i=0;i<x.a.size();++i)
		{
			for (int j=0;j<y.a.size();++j) Adder(z.a[i+j],1ll*x.a[i]*y.a[j]%mod);
			for (int j=0;j<y.b.size();++j) Adder(z.b[i+j],1ll*x.a[i]*y.b[j]%mod);
		}
		for (int i=0;i<x.b.size();++i)
			for (int j=0;j<y.a.size();++j)
				Adder(z.b[i+j],1ll*x.b[i]*y.a[j]%mod);
		return z;
	}
	int limit=1;
	while (limit<=x.a.size()+y.a.size()) limit<<=1;
	for (int i=0;i<=limit;++i) tA[i]=tB[i]=zero;
	for (int i=0;i<x.a.size();++i) tA[i]=(reads){x.a[i]%mod1,x.a[i]%mod2,x.a[i]%mod3};
	for (int i=0;i<x.b.size();++i) tB[i]=(reads){x.b[i]%mod1,x.b[i]%mod2,x.b[i]%mod3};
	for (int i=0;i<y.a.size();++i) tC[i]=(reads){y.a[i]%mod1,y.a[i]%mod2,y.a[i]%mod3};
	for (int i=0;i<y.b.size();++i) tD[i]=(reads){y.b[i]%mod1,y.b[i]%mod2,y.b[i]%mod3};
	NTT_3m(limit,tA,1),NTT_3m(limit,tB,1),NTT_3m(limit,tC,1),NTT_3m(limit,tD,1);
	for (int i=0;i<=limit;++i) tC[i]=tA[i]*tD[i]+tB[i]*tC[i],tA[i]=tA[i]*tB[i];
	NTT_3m(limit,tA,-1),NTT_3m(limit,tC,-1);
	for (int i=0;i<z.a.size();++i) z.a[i]=calc(tA[i]);
	for (int i=0;i<z.b.size();++i) z.b[i]=calc(tC[i]);
	return z;
}
rds solve(int l,int r)
{
	if (l==r) return (rds){{MD2(-pw[l]),1},{res[l]}};
	else
	{
		int mid=(l+r)>>1;
		return solve(l,mid)*solve(mid+1,r);
	}
}
int main()
{
	int rst;
	reads rest,w;
	rds d;
	pw[0]=invpw[0]=sdelta[0]=spw[0]=1;
	for (int i=1;i<=N;++i) pw[i]=2ll*pw[i-1]%mod,invpw[i]=1ll*invpw[i-1]*inv2%mod,sdelta[i]=1ll*sdelta[i-1]*fast_pow(MD2(pw[i]-1),mod-2)%mod,spw[i]=1ll*spw[i-1]*pw[i-1]%mod;
	sinv[1]=(reads){1,1,1};
	for (int i=2;i<=M;i<<=1) num[i]=++length,sinv[i]=(reads){fast_pows(i,mod1-2,mod1),fast_pows(i,mod2-2,mod2),fast_pows(i,mod3-2,mod3)};
	w=(reads){fast_pows(g,(mod1-1)/M,mod1),fast_pows(g,(mod2-1)/M,mod2),fast_pows(g,(mod3-1)/M,mod3)},rest=(reads){1,1,1};
	for (int i=0;i<(M>>1);++i,rest=rest*w) wn[i]=rest;
	w=(reads){fast_pows(g,(mod1-1)/M*(M-1),mod1),fast_pows(g,(mod2-1)/M*(M-1),mod2),fast_pows(g,(mod3-1)/M*(M-1),mod3)},rest=(reads){1,1,1};
	for (int i=0;i<(M>>1);++i,rest=rest*w) wn2[i]=rest;
	n=read(),m=read();
	for (int i=1;i<=n;++i) e.d[i][i]=1;
	for (int i=1;i<=n;++i)
		for (int j=1;j<=n;++j)
			ta[i][j]=read();
	for (int i=1;i<=n;++i)
		for (int j=1;j<=n;++j)
			tb[i][j]=read();
	for (int i=0;i<=(m<<1)-1;++i)
		for (int j=1;j<=n;++j)
			for (int k=1;k<=n;++k)
				delta[i].d[j][k]=MD(1ll*ta[j][k]*pw[i]%mod+tb[j][k]);
	A[m]=B[m-1]=e;
	for (int i=m-1;i>=0;--i) A[i]=delta[i]*A[i+1];
	for (int i=m;i<=(m<<1)-1;++i) B[i]=B[i-1]*delta[i];
	rst=fast_pow(spw[m],mod-2);
	for (int i=0;i<=m;++i)
	{
		res[i]=1ll*(A[i]*B[m-1+i]).d[1][1]*sdelta[i]%mod*sdelta[m-i]%mod*spw[m-i]%mod*rst%mod;
		if ((m-i)&1) res[i]=MD2(-res[i]);
	}
	d=solve(0,m);
	for (int i=0;i<=m;++i) printf("%d\n",d.b[i]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 138ms
memory: 61160kb

input:

2 2
1 2
3 4
2 0
1 2

output:

4
8
14

result:

ok 3 number(s): "4 8 14"

Test #2:

score: 0
Accepted
time: 142ms
memory: 59284kb

input:

4 10
520471651 866160932 848899741 650346545
756377215 402412491 920748640 255249004
371442152 93295238 350011159 679848583
528399020 957465601 22001245 407745834
363922864 418156995 324388560 611306817
671914474 323815171 638034305 796072406
765823638 254662378 175686978 123364350
786531344 3967179...

output:

890467395
623743197
432365684
555543126
145580696
550546744
959254454
836036617
945090197
106843161
866547396

result:

ok 11 numbers

Test #3:

score: 0
Accepted
time: 134ms
memory: 59120kb

input:

6 23
101670804 457970042 521121852 851881468 298366530 271962368
881900040 161849089 409608300 527884448 898980182 538728808
624037110 955334452 644656371 684645088 612630196 365375437
135489465 838789241 374389562 238291227 977400760 496900790
921432977 606711088 740916866 405856539 822796504 19906...

output:

18827363
93291123
549166310
727345493
212460686
835043567
382235992
234331494
126083178
340949995
361327462
549134620
481914498
34075195
89718312
945811332
898724999
944812555
123616792
779724718
995912550
188150326
361531843
801483934

result:

ok 24 numbers

Test #4:

score: 0
Accepted
time: 146ms
memory: 61156kb

input:

1 1
850150743
201109093

output:

201109093
850150743

result:

ok 2 number(s): "201109093 850150743"

Test #5:

score: 0
Accepted
time: 146ms
memory: 61156kb

input:

2 1
838417478 568611358
348881562 259739663
684020320 849564252
7622864 342206654

output:

684020320
838417478

result:

ok 2 number(s): "684020320 838417478"

Test #6:

score: 0
Accepted
time: 150ms
memory: 59048kb

input:

3 1
662626648 989629820 447555531
504352706 537983436 981463385
633227491 799236931 264904138
510263941 30128899 644015027
642994715 674480107 494744466
113567281 686079810 29940910

output:

510263941
662626648

result:

ok 2 number(s): "510263941 662626648"

Test #7:

score: 0
Accepted
time: 130ms
memory: 59336kb

input:

4 1
698286849 108948691 370848972 861145616
308345962 492551526 837788523 735191312
813226172 232618279 121444192 64535733
172831199 302337428 438860400 394173985
968865126 421436111 675658174 967155308
675554219 293733850 793671127 135966551
267239055 24766491 712336945 25719396
621692331 201339445...

output:

968865126
698286849

result:

ok 2 number(s): "968865126 698286849"

Test #8:

score: 0
Accepted
time: 138ms
memory: 61128kb

input:

5 1
346030494 348813388 950014436 351718810 389309705
819298156 278971731 721089919 34315191 136072724
977938439 445268765 725373786 92574089 40828378
81532232 217673244 195836050 397811725 905085770
76139672 852963918 237501084 582369241 723129091
510859036 368205749 459903015 796344358 178557942
9...

output:

510859036
346030494

result:

ok 2 number(s): "510859036 346030494"

Test #9:

score: 0
Accepted
time: 130ms
memory: 61384kb

input:

6 1
269535050 794196606 377757516 516758696 739552010 15300040
523493270 110895534 879184568 693817999 162914386 60443980
122215232 3304271 774066505 279824412 19713957 620002064
784042807 447807660 446909111 377390001 200803795 138848111
379227514 56112455 336718555 609352443 37005361 252594923
861...

output:

552563198
269535050

result:

ok 2 number(s): "552563198 269535050"

Test #10:

score: -100
Wrong Answer
time: 2445ms
memory: 485292kb

input:

1 500000
706182264
736952709

output:

136785447
598171483
96462658
472354421
404862907
475924222
148930663
498696839
489981145
308419189
727368108
54242267
208175786
519697179
768083844
961093019
793039984
921416012
467463861
524881114
996932667
241341532
779764129
373657669
858801722
603019682
46218460
401647997
362158223
235076068
810...

result:

wrong answer 1st numbers differ - expected: '892233922', found: '136785447'