QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#423163#2834. NonsenseTianTian2008TL 147ms199760kbC++141.2kb2024-05-27 21:22:022024-05-27 21:22:03

Judging History

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

  • [2024-05-27 21:22:03]
  • 评测
  • 测评结果:TL
  • 用时:147ms
  • 内存:199760kb
  • [2024-05-27 21:22:02]
  • 提交

answer

#include <iostream>
#include <cstdio>
#define mod 998244353
using namespace std;
typedef long long ll;
ll n,m,x,y,q,fac[5010],inv[5010];
ll ksm(ll x,ll y) {
	ll res=1;
	while(y) {
		if(y&1) res=res*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return res;
}
void init() {
	fac[0]=1;
	for(int i=1;i<=m;++i) fac[i]=fac[i-1]*(n-i+2)%mod;
	ll pro=1;
	for(int i=1;i<=m;++i) pro=pro*i%mod;
	inv[m]=ksm(pro,mod-2);
	for(int i=m;i>=1;--i) inv[i-1]=inv[i]*i%mod;
}
inline ll binom(ll x) {return fac[x]*inv[x]%mod;}
namespace sol1 {
	ll f[5010][5010];
	void work() {
		ll div=ksm(x-y,mod-2);
		for(int i=0;i<=m;++i)
			for(int j=0;j<=m;++j) {
				if(i==0) f[i][j]=-binom(j)*ksm(y,n+1-j)%mod;
				else f[i][j]=-f[i-1][j];
				if(j==0) f[i][j]+=binom(i)*ksm(x,n+1-i)%mod;
				else f[i][j]+=f[i][j-1];
				f[i][j]=f[i][j]*div%mod;
			}
		ll a,b;
		while(q--) {
			scanf("%lld%lld",&a,&b);
			printf("%lld\n",(f[a][b]+mod)%mod);
		}
	}
}
namespace sol2 {
	void work() {
		ll a,b;
		while(q--) {
			scanf("%lld%lld",&a,&b);
			printf("%lld\n",ksm(x,n-a-b)*binom(a+b+1)%mod);
		}
	}
}
int main() {
	while(scanf("%lld%lld%lld%lld",&n,&x,&y,&q)!=EOF) {
		m=min(n,5000ll)+1;
		init();
		if(x!=y) sol1::work();
		else sol2::work();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 8124kb

input:

3 1 2 2
1 1
1 2
100 2 3 1
1 1

output:

6
1
866021789

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 147ms
memory: 199760kb

input:

1000000000 0 1 1
1000 1000
2 0 0 1
1 1
2 998244352 998244352 1
1 1

output:

381781645
1
1

result:

ok 3 lines

Test #3:

score: 0
Accepted
time: 143ms
memory: 199748kb

input:

1000000000 998244351 998244352 1
5000 5000

output:

663228915

result:

ok single line: '663228915'

Test #4:

score: -100
Time Limit Exceeded

input:

105886041 9 3 200
4 3
5 1
1 1
4 1
3 3
1 5
2 1
1 5
2 1
1 5
3 3
4 4
2 1
4 4
1 2
5 2
5 2
2 5
4 5
3 3
4 3
1 4
3 1
5 4
5 3
5 2
5 3
3 3
1 3
4 3
2 3
3 5
1 3
3 5
2 3
4 4
3 4
5 5
2 3
1 1
3 3
4 2
1 4
4 5
2 3
1 5
2 2
4 2
5 5
2 1
4 3
3 3
3 1
2 1
2 5
1 1
4 4
1 5
1 5
3 1
3 2
2 2
4 1
5 5
3 4
2 2
2 1
5 3
5 3
2 2
1 ...

output:

721440251
764408668
442427888
914530150
115811774
360614503
666106268
360614503
666106268
360614503
115811774
166867820
666106268
166867820
985976063
717651702
717651702
405340273
435048189
115811774
721440251
719754512
660548079
998056822
165742634
717651702
165742634
115811774
407319461
721440251
...

result: