QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#254321#7754. Rolling For Daysucup-team2230#WA 1ms3496kbC++143.2kb2023-11-18 11:04:112023-11-18 11:04:11

Judging History

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

  • [2023-11-18 11:04:11]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3496kb
  • [2023-11-18 11:04:11]
  • 提交

answer

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

using ll=long long;

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using P=pair<int,int>;
using vi=vc<int>;
const uint mod=998244353;
struct mint{
	uint v;
	mint(ll vv=0){s(vv%mod+mod);}
	mint& s(uint vv){
		v=vv<mod?vv:vv-mod;
		return *this;
	}
	mint operator-()const{return mint()-*this;}
	mint& operator+=(const mint&r){return s(v+r.v);}
	mint&operator-=(const mint&r){return s(v+mod-r.v);}
	mint&operator*=(const mint&r){v=(unsigned ll)v*r.v%mod;return *this;}
	mint&operator/=(const mint&r){return *this*=r.inv();}
	mint operator+(const mint&r)const{return mint(*this)+=r;}
	mint operator-(const mint&r)const{return mint(*this)-=r;}
	mint operator*(const mint&r)const{return mint(*this)*=r;}
	mint operator/(const mint&r)const{return mint(*this)/=r;}
	mint pow(ll n)const{
		if(n<0)return inv().pow(-n);
		mint res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	mint inv()const{return pow(mod-2);}
};

void solve(){
	int n,m;
	cin>>n>>m;
	vc<int> A(m),B(m);
	for(int i=0;i<m;i++) cin>>A[i];
	int c=0;
	for(int i=0;i<m;i++){
		cin>>B[i];
		c+=B[i];
	}
	vc<mint> pd(m);
	for(int i=0;i<m;i++){
		pd[i]=1;
		for(int j=0;j<B[i];j++) pd[i]*=A[i]-j;
	}
	vc<mint> fac(n+1),finv(n+1);
	fac[0]=finv[0]=1;
	for(int i=1;i<=n;i++){
		fac[i]=fac[i-1]*i;
		finv[i]=finv[i-1]/i;
	}
	auto C=[&](int a,int b)->mint{
		if(a<b) return 0;
		return fac[a]*finv[b]*finv[a-b];
	};
	vc<int> sa(1<<m),sb(1<<m);
	vc<vc<mint>> way(1<<m,vc<mint>(c+1));
	vc<vc<vc<mint>>> prom(1<<m,vc<vc<mint>>(c+1,vc<mint>(m)));
	for(int S=0;S<1<<m;S++){
		way[S][0]=1;
		for(int i=0;i<m;i++){
			if(S>>i&1){
				sa[S]+=A[i];
				sb[S]+=B[i];
			}
		}
		for(int k=0;k<sb[S];k++){
			way[S][k+1]=way[S][k]*(sa[S]-k);
			for(int i=0;i<m;i++){
				if((S>>i&1)&&k>=B[i]-1){
					prom[S][k+1][i]=way[S^(1<<i)][k-B[i]+1]*C(k,B[i]-1)*pd[i];
					way[S][k+1]-=prom[S][k+1][i];
				}
			}
		}
	}
	vc<vc<mint>> dp(1<<m,vc<mint>(n+1));
	dp[0][0]=1;
	mint ret=0;
	for(int S=0;S<1<<m;S++){
		for(int k=0;k<c;k++){
			if(dp[S][k].v==0) continue;
			int z=k-sb[S],T=((1<<m)-1)^S;
			ret+=dp[S][k]*(n-k)/(n-k-sa[S]+sb[S]);
			mint inv=(mint) 1/(way[T][z]*(sa[T]-z));
			mint rem=1;
			for(int i=0;i<m;i++){
				if(!(S>>i&1)){
					mint p=prom[T][z+1][i]*inv;
					dp[S^(1<<i)][k+1]+=dp[S][k]*p;
					rem-=p;
				}
			}
			dp[S][k+1]+=dp[S][k]*rem;
		}
	}
	cout<<ret.v<<endl;
}
int main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	int T=1;
	while(T--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3392kb

input:

2 2
1 1
1 1

output:

2

result:

ok answer is '2'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3380kb

input:

4 2
2 2
2 1

output:

582309210

result:

ok answer is '582309210'

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 3496kb

input:

5 5
1 1 1 1 1
0 0 0 0 1

output:

1

result:

wrong answer expected '5', found '1'