QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#591081#8635. 圆rotcar07100 ✓138ms70544kbC++20960b2024-09-26 13:59:342024-09-26 13:59:41

Judging History

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

  • [2024-09-26 13:59:41]
  • 评测
  • 测评结果:100
  • 用时:138ms
  • 内存:70544kb
  • [2024-09-26 13:59:34]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
constexpr int maxn=5005;
typedef long long ll;
constexpr ll mod=998244353;
int f[maxn][maxn];
ll qpow(ll a,int b=mod-2){
    ll ans=1;
    while(b){
        if(b&1) ans=ans*a%mod;
        a=a*a%mod,b>>=1;
    }
    return ans;
}
ll fac[maxn],inv[maxn];
inline void reduce(int &x){(x>=mod)&&(x-=mod);}
inline ll C(int n,int m){if(n<m||n<0||m<0) return 0;return fac[n]*inv[m]%mod*inv[n-m]%mod;}
int main(){
    int n;cin>>n;n--;
    if(n==2) return puts("1"),0;
    for(int i=fac[0]=1;i<=n;i++) fac[i]=fac[i-1]*i%mod;
    inv[n]=qpow(fac[n]);
    for(int i=n;i>=1;i--) inv[i-1]=inv[i]*i%mod;
    f[0][0]=1;   
    for(int i=0;i<=n;i++)
    for(int j=0;j<=i;j++){
        for(int k:{1,2,3}) reduce(f[i+k][j+1]+=f[i][j]);
    }
    ll ans=0;
    for(int i=1;i<=n;i++) ans+=(C(n,i)-f[n][i]-f[n-1][i]-f[n-2][i])%mod*fac[i]%mod*inv[n]%mod*fac[n-i]%mod;
    cout<<(ans%mod+2+mod)%mod<<'\n';
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

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

input:

3

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

4

output:

2

result:

ok 1 number(s): "2"

Test #3:

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

input:

6

output:

299473309

result:

ok 1 number(s): "299473309"

Test #4:

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

input:

10

output:

487238321

result:

ok 1 number(s): "487238321"

Test #5:

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

input:

100

output:

41620761

result:

ok 1 number(s): "41620761"

Test #6:

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

input:

200

output:

208771764

result:

ok 1 number(s): "208771764"

Test #7:

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

input:

500

output:

888621375

result:

ok 1 number(s): "888621375"

Test #8:

score: 10
Accepted
time: 129ms
memory: 66484kb

input:

4798

output:

319137015

result:

ok 1 number(s): "319137015"

Test #9:

score: 10
Accepted
time: 138ms
memory: 70544kb

input:

4999

output:

818467659

result:

ok 1 number(s): "818467659"

Test #10:

score: 10
Accepted
time: 130ms
memory: 70476kb

input:

5000

output:

142907477

result:

ok 1 number(s): "142907477"