CometOJ Round #11 E

题意

有n个敌人,m种攻击,第i种攻击会攻击$a_i$次,每次攻击会对敌人总共造成$1\dots b_i$点伤害,

最后每个敌人都至少要受到1点伤害,求不同攻击方案数。

两种方案不同当且仅当某次攻击的总伤害不同或某个敌人受到的伤害不同。

$n\cdot m\le 100000$

题解

设攻击的总伤害为$s$,那么敌人受到的伤害不同方案数为$\binom{s-1}{n-1}$。

相当于除了第一点伤害以外选出$n-1$点伤害的方案数。

那么对于每种攻击计算这一次攻击中选出$k$点伤害的方案数。

这个方案数$=\sum_{1\le i\le b_i}\binom{i}{k}=\sum_{0\le i\le b_i}\binom{i}{k}-[k=0]=\binom{b_i+1}{k+1}-[k=0]$。

$b_i$很大,但是$k$不大,可以递推。

多项式快速幂/exp+ln多项式幂次然后卷积起来。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
Author: QAQ Automaton
Lang: C++
Prog: E.cpp
Mail: lk@qaq-am.com
Blog: https://www.qaq-am.com/
*/
#include<bits/stdc++.h>
#define int long long
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define all(x) x.begin(),x.end()
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const double eps=1e-8;
const double pi=acos(-1.0);
template<class T>int chkmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>int chkmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>T sqr(T a){return a*a;}
template<class T>T mmin(T a,T b){return a<b?a:b;}
template<class T>T mmax(T a,T b){return a>b?a:b;}
template<class T>T aabs(T a){return a<0?-a:a;}
template<class T>int dcmp(T a,T b){return a>b;}
template<int *a>int cmp_a(int x,int y){return a[x]<a[y];}
#define min mmin
#define max mmax
#define abs aabs
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
// getchar
#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
// print the remaining part
inline void flush () {
fwrite (obuf, 1, oS - obuf, stdout);
oS = obuf;
}
// putchar
inline void putc (char x) {
*oS ++ = x;
if (oS == oT) flush ();
}
// input a signed integer
inline bool read (signed &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
return 1;
}

inline bool read (long long &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
return 1;
}
inline bool read (char &x) {
x=gc();
return x!=EOF;
}
inline bool read(char *x){
while((*x=gc())=='\n' || *x==' '||*x=='\r')if(*x==EOF)return 0;
while(!(*x=='\n'||*x==' '||*x=='\r'||*x==EOF))*(++x)=gc();
*x=0;
return 1;
}
template<typename A,typename ...B>
inline bool read(A &x,B &...y){
return read(x)&&read(y...);
}
// print a signed integer
inline bool write (signed x) {
if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
while (x) qu[++ qr] = x % 10 + '0', x /= 10;
while (qr) putc (qu[qr --]);
return 0;
}

