QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#779640 | #8635. 圆 | yz_ly | 100 ✓ | 56ms | 199144kb | C++14 | 2.2kb | 2024-11-24 20:38:36 | 2024-11-24 20:38:38 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read(){
int f=1,x=0;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-f;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return f*x;
}
inline void write(ll k){
if(k<0){
putchar('-');
k=-k;
}
if(k>9)
write(k/10);
putchar(k%10+'0');
}
/*
qoj8635
首先先钦定选择一个n,然后长度变为n-1,1和n-1为黑色,就是一个序列问题了
一下认为n--
对于一个排列p,f(p)表示第一个位置满足题目要求的,题目要求对于2<=i<=n-1,都有i-1或i或i+1在1~f(p)
显然我们可以对于每个i,求出f(p)=i的方案数,然后最后再除以n!
那么f(p)=i相当于就是在i这个位置满足条件,但是在i-1这个位置不满足
求出g[x]表示大小为x的满足条件的**集合**数量
但么f(p)=i的方案数就是g[x]*x!*(n-x)!-g[x-1]*(x-1)!*(n-x+1)!
现在考虑求解g[x]
设dp[i][j]表示集合大小为i,最大值为j,然后对于每个2<=i<=j都满足为黑色的方案数
很显然j-1,j-2,j-3一定有一个在集合中,所以dp[i][j]=dp[i-1][j-1]+dp[i-1][j-2]+dp[i-1][j-3]
统计g[x]的时候,因为n-2,n-1,n一定有一个在集合中,分类讨论一下就行(因为n一开始是黑色的)
g[x]=dp[x-1][n-1]+dp[x][n-1](n-1在,n在不在都可以)+dp[x-1][n-2]+dp[x][n-2](n-2同理,n-1不在)+dp[x-1][n-3](n必须在)
*/
const ll mod=998244353;
int n;
ll dp[5005][5005],g[5005],jc[5005],inv[5005],ans;
int main(){
n=read();
jc[0]=inv[0]=jc[1]=inv[1]=1;
for(int i=2;i<=n;i++){
jc[i]=jc[i-1]*i%mod;
inv[i]=(mod-mod/i)*inv[mod%i]%mod;
}
for(int i=1;i<=n;i++){
inv[i]=inv[i-1]*inv[i]%mod;
}
n--;
if(n==2){
write(1);
return 0;
}
if(n==3){
write(2);
return 0;
}
dp[1][1]=dp[1][2]=dp[1][3]=1;
for(int i=2;i<=n;i++){
for(int j=1;j<=n;j++){
dp[i][j]=((j>=3?dp[i-1][j-3]:0)+(j>=2?dp[i-1][j-2]:0)+dp[i-1][j-1])%mod;
}
}
for(int i=1;i<=n;i++){
g[i]=(dp[i-1][n-1]+dp[i][n-1]+dp[i-1][n-2]+dp[i][n-2]+dp[i-1][n-3])%mod;
ans=(ans+(g[i]*jc[i]%mod*jc[n-i]%mod-g[i-1]*jc[i-1]%mod*jc[n-i+1]%mod+mod)*i%mod)%mod;
}
write((ans*inv[n]%mod+1)%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: 3556kb
input:
3
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 10
Accepted
time: 0ms
memory: 3640kb
input:
4
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 10
Accepted
time: 0ms
memory: 3592kb
input:
6
output:
299473309
result:
ok 1 number(s): "299473309"
Test #4:
score: 10
Accepted
time: 0ms
memory: 3724kb
input:
10
output:
487238321
result:
ok 1 number(s): "487238321"
Test #5:
score: 10
Accepted
time: 1ms
memory: 7796kb
input:
100
output:
41620761
result:
ok 1 number(s): "41620761"
Test #6:
score: 10
Accepted
time: 2ms
memory: 12112kb
input:
200
output:
208771764
result:
ok 1 number(s): "208771764"
Test #7:
score: 10
Accepted
time: 0ms
memory: 24236kb
input:
500
output:
888621375
result:
ok 1 number(s): "888621375"
Test #8:
score: 10
Accepted
time: 33ms
memory: 191208kb
input:
4798
output:
319137015
result:
ok 1 number(s): "319137015"
Test #9:
score: 10
Accepted
time: 43ms
memory: 199144kb
input:
4999
output:
818467659
result:
ok 1 number(s): "818467659"
Test #10:
score: 10
Accepted
time: 56ms
memory: 199140kb
input:
5000
output:
142907477
result:
ok 1 number(s): "142907477"