QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#296286#7775. 【模板】矩阵快速幂zhouhuanyi0 153ms12180kbC++144.6kb2024-01-02 17:54:242024-01-02 17:54:25

Judging History

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

  • [2024-01-02 17:54:25]
  • 评测
  • 测评结果:0
  • 用时:153ms
  • 内存:12180kb
  • [2024-01-02 17:54:24]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#define M 180000
#define N 300
#define mod 998244353
using namespace std;
const __int128 INF=(__int128)(1e32);
long long read()
{
	char c=0;
	long long sum=0;
	while (c<'0'||c>'9') c=getchar();
	while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
	return sum;
}
int MD2(int x)
{
	return x<0?x+mod:x;
}
struct reads
{
	int num;
	long long data;
};
int S,T,n,m,tk,inv[N+1],md[N+1],num[M+1][N+1];
__int128 DP[N+1][N+1],DSP[N+1][N+1],sk;
string k;
vector<reads>E[N+1];
struct frac
{
	__int128 x;
	int y;
	bool operator < (const frac &t)const
	{
		return x*t.y<y*t.x;
	}
	bool operator >= (const frac &t)const
	{
		return x*t.y>=y*t.x;
	}
	bool operator == (const frac &t)const
	{
		return x*t.y==y*t.x;
	}
	bool operator != (const frac &t)const
	{
		return x*t.y!=y*t.x;
	}
};
frac operator + (frac a,frac b)
{
	return (frac){a.x*b.y+a.y*b.x,a.y*b.y};
}
frac operator - (frac a,frac b)
{
	return (frac){a.x*b.y-a.y*b.x,a.y*b.y};
}
struct rds
{
	frac a,b;
	bool operator < (const rds &t)const
	{
		return a!=t.a?a<t.a:b<t.b;
	}
	bool operator == (const rds &t)const
	{
		return a==t.a&&b==t.b;
	}
	bool operator != (const rds &t)const
	{
		return a!=t.a||b!=t.b;
	}
};
rds zero=(rds){(frac){0,1},(frac){0,1}},inf=(rds){(frac){1,0},(frac){1,0}},dp[2][N+1],delta[N+1][N+1];
rds operator + (rds x,rds y)
{
	if (x==inf||y==inf) return inf;
	x.a=x.a+y.a,x.b=x.b+y.b;
	if (x.b>=(frac){sk,1}) x.a=x.a+(frac){1,1},x.b=x.b-(frac){sk,1};
	return x;
}
rds operator - (rds x,rds y)
{
	if (x==inf) return inf;
	x.a=x.a-y.a,x.b=x.b-y.b;
	if (x.b<(frac){0,1}) x.a=x.a-(frac){1,1},x.b=x.b+(frac){sk,1};
	return x;
}
rds change(rds x)
{
	__int128 d;
	if (x.b.x>=0) d=x.b.x/x.b.y/sk,x.a=x.a+(frac){d,1},x.b=x.b-(frac){d*sk,1};
	else d=(-x.b.x+x.b.y*sk-1)/x.b.y/sk,x.a=x.a-(frac){d,1},x.b=x.b+(frac){d*sk,1};
	return x;
}
int get(rds x)
{
	if (x==inf) return -1;
	__int128 res=(x.a.x%mod)*inv[x.a.y]*tk+(x.b.x%mod)*inv[x.b.y];
	res=MD2(res%mod);
	return res;
}
int main()
{
	int x,y,d,op;
	long long z;
	inv[1]=1;
	for (int i=2;i<=N;++i) inv[i]=MD2(-1ll*(mod/i)*inv[mod%i]%mod);
	S=read(),T=read();
	for (int qt=1;qt<=T;++qt)
	{
		n=read(),m=read(),sk=tk=0,cin>>k;
		for (int i=2;i<=n;++i) md[i]=0;
		for (int i=1;i<=n;++i) E[i].clear();
		for (int i=0;i<k.length();++i)
		{
			sk=min(sk*10+k[i]-'0',INF),tk=(tk*10ll+k[i]-'0')%mod;
			for (int j=2;j<=n;++j) md[j]=(md[j]*10ll+k[i]-'0')%j;
		}
		for (int i=1;i<=m;++i) x=read(),y=read(),z=read(),E[x].push_back((reads){y,z});
		for (int i=1;i<=n;++i)
		{
			for (int j=0;j<=n;++j)
				for (int k=1;k<=n;++k)
					DSP[j][k]=INF;
			DSP[0][i]=0;
			for (int j=1;j<=n;++j)
				for (int k=1;k<=n;++k)
					for (int t=0;t<E[k].size();++k)
						DSP[j][E[k][t].num]=min(DSP[j][E[k][t].num],DSP[j-1][k]+E[k][t].data);
			for (int j=0;j<=n;++j) DP[j][i]=DSP[j][i];
		}
		for (int i=1;i<=n;++i)
			for (int j=n*n-i+1;j<=n*n;++j)
				num[i][j%i]=j;
		if (sk<=((n*n)<<1))
		{
			for (int i=1;i<=n;++i) dp[0][i]=inf;
			dp[0][1]=zero,op=0;
			for (int i=1;i<=sk;++i)
			{
				op^=1;
				for (int j=1;j<=n;++j) dp[op][j]=inf;
				for (int j=1;j<=n;++j)
					for (int k=0;k<E[j].size();++k)
						dp[op][E[j][k].num]=min(dp[op][E[j][k].num],dp[op^1][j]+change((rds){(frac){0,1},(frac){E[j][k].data,1}}));
			}
			for (int i=1;i<=n;++i) printf("%d ",get(dp[op][i]));
			puts("");
		}
		else
		{
			for (int i=0;i<n;++i)
				for (int j=1;j<=n;++j)
					delta[i][j]=inf;
			dp[0][1]=zero,op=0;
			for (int i=1;i<=n*n;++i)
			{
				op^=1;
				for (int j=1;j<=n;++j) dp[op][j]=inf;
				for (int j=1;j<=n;++j)
					for (int k=0;k<E[j].size();++k)
						dp[op][E[j][k].num]=min(dp[op][E[j][k].num],dp[op^1][j]+change((rds){(frac){0,1},(frac){E[j][k].data,1}}));
				if (i>=n*n-n+1)
				{
					for (int j=1;j<=n;++j)
						if (dp[op][j]!=inf)
						{
							for (int k=2;k<=n;++k)
								if (DP[k][j]!=INF)
									d=num[k][((md[k]-i)%k+k)%k],delta[n*n-d][j]=min(delta[n*n-d][j],dp[op][j]+change((rds){(frac){DP[k][j],k},(frac){-DP[k][j]*(d+i),k}}));
						}
				}
			}
			op=0;
			for (int i=1;i<=n;++i) dp[op][i]=delta[0][i];
			for (int i=n*n-1;i>=0;--i)
			{
				op^=1;
				for (int j=1;j<=n;++j) dp[op][j]=inf;
				if (i>=n*n-n+1)
				{
					for (int j=1;j<=n;++j) dp[op][j]=delta[n*n-i][j];
				}
				for (int j=1;j<=n;++j)
					for (int k=0;k<E[j].size();++k)
						dp[op][E[j][k].num]=min(dp[op][E[j][k].num],dp[op^1][j]+change((rds){(frac){0,1},(frac){E[j][k].data,1}}));
			}
			for (int i=1;i<=n;++i) printf("%d ",get(dp[op][i]));
			puts("");
		}
	}
	return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 10
Accepted
time: 66ms
memory: 12180kb

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:

395495792 395495781 395495783 395495839 395495793 395495789 395495754 395495832 395495845 395495755 395495823 395495773 395495753 395495800 395495782 395495763 395495847 395495761 395495794 395495791 395495786 395495821 395495798 395495765 395495772 395495770 395495757 395495819 395495843 395495828 ...

result:

ok 100 numbers

Test #2:

score: -10
Wrong Answer
time: 153ms
memory: 10740kb

input:

1
1
100 200 998858598565699977
89 61 596014036562538539
89 84 921297646113897322
61 84 946923234442637386
61 35 641628261157284465
84 35 979893473772327497
84 78 700172488379560611
35 78 963617193748189613
35 54 951598888254521423
78 54 680825215292116806
78 21 737055858973038555
54 21 7491794406112...

output:

627335121 302898219 240025702 634507919 406677636 263120157 414836894 397178128 993122330 445020775 424191039 796538849 104561682 827171189 645384881 380155354 214313356 473493420 754590333 454059607 579186899 898724120 950766249 624228476 28562202 471976424 103569775 854050440 293807530 881401728 9...

result:

wrong answer 1st numbers differ - expected: '590375247', found: '627335121'

Subtask #2:

score: 0
Runtime Error

Test #7:

score: 0
Runtime Error

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:


result:


Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Skipped

Dependency #1:

0%

Subtask #6:

score: 0
Skipped

Dependency #3:

0%