QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#540#312339#7754. Rolling For DaysYansuan_HCl275307894aFailed.2024-02-19 19:19:082024-02-19 19:19:09

Details

Extra Test:

Accepted
time: 0ms
memory: 8152kb

input:

4 2
2 2
0 1

output:

2

result:

ok answer is '2'

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#312339#7754. Rolling For Days275307894aAC ✓67ms76624kbC++142.5kb2024-01-23 20:21:582024-01-23 20:21:58

answer

#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=(1<<12)+5,M=1000+5,K=1000+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(263082);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
	Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
	Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
	#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;

int n,m,k,sa[N],sb[N],A[N],B[N];ll frc[N],frv[N],inv[N],C[N][N];
ll dp[N][N][2],g[N][2];
ll mpow(ll x,int y=mod-2){ll ans=1;while(y) y&1&&(ans=ans*x%mod),y>>=1,x=x*x%mod;return ans;}
void Solve(){
	int i,j,h;scanf("%d%d",&n,&m);k=(1<<m);
	for(i=0;i<m;i++) scanf("%d",&A[i]);
	for(i=0;i<m;i++) scanf("%d",&B[i]);
	for(i=1;i<k;i++) sa[i]=sa[i^(i&-i)]+A[__lg(i&-i)];
	for(i=1;i<k;i++) sb[i]=sb[i^(i&-i)]+B[__lg(i&-i)];
	inv[1]=1;for(i=2;i<=n;i++) inv[i]=(mod-inv[mod%i])*(mod/i)%mod;
	for(frc[0]=frv[0]=i=1;i<=n;i++) frc[i]=frc[i-1]*i%mod,frv[i]=frv[i-1]*inv[i]%mod;
	for(i=0;i<=n;i++) for(C[i][0]=j=1;j<=i;j++) C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
	int goal=0;for(i=0;i<m;i++) if(!B[i]) goal|=1<<i;
	dp[goal][0][0]=1;
	for(i=0;i<k;i++){
		Mc(g,dp[i]);
		for(h=1;h<=sb[k-1]-sb[i];h++){
			g[h][0]=(g[h][0]+g[h-1][0]*inv[sa[k-1]-sa[i]-(h-1)])%mod;
			g[h][1]=(g[h][1]+(g[h-1][1]+g[h-1][0]*inv[sa[k-1]-sa[i]-(h-1)]%mod*(sa[k-1]-sb[i]-(h-1))%mod)*inv[sa[k-1]-sa[i]-(h-1)])%mod;
		} 
		for(h=0;h<=sb[k-1]-sb[i];h++) g[h][0]=(g[h][0]+mod-dp[i][h][0])%mod,g[h][1]=(g[h][1]+mod-dp[i][h][1])%mod;
		for(j=0;j<m;j++)if(~i>>j&1){
			for(h=B[j];h<=sb[k-1]-sb[i];h++){
				ll w=C[h-1][B[j]-1]*C[A[j]][B[j]]%mod*frc[B[j]]%mod;
				dp[i|(1<<j)][h-B[j]][0]=(dp[i|(1<<j)][h-B[j]][0]+g[h][0]*w)%mod;
				dp[i|(1<<j)][h-B[j]][1]=(dp[i|(1<<j)][h-B[j]][1]+g[h][1]*w)%mod;
			}
		}
	}	
	gdb(dp[k-1][0][1],dp[k-1][0][0]);
	printf("%lld\n",dp[k-1][0][1]*mpow(dp[k-1][0][0])%mod);
}
int main(){
	int t=1;
	// scanf("%d",&t);
	while(t--) Solve();
	cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}