QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#591021#8635. 圆ship2077100 ✓15ms4036kbC++23981b2024-09-26 13:40:452024-09-26 13:40:46

Judging History

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

  • [2024-09-26 13:40:46]
  • 评测
  • 测评结果:100
  • 用时:15ms
  • 内存:4036kb
  • [2024-09-26 13:40:45]
  • 提交

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);
	for (int i=fac[0]=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%mod;
    dp[0][0]=1;
	for (int i=1;i<=n;i++){
        if (now) now--; else now=3;
        dp[now][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=0;i<=n;i++) cerr<<dp[now][i]<<", ";cerr<<endl;
	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;
    }
    if (ans<0) ans+=mod;
	printf("%lld\n",1ll*ans*qpow(fac[n-1],mod-2)%mod);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

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

input:

3

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

4

output:

2

result:

ok 1 number(s): "2"

Test #3:

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

input:

6

output:

299473309

result:

ok 1 number(s): "299473309"

Test #4:

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

input:

10

output:

487238321

result:

ok 1 number(s): "487238321"

Test #5:

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

input:

100

output:

41620761

result:

ok 1 number(s): "41620761"

Test #6:

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

input:

200

output:

208771764

result:

ok 1 number(s): "208771764"

Test #7:

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

input:

500

output:

888621375

result:

ok 1 number(s): "888621375"

Test #8:

score: 10
Accepted
time: 14ms
memory: 4036kb

input:

4798

output:

319137015

result:

ok 1 number(s): "319137015"

Test #9:

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

input:

4999

output:

818467659

result:

ok 1 number(s): "818467659"

Test #10:

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

input:

5000

output:

142907477

result:

ok 1 number(s): "142907477"