QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#774153#8635. 圆zyxawa100 ✓119ms199292kbC++141.4kb2024-11-23 12:08:182024-11-23 12:08:18

Judging History

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

  • [2024-11-23 12:08:18]
  • 评测
  • 测评结果:100
  • 用时:119ms
  • 内存:199292kb
  • [2024-11-23 12:08:18]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,a[11],v[11];
ll s,pre[5001]={1},f[5001][5001],g[5001];
const int mod=998244353;
ll qpow(ll a,ll b){
    ll s=1;
    for(;b;b>>=1,a=a*a%mod) if(b&1) s=s*a%mod;
    return s;
}
void dfs(int x){
    if(x==n+1){
        bitset <11> b;
        for(int i=1;i<=n;i++){
            b[a[i]]=b[a[i]%n+1]=b[(a[i]+n-2)%n+1]=1;
            if(b.count()==n) return (void)(s+=i);
        }
    }
    for(int i=1;i<=n;i++) if(!v[i]) a[x]=i,v[i]=1,dfs(x+1),v[i]=0;
}
void dp(){
    for(int i=3;i<n;i++) for(int j=1;j<=i;j++) (f[i][j]+=f[i-1][j-1]+f[i-2][j-1]+f[i-3][j-1])%=mod;
}
int main(){
	scanf("%d",&n);
    for(int i=1;i<=n;i++) pre[i]=pre[i-1]*i%mod;
    if(n<=8) return dfs(1),printf("%lld",s*qpow(pre[n],mod-2)%mod),0;
    memset(f,0,sizeof(f)),f[2][0]=1,dp();
    for(int i=0;i<n;i++) (g[i+1]+=f[n-1][i])%=mod;
    memset(f,0,sizeof(f)),f[2][1]=f[3][1]=f[4][1]=1,dp();
    for(int i=0;i<n;i++) (g[i+1]+=f[n-1][i]+f[n-2][i])%=mod;
    memset(f,0,sizeof(f)),f[2][1]=f[3][1]=1,dp();
    for(int i=0;i<n;i++) (g[i+1]+=f[n-1][i]+f[n-2][i]+f[n-3][i])%=mod;
    memset(f,0,sizeof(f)),f[2][2]=f[3][2]=f[4][2]=1,dp();
    for(int i=0;i<n;i++) (g[i+1]+=f[n-1][i]+f[n-2][i]+f[n-3][i])%=mod;
    for(int i=1;i<=n;i++) (s+=g[i]*pre[i]%mod*pre[n-i])%=mod;
    printf("%lld",(pre[n]*(n+1)-s+mod)%mod*qpow(pre[n],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: 3824kb

input:

3

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

4

output:

2

result:

ok 1 number(s): "2"

Test #3:

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

input:

6

output:

299473309

result:

ok 1 number(s): "299473309"

Test #4:

score: 10
Accepted
time: 59ms
memory: 199176kb

input:

10

output:

487238321

result:

ok 1 number(s): "487238321"

Test #5:

score: 10
Accepted
time: 56ms
memory: 199292kb

input:

100

output:

41620761

result:

ok 1 number(s): "41620761"

Test #6:

score: 10
Accepted
time: 53ms
memory: 199180kb

input:

200

output:

208771764

result:

ok 1 number(s): "208771764"

Test #7:

score: 10
Accepted
time: 47ms
memory: 199184kb

input:

500

output:

888621375

result:

ok 1 number(s): "888621375"

Test #8:

score: 10
Accepted
time: 115ms
memory: 199272kb

input:

4798

output:

319137015

result:

ok 1 number(s): "319137015"

Test #9:

score: 10
Accepted
time: 117ms
memory: 199208kb

input:

4999

output:

818467659

result:

ok 1 number(s): "818467659"

Test #10:

score: 10
Accepted
time: 119ms
memory: 199236kb

input:

5000

output:

142907477

result:

ok 1 number(s): "142907477"