QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#591029#8635. 圆ship2077100 ✓15ms3972kbC++23859b2024-09-26 13:43:122024-09-26 13:43:12

Judging History

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

  • [2024-09-26 13:43:12]
  • 评测
  • 测评结果:100
  • 用时:15ms
  • 内存:3972kb
  • [2024-09-26 13:43:12]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
constexpr int M=5005,mod=998244353;
int n,ans,now,fac[M],ifac[M],dp[4][M];
int qpow(int x,int n){
	int s=1; while (n){
		if (n&1) s=1ll*s*x%mod;
		x=1ll*x*x%mod; n>>=1;
	} return s;
}
int rdc(int x){return x>=mod?x-mod:x;}
int main(){
    scanf("%d",&n); dp[0][0]=1;
	for (int i=fac[0]=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%mod;
	for (int i=1;i<=n;i++){
        dp[now=now?now-1:3][0]=0;
		for (int j=1;j<=i;j++){
			dp[now][j]=dp[(now+1)&3][j-1];
			if (i>=2) dp[now][j]=rdc(dp[now][j]+dp[(now+2)&3][j-1]);
			if (i>=3) dp[now][j]=rdc(dp[now][j]+dp[(now+3)&3][j-1]);
		}
    }
	for (int i=1;i<=n;i++){
        const int coef=i==n?fac[n]:((0ll+mod-fac[n-i])%mod*fac[i-1]%mod);
        ans=(ans+1ll*dp[now][i]*coef)%mod;
    }
	printf("%lld\n",1ll*ans*qpow(fac[n-1],mod-2)%mod);
	return 0;
}

詳細信息


Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 0ms
memory: 3900kb

input:

3

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 10
Accepted
time: 0ms
memory: 3900kb

input:

4

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 10
Accepted
time: 0ms
memory: 3916kb

input:

6

output:

299473309

result:

ok 1 number(s): "299473309"

Test #4:

score: 10
Accepted
time: 0ms
memory: 3892kb

input:

10

output:

487238321

result:

ok 1 number(s): "487238321"

Test #5:

score: 10
Accepted
time: 0ms
memory: 3972kb

input:

100

output:

41620761

result:

ok 1 number(s): "41620761"

Test #6:

score: 10
Accepted
time: 0ms
memory: 3960kb

input:

200

output:

208771764

result:

ok 1 number(s): "208771764"

Test #7:

score: 10
Accepted
time: 1ms
memory: 3784kb

input:

500

output:

888621375

result:

ok 1 number(s): "888621375"

Test #8:

score: 10
Accepted
time: 13ms
memory: 3892kb

input:

4798

output:

319137015

result:

ok 1 number(s): "319137015"

Test #9:

score: 10
Accepted
time: 15ms
memory: 3972kb

input:

4999

output:

818467659

result:

ok 1 number(s): "818467659"

Test #10:

score: 10
Accepted
time: 15ms
memory: 3896kb

input:

5000

output:

142907477

result:

ok 1 number(s): "142907477"