QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#336707 | #7754. Rolling For Days | lefy | WA | 1ms | 8112kb | C++14 | 2.7kb | 2024-02-24 19:51:40 | 2024-02-24 19:51:41 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e3+10,M=1<<12,mod=998244353;
int a[15],b[15],val[M],vb[M];
int c[N][N];
ll C(int x,int y){
if(x<0||y<0||x<y)return 0;
return c[x][y];
}
ll inv[N],Inv[N],sinv[N];
ll f[N][M],g[N][M],sf[N][M],sg[N][M],sg2[N][M],fac[N],G[N][M];
ll getmul(int l,int r){
if(l>r||r<=0)return 1;
if(!l)return fac[r];
return fac[r]*Inv[l-1]%mod;
}
ll getsum(int l,int r){
if(l>r||r<=0)return 0;
if(!l)return sinv[r];
return (sinv[r]-sinv[l-1]+mod)%mod;
}
int main() {
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)scanf("%d",&a[i]);
int st=0;
for(int i=1;i<=m;i++){
scanf("%d",&b[i]);
if(!b[i])st|=1<<i-1;
}
for(int s=0;s<(1<<m);s++)for(int i=1;i<=m;i++)if(s&(1<<i-1)){
val[s]+=a[i]-b[i],vb[s]+=b[i];
// cout<<i<<"\n";
}
for(int i=0;i<=n;i++){
c[i][0]=1;
for(int j=1;j<=i;j++)c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
}
sinv[0]=Inv[0]=Inv[1]=inv[1]=1;
sinv[1]=2;fac[1]=fac[0]=1;
for(int i=2;i<=n;i++){
fac[i]=fac[i-1]*i%mod;
sinv[i]=inv[i]=inv[mod%i]*(mod-mod/i)%mod,Inv[i]=Inv[i-1]*inv[i]%mod;
sinv[i]=(sinv[i]+sinv[i-1])%mod;
}
g[0][st]=1;
for(int i=0;i<=vb[(1<<m)-1];i++)for(int s=0;s<(1<<m);s++){
for(int x=1;x<=m;x++)if((s&(1<<x-1))&&b[x]){
ll sum=C(i-1-vb[s-(1<<x-1)],b[x]-1)*getmul(a[x]-b[x]+1,a[x])%mod;
g[i][s]+=sg[i-1][s-(1<<x-1)]*sum%mod;
// if(i==1&&s==2)cout<<g[i][s]<<"\n";
f[i][s]+=sf[i-1][s-(1<<x-1)]*sum%mod;
sum=sum*val[s-(1<<x-1)]%mod*sg2[i-1][s-(1<<x-1)]%mod;
// cout<<sg2[i-1][s-(1<<x-1)]<<"\n";
f[i][s]+=sum;
g[i][s]%=mod,f[i][s]%=mod;
}
if(i==n)continue;
sg[i][s]=((i?sg[i-1][s]:0)+g[i][s])%mod;
sg[i][s]=sg[i][s]*inv[n+1-i-1-val[s]]%mod;
// if(i){
// G[i][s]=(G[i-1][s]+g[i][s])%mod;
// if(i<n)G[i][s]=G[i][s]*inv[n+1-i-1,val[s]]%mod;
ll tmp=inv[n-val[s]-i];
if(i)
sg2[i][s]=(sg2[i-1][s]*tmp%mod+sg[i-1][s]*tmp%mod)%mod;
// sg2[i][s]=((i?sg2[i-1][s]:0)+sg[i][s]*inv[]%mod)%mod;
sg2[i][s]=(sg2[i][s]+g[i][s]*tmp%mod*tmp%mod)%mod;
// }
// sg2[i][s]=((i?sg2[i-1][s]:0)+g[i][s]*inv[n-])
sf[i][s]=((i?sf[i-1][s]:0)+f[i][s])%mod;
sf[i][s]=sf[i][s]*inv[n+1-i-1-val[s]]%mod;
// cout<<i<<" "<<s<<"\n";
// cout<<f[i][s]<<" "<<g[i][s]<<"\n";
}
// cout<<f[vb[(1<<m)-1]][(1<<m)-1]<<"\n";
printf("%lld",(f[vb[(1<<m)-1]][(1<<m)-1]+vb[(1<<m)-1])%mod);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 8008kb
input:
2 2 1 1 1 1
output:
2
result:
ok answer is '2'
Test #2:
score: 0
Accepted
time: 0ms
memory: 8112kb
input:
4 2 2 2 2 1
output:
582309210
result:
ok answer is '582309210'
Test #3:
score: 0
Accepted
time: 0ms
memory: 5944kb
input:
5 5 1 1 1 1 1 0 0 0 0 1
output:
5
result:
ok answer is '5'
Test #4:
score: 0
Accepted
time: 1ms
memory: 6020kb
input:
4 4 1 1 1 1 1 1 1 0
output:
831870299
result:
ok answer is '831870299'
Test #5:
score: 0
Accepted
time: 1ms
memory: 6020kb
input:
5 2 4 1 2 1
output:
598946616
result:
ok answer is '598946616'
Test #6:
score: -100
Wrong Answer
time: 1ms
memory: 6108kb
input:
5 2 3 2 3 1
output:
83187035
result:
wrong answer expected '482484776', found '83187035'