QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#641162 | #8635. 圆 | hhy0613 | 100 ✓ | 53ms | 101476kb | C++14 | 1.0kb | 2024-10-14 18:56:02 | 2024-10-14 18:56:03 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=5005,mod=998244353;
int dp[N][N],g[N],fact[N];
int qpow(int a,int b){
int ans=1;
while(b>0){
if(b&1) ans=1ll*ans*a%mod;
b>>=1;
a=1ll*a*a%mod;
}
return ans;
}
int main(){
ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
int n;
cin >> n;
fact[0]=1;
for(int i=1;i<N;++i) fact[i]=1ll*fact[i-1]*i%mod;
if(n==3){
cout << "1\n";
return 0;
}
if(n==4){
cout << "2\n";
return 0;
}
--n;
dp[0][0]=1;
dp[0][1]=1;
dp[0][2]=1;
dp[1][1]=1;
dp[1][2]=2;
dp[2][2]=1;
dp[1][3]=3;
dp[2][3]=3;
dp[3][3]=1;
for(int i=1;i<=n;++i){
for(int j=4;j<=n;++j) dp[i][j]=((dp[i-1][j-1]+dp[i-1][j-2])%mod+dp[i-1][j-3])%mod;
}
for(int i=1;i<=n;++i) g[i]=((dp[i-1][n-1]+dp[i-1][n-2])%mod+dp[i-1][n-3])%mod;
int ans=1;
for(int x=1;x<=n;++x) ans=(ans+1ll*x*qpow(fact[n],mod-2)%mod*((1ll*g[x]*fact[x]%mod*fact[n-x]%mod+mod-1ll*g[x-1]*fact[x-1]%mod*fact[n-x+1]%mod)%mod)%mod)%mod;
cout << ans << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 0ms
memory: 3688kb
input:
3
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 10
Accepted
time: 0ms
memory: 5720kb
input:
4
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 10
Accepted
time: 1ms
memory: 5696kb
input:
6
output:
299473309
result:
ok 1 number(s): "299473309"
Test #4:
score: 10
Accepted
time: 0ms
memory: 3772kb
input:
10
output:
487238321
result:
ok 1 number(s): "487238321"
Test #5:
score: 10
Accepted
time: 1ms
memory: 6132kb
input:
100
output:
41620761
result:
ok 1 number(s): "41620761"
Test #6:
score: 10
Accepted
time: 1ms
memory: 7860kb
input:
200
output:
208771764
result:
ok 1 number(s): "208771764"
Test #7:
score: 10
Accepted
time: 0ms
memory: 14260kb
input:
500
output:
888621375
result:
ok 1 number(s): "888621375"
Test #8:
score: 10
Accepted
time: 47ms
memory: 97480kb
input:
4798
output:
319137015
result:
ok 1 number(s): "319137015"
Test #9:
score: 10
Accepted
time: 52ms
memory: 101476kb
input:
4999
output:
818467659
result:
ok 1 number(s): "818467659"
Test #10:
score: 10
Accepted
time: 53ms
memory: 101472kb
input:
5000
output:
142907477
result:
ok 1 number(s): "142907477"