QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#705470 | #7754. Rolling For Days | Appleblue17 | WA | 1ms | 5820kb | C++14 | 2.3kb | 2024-11-02 23:35:46 | 2024-11-02 23:35:56 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1100,M=4400,mod=998244353;
int ksm(int a,int x){
int tot=1;
while(x){
if(x & 1ll) tot=tot*a%mod;
a=a*a%mod;
x>>=1ll;
}
return tot;
}
int mul[N],inv[N];
void init(int lim){
mul[0]=inv[0]=1;
for(int i=1;i<=lim;i++) mul[i]=mul[i-1]*i%mod;
inv[lim]=ksm(mul[lim],mod-2);
for(int i=lim-1;i>=1;i--) inv[i]=inv[i+1]*(i+1)%mod;
}
int C(int m,int n){
if(m<0 || n<0 || m<n) return 0;
return mul[m]*inv[n]%mod*inv[m-n]%mod;
}
void gmod(int &x){
x%=mod;
}
int n,m,alc=1;
int a[N],b[N];
int f[M][N],g[M][N];
int sumc[M],sumb[M];
inline int A(int t,int mac){
return ksm(n-t-sumc[mac],mod-2);
}
inline int B(int t,int mac){
return (n-t)*A(t,mac)%mod;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
init(N-5);
cin>>n>>m;
for(int i=0;i<m;i++) cin>>a[i];
for(int i=0;i<m;i++) cin>>b[i];
for(int mac=0;mac<(1<<m);mac++){
for(int i=0;i<m;i++){
if(!(mac>>i & 1)){
sumc[mac]+=a[i]-b[i];
sumb[mac]+=b[i];
}
}
}
for(int i=0;i<m;i++) alc=alc*mul[a[i]]%mod*inv[a[i]-b[i]]%mod;
f[0][0]=1;
for(int mac=0;mac<(1<<m);mac++){
// cout<<mac<<": "<<endl;
for(int i=n;i>=0;i--){
if(!f[mac][i] && !g[mac][i]) continue;
// cout<<mac<<" "<<i<<": "<<endl;
int t=sumb[mac]+i-1;
if(i){
gmod(f[mac][i-1]+=f[mac][i]*A(t,mac)%mod);
gmod(g[mac][i-1]+=g[mac][i]*A(t,mac)%mod);
gmod(g[mac][i-1]+=f[mac][i]*A(t,mac)%mod*B(t,mac)%mod);
// cout<<" => "<<mac<<" "<<i-1<<" ("<<t<<", "<<A(t,mac)<<", "<<A(t,mac)*B(t,mac)%mod<<")"<<endl;
}
for(int x=0;x<m;x++){
if(mac>>x & 1) continue;
int nmac=mac | (1<<x);
gmod(f[nmac][i+b[x]-1]+=f[mac][i]*C(i+b[x]-1,i)%mod*A(t,nmac)%mod);
gmod(g[nmac][i+b[x]-1]+=g[mac][i]*C(i+b[x]-1,i)%mod*A(t,nmac)%mod);
gmod(g[nmac][i+b[x]-1]+=f[mac][i]*C(i+b[x]-1,i)%mod*A(t,nmac)%mod*B(t,nmac)%mod);
// cout<<t<<" "<<mac<<" "<<n-t-sumc[nmac]<<endl;
// cout<<" => "<<nmac<<" "<<i-1+b[x]<<" ("<<t<<", "<<C(i+b[x]-1,i)<<", "<<A(t,nmac)<<", "<<A(t,nmac)*B(t,nmac)%mod<<")"<<endl;
}
}
// for(int i=0;i<=n;i++) cout<<f[mac][i]<<" ";
// cout<<endl;
// for(int i=0;i<=n;i++) cout<<g[mac][i]<<" ";
// cout<<endl;
}
cout<<g[(1<<m)-1][0]*alc%mod;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5700kb
input:
2 2 1 1 1 1
output:
2
result:
ok answer is '2'
Test #2:
score: 0
Accepted
time: 1ms
memory: 5820kb
input:
4 2 2 2 2 1
output:
582309210
result:
ok answer is '582309210'
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 5728kb
input:
5 5 1 1 1 1 1 0 0 0 0 1
output:
0
result:
wrong answer expected '5', found '0'