inline bool write (long long x) {
if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
while (x) qu[++ qr] = x % 10 + '0', x /= 10;
while (qr) putc (qu[qr --]);
return 0;
}
inline bool write (char x) {
putc(x);
return 0;
}
inline bool write(const char *x){
while(*x){putc(*x);++x;}
return 0;
}
inline bool write(char *x){
while(*x){putc(*x);++x;}
return 0;
}
template<typename A,typename ...B>
inline bool write(A x,B ...y){
return write(x)||write(y...);
}
//no need to call flush at the end manually!
struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: read;
using io :: putc;
using io :: write;
int inf;
struct _init_{
_init_(){
memset(&inf,0x3f,sizeof(inf));
}
};
_init_ ___INIT__;
const int p=998244353;
int fac[200005],inv[200005],invf[200005];
int a[200005],b[200005];
int fpm(int a,int b){
int c=1;
for(;b;b>>=1,a=a*a%p)if(b&1)c=c*a%p;
return c;
}
namespace Polynomial{
const ll p=998244353,g_=3;
ll rev[266667];
ll fpm(ll a,ll b){
ll c=1;
while(b){
if(b&1)c=c*a%p;
a=a*a%p;
b>>=1;
}
return c;
}
void Rev(ll n,ll *f){
for(ll i=0;i<=n&&i<n-i;++i)swap(f[i],f[n-i]);
}
void NTT(ll *f,ll n,ll flag){
for(ll i=1;i<n;++i){
rev[i]=(rev[i>>1]>>1)|((i&1)*(n>>1));
if(i<rev[i])swap(f[i],f[rev[i]]);
}
for(ll i=1;i<n;i<<=1){
ll ww=fpm(g_,flag*((p-1)/(i+i))+p-1);
for(ll j=0;j<n;j+=i+i){
ll w=1,u,v;
for(ll k=0;k<i;++k){
u=f[j+k];v=f[j+k+i]*w%p;
f[j+k]=(u+v)%p;
f[j+k+i]=(u-v+p)%p;
w=w*ww%p;
}
}
}
if(!~flag){
ll in=fpm(n,p-2);
for(ll i=0;i<n;++i)f[i]=f[i]*in%p;
}
}
ll f1[266667],g1[266667];
void Mul(ll n,ll *f,ll m,ll *g,ll *h){//卷积
ll N=1;
while(N<=n+m)N<<=1;
for(ll i=0;i<N;++i){f1[i]=f[i];if(i>n)f[i]=0;}
for(ll i=0;i<N;++i){g1[i]=g[i];if(i>m)g[i]=0;}
NTT(f,N,1);
if(f!=g)NTT(g,N,1);
for(ll i=0;i<N;++i)h[i]=f[i]*g[i]%p;
NTT(h,N,-1);
if(f!=h){for(ll i=0;i<N;++i)f[i]=f1[i];}
if(g!=h){for(ll i=0;i<N;++i)g[i]=g1[i];}
}
/*
1: Inv
2: Div ln
3. Sqrt exp
4. Sqrt Pow
*/
ll h1[266667],h2[266667],h3[266667],h4[266667];
void Inv(ll n,ll *f,ll *g){//求逆
if(n==1){
g[0]=fpm(*f,p-2);
g[1]=g[2]=g[3]=0;
return;
}
else{
ll m=(n+1)>>1;
Inv(m,f,g);
ll N=1;
while(N<=n+m+m-3)N<<=1;
for(ll i=0;i<N;++i){f1[i]=f[i];if(i>=n)f[i]=0;if(i>=m)g[i]=0;}
NTT(f,N,1);
NTT(g,N,1);
for(ll i=0;i<N;++i)g[i]=(g[i]+g[i]-g[i]*g[i]%p*f[i]%p+p)%p;
NTT(g,N,-1);
for(ll i=0;i<N;++i)f[i]=f1[i];
for(ll i=n;i<N;++i)g[i]=0;
}
}
void Div(ll n,ll *f,ll m,ll *g,ll *q,ll *r){//除法
Rev(n,f);
Rev(m,g);
Inv(n-m+1,g,h2);
for(ll i=n-m+1;i<=n+n-m-m+2;++i)q[i]=0;
Rev(n,f);
Rev(m,g);
Rev(n-m,q);
Mul(m,g,n-m,q,r);
for(ll i=0;i<=n;++i){r[i]=(f[i]-r[i]+p)%p;}
}
void Sqrt(ll n,ll *f,ll *g){//开根
if(n==1){
g[0]=(ll)(sqrt(f[0])+0.5);
g[1]=0;
}
else{
ll m;
Sqrt(m=(n+1)>>1,f,g);
Mul(m-1,g,m-1,g,h3);
if(n-1>m+m-2)h3[n-1]=0;
for(ll i=0;i<n;++i)h3[i]=(h3[i]+f[i])%p;
for(ll i=0;i<m;++i)g[i]=g[i]*2%p;
Inv(n,g,h4);
Mul(n-1,h4,n-1,h3,g);
for(ll i=n;i<n+n;++i)g[i]=0;
}
}
ll inv[266667];
void Integ(ll n,ll *f,ll *g){//积分
inv[1]=1;
for(ll i=2;i<=n+1;++i)inv[i]=(p-p/i)*inv[p%i]%p;
for(ll i=n;~i;--i)g[i+1]=f[i]*inv[i+1]%p;
g[0]=0;
}
void Deriv(ll n,ll *f,ll *g){//求导
for(ll i=1;i<=n;++i)g[i-1]=f[i]*i%p;
g[n]=0;
}
void ln(ll n,ll *f,ll *g){//对数
Inv(n,f,h2);
Deriv(n-1,f,g);
Mul(n-1,h2,n-2,g,g);
for(ll i=n-1;i<=n+n-3;++i)g[i]=0;
Integ(n-2,g,g);
}
void exp(ll n,ll *f,ll *g){//指数
if(n==1){
g[0]=1;
g[1]=0;
}
else{
exp((n+1)>>1,f,g);
ln(n,g,h3);
for(int i=0;i<n;++i){
h3[i]=(f[i]-h3[i]+p)%p;
}
h3[0]=(h3[0]+1)%p;
Mul(n-1,h3,(n-1)>>1,g,g);
for(int i=n;i<n+n;++i)g[i]=0;
}
}
void Pow(ll n,ll *f,ll k,ll *g){//幂次
for(int i=0;i<=n;++i)g[i]=i==0;
for(;k;k>>=1){
if(k&1)Mul(n,g,n,f,g);
Mul(n,f,n,f,f);
}
}
}
int ans[300005],f[300005],g[300005];
void Calc(int a,int n,int *f){
f[0]=a+1;
for(int i=1;i<=n;++i)f[i]=f[i-1]*(a-i+1)%p*inv[i+1]%p;
}
signed main(){
#ifdef QAQAutoMaton
freopen("E.in","r",stdin);
freopen("E.out","w",stdout);
#endif
int n,m;
read(n,m);
inv[1]=1;
for(int i=2;i<=200000;++i){
inv[i]=(p-p/i)*inv[p%i]%p;
}

for(int i=1;i<=m;++i)read(a[i],b[i]);
Calc(b[1]-1,n,ans);
--a[1];
for(int i=1;i<=m;++i)if(a[i]){
Calc(b[i],n,f);
f[0]=(f[0]+p-1)%p;
Polynomial::Pow(n-1,f,a[i],g);
Polynomial::Mul(n-1,ans,n-1,g,ans);
for(int j=0;j<=n+n;++j)f[i]=g[i]=0;
}
write(ans[n-1],'\n');
return 0;
}
# 数学

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×