UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#202073#3521. Draw a CardLittle091003952ms67248kbC++112.3kb2024-02-11 12:16:542024-02-11 13:12:02

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define mem(x) memset(x,0,sizeof(x))
#define endl "\n"
#define printYes cout << "Yes\n"
#define printYES cout << "YES\n"
#define printNo cout << "No\n"
#define printNO cout << "NO\n"
#define lowbit(x) ((x)&(-(x)))
#define pb push_back
#define mkp make_pair
#define pii pair<ll,ll>
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define rep(i,j,k) for (int i=(j);i<=(k);i++)
#define per(i,j,k) for (int i=(j);i>=(k);i--)
#define pcnt(x) __builtin_popcount(x)
mt19937 rnd(time(0));
template<class T>void chkmin(T&x,T y){x=min(x,y);}
template<class T>void chkmax(T&x,T y){x=max(x,y);}

const ll inf=1000000000000000000; 
//const ll inf=1000000000;
const ll mod=998244353;
//const ll mod=1000000007;

const int N=1e5+5;
int n,m; 
ll jc[N+10],inv[N+10];
ll ksm(ll x,ll y)
{
	ll res=1;
	while (y)
	{
		if (y&1) res=res*x%mod;
		x=x*x%mod,y>>=1;
	}
	return res;
}
inline ll C(ll x,ll y)
{
	if (y<0||y>x) return 0;
	return jc[x]*inv[y]%mod*inv[x-y]%mod;
}
void init()
{
	jc[0]=1;
	for (int i=1;i<=N;i++) jc[i]=jc[i-1]*i%mod;
	inv[N]=ksm(jc[N],mod-2);
	for (int i=N-1;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod;
}

int a[15],b[15];
pii dp[1005][1<<12];
pii dfs(int x,int s)
{
	if (s==(1<<m)-1)
	{
		if (x==0) return mkp(1,0);
		return mkp(0,0);
	}
	if (dp[x][s].fi!=-1) return dp[x][s];
	ll num=0,res=0,sa=0,sb=-x;
	rep(i,0,m-1)
	{
		if (s&(1<<i)) sa+=a[i]-b[i];
		else sb+=a[i];
	}
	if (sb<=0) return mkp(0,0);
	ll val=(sa+sb)*ksm(sb,mod-2)%mod;
	ll W=ksm(sb,mod-2);
	rep(i,0,m-1)
	{
		if (s&(1<<i)) continue;
		if (b[i]>x+1) continue;
		//ll val=0;
		//rep(j,1,b[i]) (val+=(sa+j)*ksm(j,mod-2))%=mod;
		ll w=W*C(x,b[i]-1)%mod*C(a[i],b[i])%mod*jc[b[i]]%mod;
		pii z=dfs(x+1-b[i],s|(1<<i));
		(num+=w*z.fi)%=mod;
		(res+=w*(z.se+z.fi*val%mod))%=mod;
	}
	pii z=dfs(x+1,s);
	(num+=z.fi*W)%=mod;
	(res+=(z.se+z.fi*val%mod)*W)%=mod;
	return dp[x][s]=mkp(num,res);
}


int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> m;
	rep(i,0,m-1) cin >> a[i];
	rep(i,0,m-1) cin >> b[i];
	init();
	memset(dp,-1,sizeof(dp));
	int s=0;
	rep(i,0,m-1) if (b[i]==0) s|=(1<<i);
	pii res=dfs(0,s);
	cout << res.se*ksm(res.fi,mod-2)%mod;//res.fi
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 15ms
memory: 67168kb

input:

300 3
94 108 98
21 6 24

output:

429514955

result:

ok 1 number(s): "429514955"

Test #2:

score: 0
Accepted
time: 23ms
memory: 67152kb

input:

300 3
114 87 99
17 73 0

output:

373612158

result:

ok 1 number(s): "373612158"

Test #3:

score: 0
Accepted
time: 15ms
memory: 67168kb

input:

300 2
160 140
83 43

output:

607272497

result:

ok 1 number(s): "607272497"

Subtask #2:

score: 20
Accepted

Test #4:

score: 20
Accepted
time: 15ms
memory: 67140kb

input:

20 6
2 5 2 3 2 6
2 1 2 0 1 2

output:

102890512

result:

ok 1 number(s): "102890512"

Test #5:

score: 0
Accepted
time: 14ms
memory: 67136kb

input:

20 10
2 1 1 2 2 3 2 2 0 5
0 1 0 1 0 1 1 2 0 0

output:

349385555

result:

ok 1 number(s): "349385555"

Test #6:

score: 0
Accepted
time: 20ms
memory: 67136kb

input:

20 12
1 1 2 0 1 3 2 3 2 1 3 1
1 0 2 0 1 3 2 2 0 1 2 1

output:

353363623

result:

ok 1 number(s): "353363623"

Test #7:

score: 0
Accepted
time: 12ms
memory: 67140kb

input:

20 4
7 3 6 4
5 2 6 3

output:

395691825

result:

ok 1 number(s): "395691825"

Subtask #3:

score: 15
Accepted

Test #8:

score: 15
Accepted
time: 23ms
memory: 67140kb

input:

30 4
6 7 9 8
2 5 6 3

output:

60666143

result:

ok 1 number(s): "60666143"

Test #9:

score: 0
Accepted
time: 16ms
memory: 67140kb

input:

30 6
6 3 4 7 4 6
4 0 2 3 0 3

output:

630806734

result:

ok 1 number(s): "630806734"

Test #10:

score: 0
Accepted
time: 16ms
memory: 67140kb

input:

30 10
1 1 1 2 4 2 9 3 4 3
1 1 1 1 2 1 6 3 2 0

output:

179858440

result:

ok 1 number(s): "179858440"

Test #11:

score: 0
Accepted
time: 18ms
memory: 67136kb

input:

30 12
3 3 4 3 1 2 3 2 3 2 1 3
1 0 4 1 1 0 1 1 1 1 1 0

output:

301187770

result:

ok 1 number(s): "301187770"

Test #12:

score: 0
Accepted
time: 12ms
memory: 67136kb

input:

30 9
3 4 6 3 5 1 2 4 2
3 0 3 2 0 1 0 0 2

output:

436026962

result:

ok 1 number(s): "436026962"

Subtask #4:

score: 10
Accepted

Test #13:

score: 10
Accepted
time: 16ms
memory: 67140kb

input:

50 12
1 7 2 5 7 2 5 5 1 4 8 3
0 3 1 0 7 2 4 0 1 1 2 1

output:

239294646

result:

ok 1 number(s): "239294646"

Test #14:

score: 0
Accepted
time: 15ms
memory: 67140kb

input:

50 11
2 8 8 4 4 2 3 8 5 1 5
1 3 0 1 0 2 3 1 0 0 2

output:

765549051

result:

ok 1 number(s): "765549051"

Test #15:

score: 0
Accepted
time: 24ms
memory: 67140kb

input:

50 7
8 8 4 8 7 6 9
2 0 2 0 4 4 3

output:

549208280

result:

ok 1 number(s): "549208280"

Test #16:

score: 0
Accepted
time: 15ms
memory: 67136kb

input:

50 8
7 7 9 6 5 2 9 5
6 4 3 4 2 1 0 5

output:

839273742

result:

ok 1 number(s): "839273742"

Subtask #5:

score: 10
Accepted

Test #17:

score: 10
Accepted
time: 27ms
memory: 67248kb

input:

1000 6
163 186 169 133 180 169
22 107 126 110 157 76

output:

157453508

result:

ok 1 number(s): "157453508"

Test #18:

score: 0
Accepted
time: 15ms
memory: 67244kb

input:

1000 5
207 204 201 189 199
186 78 40 43 44

output:

883958433

result:

ok 1 number(s): "883958433"

Test #19:

score: 0
Accepted
time: 36ms
memory: 67248kb

input:

1000 6
199 154 146 162 174 165
189 144 141 159 169 157

output:

979732021

result:

ok 1 number(s): "979732021"

Subtask #6:

score: 35
Accepted

Test #20:

score: 35
Accepted
time: 1340ms
memory: 67244kb

input:

1000 12
83 77 95 76 81 104 78 79 75 96 71 85
73 77 90 76 74 95 71 79 67 93 63 84

output:

286972637

result:

ok 1 number(s): "286972637"

Test #21:

score: 0
Accepted
time: 1623ms
memory: 67244kb

input:

1000 12
93 77 93 91 87 70 94 70 78 92 72 83
2 30 13 87 61 11 30 27 57 47 65 51

output:

683836742

result:

ok 1 number(s): "683836742"

Test #22:

score: 0
Accepted
time: 642ms
memory: 67236kb

input:

1000 12
85 92 75 75 83 90 89 76 80 84 87 84
52 86 59 34 0 41 35 64 63 57 65 66

output:

100350235

result:

ok 1 number(s): "100350235"

Extra Test:

score: 0
Extra Test Passed