QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#561814 | #7875. Queue Sorting | huaxiamengjin | WA | 125ms | 12528kb | C++14 | 1.7kb | 2024-09-13 10:59:03 | 2024-09-13 10:59:04 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int NN=998244353;
int p[100100],ni[100100];
int n;
int mi(int x,int y){
int sum=1;
for (;y;y>>=1,x=x*x%NN)
if(y&1)sum=sum*x%NN;
return sum;
}
int c(int x,int y){
if(x<0||y<0||x<y)return 0;
return p[x]*ni[y]%NN*ni[x-y]%NN;
}
int ans=1;
int f[1010][1010][2];
int a[100100];
signed main(){
cin>>n;
int N=1e5;
p[0]=1;
for (int i=1;i<=N;i++)
p[i]=p[i-1]*i%NN;
ni[N]=mi(p[N],NN-2);
for (int i=N;i;i--)
ni[i-1]=ni[i]*i%NN;
for (int i=1;i<=n;i++)
cin>>a[i];
int sum=0;
// for (int i=0;i<a[1];i++)
// f[1][i][1]=1;
f[0][0][1]=1;
for (int i=1;i<=n;i++){
sum+=a[i];
for (int k=0;k<sum;k++)
f[i][k][0]=f[i-1][k][0],f[i][k][1]=f[i-1][k][1];
if(a[i]>=1)
for (int k=0;k<sum-a[i];k++){
if(k+1<sum)f[i][k+1][0]=(f[i][k+1][0]+f[i-1][k][1])%NN;
for (int kk=k+1;kk<sum;kk++)
f[i][kk][0]=(f[i][kk][0]+f[i-1][k][0]*c(kk-k-1,0))%NN;
if(1==a[i])continue;
int kk=sum-(a[i]-1);
f[i][kk][1]=(f[i][kk][1]+f[i-1][k][0]*c(kk-k-1,0))%NN;
}
for (int j=2;j<=a[i];j++){
for (int k=0;k<sum-a[i];k++){
for (int kk=k+j;kk<sum;kk++){
f[i][kk][0]=(f[i][kk][0]+f[i-1][k][0]*c(kk-k-1,j-1))%NN;
f[i][kk][0]=(f[i][kk][0]+f[i-1][k][1]*c(kk-k-2,j-2))%NN;
}
if(j==a[i])continue;
int kk=sum-(a[i]-j);
f[i][kk][1]=(f[i][kk][1]+f[i-1][k][0]*c(kk-k-1,j-1))%NN;
f[i][kk][1]=(f[i][kk][1]+f[i-1][k][1]*c(kk-k-2,j-2))%NN;
}
}
// for (int j=0;j<=sum;j++)
// cout<<f[i][j][0]<<" "<<f[i][j][1]<<" "<<i<<" "<<j<<"\n";
// cout<<"*******\n";
for (int j=0;j<sum;j++)
ans=(ans+f[i][j][0])%NN;
}
cout<<ans<<"\n";
} //16796
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 6032kb
input:
4 1 1 1 1
output:
14
result:
ok 1 number(s): "14"
Test #2:
score: -100
Wrong Answer
time: 125ms
memory: 12528kb
input:
300 0 5 2 2 1 0 3 2 2 5 2 1 1 2 1 3 2 3 2 0 0 0 0 1 2 2 3 0 2 2 3 2 0 2 3 0 6 0 0 2 0 1 3 2 1 1 1 3 4 0 1 0 4 1 1 1 1 1 1 2 3 2 1 2 3 2 3 0 5 3 3 2 0 1 1 0 2 1 1 2 0 0 2 1 1 3 2 2 1 2 1 3 0 3 0 1 2 2 0 5 0 2 2 0 0 0 1 2 1 4 2 1 1 0 3 0 2 0 3 1 1 2 0 2 1 1 0 2 0 1 2 2 3 3 1 1 1 1 0 1 3 3 1 0 2 2 4 2 ...
output:
1
result:
wrong answer 1st numbers differ - expected: '507010274', found: '1'