QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#449795#7775. 【模板】矩阵快速幂32162535 651ms8972kbC++143.2kb2024-06-21 17:22:542024-06-21 17:22:55

Judging History

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

  • [2024-06-21 17:22:55]
  • 评测
  • 测评结果:35
  • 用时:651ms
  • 内存:8972kb
  • [2024-06-21 17:22:54]
  • 提交

answer

#include<cstdio>
#include<algorithm>
const int N=303,M=603,Vm=90003,mod=998244353;
typedef long long ll;const ll Mod=mod;
typedef __int128 LL;const LL inf=1e37;
//typedef ll LL;const LL inf=1e18;
char sk[107];int U[M],V[M],n,m;ll W[M];
struct frc{LL vl;int c;};
inline bool operator<(const frc&x,const frc&y){return x.vl*y.c<y.vl*x.c;}
inline bool operator==(const frc&x,const frc&y){return x.vl*y.c==y.vl*x.c;}
template<class T>inline void ckmn(T&a,const T&b){if(b<a)a=b;}
LL f[N][N],tmpr[N][N];
frc disr[N];int modv[N];
LL readmx(char*s,LL mx){LL ans=0;for(;*s;++s)ans=std::min(ans*10+((*s)^48),inf);return ans;}
ll readmod(char*s,ll mod){ll ans=0;for(;*s;++s)ans=(ans*10+((*s)^48))%mod;return ans;}
void chkr(int u,int t){if(tmpr[t][u]<inf)ckmn(disr[u],(frc){tmpr[t][u],t});}
struct pii{LL A,vl;int c;};LL K;pii g[N][N];
inline pii addp(const pii&a,ll b){return {a.A,a.vl+(LL)b*a.c,a.c};}
inline bool operator<(const pii&x,const pii&y)
{
	LL va=x.A*y.c-y.A*x.c,vb=y.vl*x.c-x.vl*y.c;
	if(!va)return vb>0;
	if(va>0)
	{
		if(vb<=0||K==inf)return 0;
		return K<=(vb-1)/va;
	}
	va=-va;vb=-vb;
	if(vb<=0||K==inf)return 1;
	return K>vb/va;
}
int main()
{
//	freopen("huan.in","r",stdin);
//	freopen("huan.out","w",stdout);
	int T;scanf("%*d%d",&T);
	while(T--)
	{
		scanf("%d%d%s",&n,&m,sk);K=readmx(sk,inf);
		for(int i=1;i<=m;++i)scanf("%d%d%lld",U+i,V+i,W+i);
		
		int mx=n*n;if(K<=mx+mx)mx=K;
		
		f[0][1]=0;for(int i=2;i<=n;++i)f[0][i]=inf;
		for(int i=1;i<=mx;++i)
		{
			LL*cur=f[i%n],*las=f[(i-1)%n];std::fill(cur+1,cur+n+1,inf);
			for(int j=1;j<=m;++j)ckmn(cur[V[j]],las[U[j]]+W[j]);
		}
		
		if(K==mx){for(int i=1;i<=n;++i)printf("%d ",f[mx%n][i]<inf?(int)(f[mx%n][i]%mod):-1);puts("");continue;}
		
		for(int u=1;u<=n;++u)
		{
			disr[u]={1,0};
			std::fill(tmpr[1]+1,tmpr[1]+n+1,inf);
			for(int j=1;j<=m;++j)if(U[j]==u)ckmn<LL>(tmpr[1][V[j]],W[j]);
			chkr(u,1);
			for(int i=2;i<n;++i)
			{
				std::fill(tmpr[i]+1,tmpr[i]+n+1,inf);
				for(int j=1;j<=m;++j)ckmn(tmpr[i][V[j]],tmpr[i-1][U[j]]+W[j]);
				chkr(u,i);
			}
		}
		
		for(int i=1;i<=n;++i)modv[i]=readmod(sk,i);
		
		#define prt(x) (ll)((x)/(LL)1e18),(ll)((x)%(LL)1e18)
		for(int i=0;i<n;++i)std::fill(g[i]+1,g[i]+n+1,(pii){1,0,0});
		for(int u=1;u<=n;++u)if(disr[u].c)
		{
			const int r=disr[u].c;
//			printf("u=%d r=%d\n",u,r);
			for(int ii=0;ii<n;++ii)if(f[(n-ii)%n][u]<inf)
			{
				int jj=-(modv[r]-2*n*n+ii)%r;if(jj<0)jj+=r;
				LL disii=f[(n-ii)%n][u],vfs=disii*r-(2*n*n-ii-jj)*disr[u].vl;
				ckmn(g[(n-jj)%n][u],(pii){disr[u].vl,vfs,r});
//				printf("u=%d ii=%d jj=%d vfs=%lld|%018lld r=%d\n",u,ii,jj,prt(vfs),r);
			}
		}
		
		for(int i=n*n-1;~i;--i)
		{
			pii*cur=g[i%n],*las=g[(i+1)%n];
			if(i<=n*n-n)std::fill(cur+1,cur+n+1,(pii){1,0,0});
			for(int j=1;j<=m;++j)ckmn(cur[V[j]],addp(las[U[j]],W[j]));
		}
		
		for(int i=1;i<=n;++i)
			if(!g[0][i].c)printf("-1 ");
			else
			{
				const int r=g[0][i].c;
//				printf("g[0][i].vl=%lld|%018lld\n",prt(g[0][i].vl));
				ll mmod=Mod*r,avl=g[0][i].A%mmod;
//				printf("avlt=%lld|%018lld\n",prt(avl));
				int ans=(((LL)readmod(sk,mmod)*avl+g[0][i].vl)%mmod)/r;
				printf("%d ",ans);
			}
		puts("");
	}
	return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 11ms
memory: 8480kb

input:

1
1
100 101 899539897889989959
74 35 910832669819965536
35 85 910832669819965536
85 88 910832669819965536
88 30 910832669819965536
30 58 910832669819965536
58 60 910832669819965536
60 34 910832669819965536
34 8 910832669819965536
8 67 910832669819965536
67 89 910832669819965536
89 32 910832669819965...

output:

314204566 314204555 314204557 314204513 314204567 314204563 314204528 314204606 314204519 314204529 314204597 314204547 314204527 314204574 314204556 314204537 314204521 314204535 314204568 314204565 314204560 314204595 314204572 314204539 314204546 314204544 314204531 314204593 314204517 314204602 ...

result:

wrong answer 1st numbers differ - expected: '395495792', found: '314204566'

Subtask #2:

score: 15
Accepted

Test #7:

score: 15
Accepted
time: 620ms
memory: 8936kb

input:

2
1
300 598 8179377797889487867988994778539839593376697796496698959964978969
1 2 977880533270721156
2 1 977880533270721156
2 3 977880533270721156
3 2 977880533270721156
3 4 977880533270721156
4 3 977880533270721156
4 5 977880533270721156
5 4 977880533270721156
5 6 977880533270721156
6 5 977880533270...

output:

-1 313446627 -1 313436465 -1 313426303 -1 313416141 -1 313405979 -1 313395817 -1 313385655 -1 313375493 -1 313365331 -1 313355169 -1 313345007 -1 313334845 -1 313324683 -1 313314521 -1 313304359 -1 313294197 -1 313284035 -1 313273873 -1 313263711 -1 313253549 -1 313243387 -1 313233225 -1 313223063 -...

result:

ok 300 numbers

Test #8:

score: 0
Accepted
time: 622ms
memory: 8860kb

input:

2
1
300 598 9284745978997975899894787995823975998931999649789777849997467689
1 2 946893593823801228
2 1 946893593823801228
2 3 761384824565158999
3 2 761384824565158999
3 4 642721010434291429
4 3 642721010434291429
4 5 936762490761905983
5 4 936762490761905983
5 6 785485094128355256
6 5 785485094128...

output:

-1 613575042 -1 416269325 -1 387291578 -1 980556870 -1 491367967 -1 221793101 -1 191668085 -1 356035653 -1 428450970 -1 964149805 -1 511723806 -1 423081033 -1 947783979 -1 325795034 -1 115778037 -1 86469999 -1 111666379 -1 386592847 -1 223100328 -1 381885001 -1 23001328 -1 84087613 -1 517941041 -1 9...

result:

ok 300 numbers

Test #9:

score: 0
Accepted
time: 624ms
memory: 8848kb

input:

2
1
300 598 7877597936928589688789427798322599997378688496694695996269389696
1 2 866412995946330002
2 1 866412995946330002
2 3 866412995946330002
3 2 866412995946330002
3 4 866412995946330002
4 3 866412995946330002
4 5 866412995946330002
5 4 866412995946330002
5 6 866412995946330002
6 5 866412995946...

output:

708443714 -1 708438498 -1 708433282 -1 708428066 -1 708422850 -1 708417634 -1 708412418 -1 708407202 -1 708401986 -1 708396770 -1 708391554 -1 708386338 -1 708381122 -1 708375906 -1 708370690 -1 708365474 -1 708360258 -1 708355042 -1 708349826 -1 708344610 -1 708339394 -1 708334178 -1 708328962 -1 7...

result:

ok 300 numbers

Test #10:

score: 0
Accepted
time: 623ms
memory: 8936kb

input:

2
1
300 598 74686617152792803
1 2 920869599353968456
2 1 920869599353968456
2 3 920869599353968456
3 2 920869599353968456
3 4 920869599353968456
4 3 920869599353968456
4 5 920869599353968456
5 4 920869599353968456
5 6 920869599353968456
6 5 920869599353968456
6 7 920869599353968456
7 6 9208695993539...

output:

-1 537762223 -1 537752459 -1 537742695 -1 537732931 -1 537723167 -1 537713403 -1 537703639 -1 537693875 -1 537684111 -1 537674347 -1 537664583 -1 537654819 -1 537645055 -1 537635291 -1 537625527 -1 537615763 -1 537605999 -1 537596235 -1 537586471 -1 537576707 -1 537566943 -1 537557179 -1 537547415 -...

result:

ok 300 numbers

Test #11:

score: 0
Accepted
time: 432ms
memory: 8192kb

input:

2
40
120 238 7647979978895986883485788838258737687493899697379499657768989994
1 2 940784508355800649
2 1 940784508355800649
2 3 940784508355800649
3 2 940784508355800649
3 4 940784508355800649
4 3 940784508355800649
4 5 940784508355800649
5 4 940784508355800649
5 6 940784508355800649
6 5 94078450835...

output:

383704267 -1 383701847 -1 383699427 -1 383697007 -1 383694587 -1 383692167 -1 383689747 -1 383687327 -1 383684907 -1 383682487 -1 383680067 -1 383677647 -1 383675227 -1 383672807 -1 383670387 -1 383667967 -1 383665547 -1 383663127 -1 383660707 -1 383658287 -1 383655867 -1 383653447 -1 383651027 -1 3...

result:

ok 3146 numbers

Test #12:

score: 0
Accepted
time: 510ms
memory: 8088kb

input:

2
5697
96 190 8939398847797777979859997957885578698889795859699765658877967896
1 2 940438543633266209
2 1 940438543633266209
2 3 940438543633266209
3 2 940438543633266209
3 4 940438543633266209
4 3 940438543633266209
4 5 940438543633266209
5 4 940438543633266209
5 6 940438543633266209
6 5 9404385436...

output:

57861585 -1 57859879 -1 57858173 -1 57856467 -1 57854761 -1 57853055 -1 57851349 -1 57849643 -1 57847937 -1 57846231 -1 57844525 -1 57842819 -1 57841113 -1 57839407 -1 57837701 -1 57835995 -1 57834289 -1 57832583 -1 57830877 -1 57829171 -1 57827465 -1 57825759 -1 57824053 -1 57822347 -1 57820641 -1 ...

result:

ok 77560 numbers

Subtask #3:

score: 20
Accepted

Dependency #2:

100%
Accepted

Test #13:

score: 20
Accepted
time: 644ms
memory: 8956kb

input:

3
1
300 600 9479768887366979469968967538414386738799799469768954967897479478
235 118 610005418879451235
118 235 610005418879451235
229 118 610005418879451235
118 229 610005418879451235
36 235 610005418879451235
235 36 610005418879451235
265 229 610005418879451235
229 265 610005418879451235
24 36 610...

output:

494335567 494326423 494248699 494244127 494344711 494326423 494358427 494335567 494362999 494303563 494344711 494294419 494344711 494344711 494239555 494285275 494298991 494335567 494294419 494221267 494344711 494353855 494289847 494404147 494298991 494294419 494230411 494253271 494230411 494367571 ...

result:

ok 300 numbers

Test #14:

score: 0
Accepted
time: 615ms
memory: 8972kb

input:

3
1
300 600 5776769948887747678764766855867697879888989838869789796489887868
283 274 755089058915384251
274 283 755089058915384251
244 283 888168172221533892
283 244 888168172221533892
282 283 888128579062348874
283 282 888128579062348874
40 244 889268402435235212
244 40 889268402435235212
182 282 9...

output:

176036896 694748344 -1 -1 -1 -1 600566244 -1 -1 -1 -1 -1 718827887 436968623 37585847 -1 -1 504374914 -1 633560024 856820739 157217839 -1 306684175 563519989 184280158 797877375 730487505 574440187 141621833 108771729 627363885 6744545 -1 216801629 -1 -1 -1 -1 635875737 -1 -1 -1 172431836 -1 7053201...

result:

ok 300 numbers

Test #15:

score: 0
Accepted
time: 615ms
memory: 8880kb

input:

3
1
300 600 7799975936983268595994769498698386999688649798971695584484797589
213 87 992365484371550852
87 213 992365484371550852
292 213 992365484371550852
213 292 992365484371550852
125 292 992365484371550852
292 125 992365484371550852
32 213 992365484371550852
213 32 992365484371550852
231 32 9923...

output:

-1 352350370 352353940 352300390 -1 352257550 352357510 -1 -1 -1 -1 -1 352303960 -1 -1 -1 352328950 -1 -1 352353940 -1 -1 352346800 352325380 352368220 -1 352368220 352325380 -1 352339660 352368220 352278970 352343230 -1 -1 -1 -1 -1 -1 -1 -1 -1 352261120 -1 352314670 352311100 -1 -1 352371790 352275...

result:

ok 300 numbers

Test #16:

score: 0
Accepted
time: 651ms
memory: 8860kb

input:

3
1
300 600 108915867328921644
78 120 915329174369582501
120 78 915329174369582501
166 120 915329174369582501
120 166 915329174369582501
24 120 915329174369582501
120 24 915329174369582501
2 24 915329174369582501
24 2 915329174369582501
146 2 915329174369582501
2 146 915329174369582501
266 2 9153291...

output:

796638071 796675403 796642219 796633923 796625627 796613183 796621479 796625627 796600739 796609035 796617331 796625627 796617331 796621479 796625627 796600739 796629775 796579999 796675403 796613183 796625627 796617331 796604887 796671255 796629775 796675403 796658811 796596591 796600739 796625627 ...

result:

ok 300 numbers

Test #17:

score: 0
Accepted
time: 497ms
memory: 7212kb

input:

3
48
120 240 7737895866885999885898998578585996398987747885374658446818863997
97 35 804386118934281915
35 97 804386118934281915
59 35 804386118934281915
35 59 804386118934281915
111 35 804386118934281915
35 111 804386118934281915
62 111 804386118934281915
111 62 804386118934281915
54 59 804386118934...

output:

-1 817576206 817570296 -1 -1 -1 -1 817576206 817576206 -1 -1 -1 817577388 -1 817575024 817573842 817576206 -1 -1 -1 -1 817569114 817573842 -1 817578570 817579752 817578570 -1 -1 -1 817575024 -1 817570296 -1 -1 -1 817577388 817576206 -1 -1 817571478 817575024 -1 -1 817571478 817571478 817572660 81757...

result:

ok 3516 numbers

Test #18:

score: 0
Accepted
time: 567ms
memory: 4868kb

input:

3
6185
96 192 9829599865896867589898965976864696579885564749989527653879744756
27 20 972718145577806019
20 27 972718145577806019
11 27 972718145577806019
27 11 972718145577806019
44 11 972718145577806019
11 44 972718145577806019
70 11 972718145577806019
11 70 972718145577806019
57 44 972718145577806...

output:

432574626 -1 432575632 -1 432569596 432580662 432581668 -1 432577644 432579656 432583680 432571608 432582674 432582674 -1 432570602 -1 432576638 -1 432584686 -1 -1 -1 -1 432582674 432575632 -1 432576638 432573620 -1 432583680 432570602 -1 432580662 432577644 -1 -1 432578650 -1 432575632 -1 432572614...

result:

ok 83979 numbers

Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Skipped

Dependency #1:

0%

Subtask #6:

score: 0
Skipped

Dependency #3:

100%
Accepted

Dependency #4:

0%