QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#619515#8635. 圆hewanying100 ✓54ms169540kbC++141.2kb2024-10-07 14:31:442024-10-07 14:31:48

Judging History

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

  • [2024-10-07 14:31:48]
  • 评测
  • 测评结果:100
  • 用时:54ms
  • 内存:169540kb
  • [2024-10-07 14:31:44]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

const int N=5e3+5,H=998244353;
int n,f[N][N][3],ans=0,fac[N],ifac[N];

int adc(int a,int b){return a+b>=H?a+b-H:a+b;}
int dec(int a,int b){return a<b?a-b+H:a-b;}
int mul(int a,int b){return 1ll*a*b%H;}
void add(int &a,int b){a=adc(a,b);}
void del(int &a,int b){a=dec(a,b);}

int qpow(int a,int b=H-2){
  int res=1;
  while(b){if(b&1) res=mul(res,a);a=mul(a,a),b>>=1;}
  return res;
}

void init(){
  fac[0]=1;
  for(int i=1;i<N;i++) fac[i]=mul(fac[i-1],i);
  ifac[N-1]=qpow(fac[N-1]);
  for(int i=N-1;i>=1;i--) ifac[i-1]=mul(ifac[i],i);
}

int binom(int n,int m){
  if(m<0||n<m) return 0;
  return mul(fac[n],mul(ifac[m],ifac[n-m]));
}


int main(){
  ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  cin>>n;init();

  f[1][1][0]=1;
  for(int i=2;i<=n;i++){
	for(int j=1;j<=i;j++) for(int k=0;k<3;k++) add(f[i][j][0],f[i-1][j-1][k]);
	for(int j=0;j<i;j++) for(int k=1;k<3;k++) add(f[i][j][k],f[i-1][j][k-1]);
  }

  for(int i=0;i<=n;i++){
	int res=0;
    for(int j=0;j<3;j++) add(res,f[n][i][j]);
	for(int j=0;j<2;j++) add(res,f[n-1][i][j]);
	add(res,f[n-2][i][0]);
    add(ans,mul(qpow(binom(n,i)),dec(binom(n,i),res)));
  }
  cout<<ans;
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

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

input:

3

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

4

output:

2

result:

ok 1 number(s): "2"

Test #3:

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

input:

6

output:

299473309

result:

ok 1 number(s): "299473309"

Test #4:

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

input:

10

output:

487238321

result:

ok 1 number(s): "487238321"

Test #5:

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

input:

100

output:

41620761

result:

ok 1 number(s): "41620761"

Test #6:

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

input:

200

output:

208771764

result:

ok 1 number(s): "208771764"

Test #7:

score: 10
Accepted
time: 3ms
memory: 7196kb

input:

500

output:

888621375

result:

ok 1 number(s): "888621375"

Test #8:

score: 10
Accepted
time: 39ms
memory: 157740kb

input:

4798

output:

319137015

result:

ok 1 number(s): "319137015"

Test #9:

score: 10
Accepted
time: 40ms
memory: 169476kb

input:

4999

output:

818467659

result:

ok 1 number(s): "818467659"

Test #10:

score: 10
Accepted
time: 54ms
memory: 169540kb

input:

5000

output:

142907477

result:

ok 1 number(s): "142907477"