Comet OJ - Contest # 11 E ffort (fast power polynomial composition count +)

Portal.

answer:

When considering the final total number of injuries is s, then the allocation bit baffle, the program number is \ (S-C_ {}. 1. 1-n-^ {} \) .

So the problem is that a lot of the total number of injuries, not one by one operator.

\ (S-C_ {}. 1. 1-n-^ {} \) of OGF is \ ({x ^ {n- 1} \ over (1-x) ^ n} \)

\(F=FA+R->F={R \over 1-A}\)

To give recursive \ (l- A = (. 1-X) n-^ \) , the preceding item can be calculated by the number of combinations.

Then each is constant coefficient recursive, every time modulo out just fine.

Complexity is \ (O (log ^ 2) \)

Solution to a problem is given a more clever way, we do not directly ask \ (s \) number of injury scheme.

Consider \ (f (x) \) indicates any harm, x number assigned to individual programs.

When combined \ (f, g \) piles always damage, since the intermediate baffles can Ca may not be inserted, so that \ (F * G * (. 1 + X) \) .

Convolution with NTT, after each count only the reserved front n item.

Fast power is set directly \ (O (log ^ 2) \) , but can be used to optimize exp.

This equation using the initial value like \ (\ sum_ Y = {I} {I} ^ ^ xC_ C_ {X = Y + Y +. 1. 1} ^ {} \) .

Until now I realized that Pascal's Triangle of a method for finding a diagonal and is the same, because the flip about enough.

Code:

#include<bits/stdc++.h>
#define fo(i, x, y) for(int i = x, B = y; i <= B; i ++)
#define ff(i, x, y) for(int i = x, B = y; i <  B; i ++)
#define fd(i, x, y) for(int i = x, B = y; i >= B; i --)
#define ll long long
#define pp printf
#define hh pp("\n")
using namespace std;

const int mo = 998244353;

ll ksm(ll x, ll y) {
    ll s = 1;
    for(; y; y /= 2, x = x * x % mo)
        if(y & 1) s = s * x % mo;
    return s;
}

typedef vector<ll> V;
#define pb push_back
#define si size()
#define re resize

namespace ntt {
    const int nm = 262144;
    ll w[nm], a[nm], b[nm]; int r[nm];
    void build() {
        for(int i = 1; i < nm; i *= 2) {
            w[i] = 1;
            ll v = ksm(3, (mo - 1) / 2 / i);
            ff(j, 1, i) w[i + j] = w[i + j - 1] * v % mo;
        }
    }
    void dft(ll *a, int n, int f) {
        ff(i, 0, n) {
            r[i] = r[i / 2] / 2 + (i & 1) * (n / 2);
            if(i < r[i]) swap(a[i], a[r[i]]);
        } ll b;
        for(int i = 1; i < n; i *= 2) for(int j = 0; j < n; j += 2 * i) ff(k, 0, i)
            b = a[i + j + k] * w[i + k], a[i + j + k] = (a[j + k] - b) % mo, a[j + k] = (a[j + k] + b) % mo;
        if(f == -1) {
            reverse(a + 1, a + n);
            b = ksm(n, mo - 2);
            ff(i, 0, n) a[i] = (a[i] + mo) * b % mo;
        }
    }
    V operator * (V p, V q) {
        int n0 = p.si + q.si - 1, n = 1;
        while(n < n0) n *= 2;
        ff(i, 0, n) a[i] = b[i] = 0;
        ff(i, 0, p.si) a[i] = p[i];
        ff(i, 0, q.si) b[i] = q[i];
        dft(a, n, 1); dft(b, n, 1);
        ff(i, 0, n) a[i] = a[i] * b[i] % mo;
        dft(a, n, -1);
        p.re(n0);
        ff(i, 0, n0) p[i] = a[i];
        return p;
    }
    void dft(V &p, int f) {
        int n = p.si;
        ff(i, 0, n) a[i] = p[i];
        dft(a, n, f);
        ff(i, 0, n) p[i] = a[i];
    }
}

using ntt :: operator *;
using ntt :: dft;

V qni(V a) {
    V b; b.re(1); b[0] = ksm(a[0], mo - 2);
    for(int n = 2; n < a.si * 2; n *= 2) {
        V c = a; c.re(n); c.re(2 * n); dft(c, 1);
        b.re(2 * n); dft(b, 1);
        ff(i, 0, 2 * n) b[i] = (2 * b[i] - c[i] * b[i] % mo * b[i]) % mo;
        dft(b, -1); b.re(n);
    }
    b.re(a.si); return b;
}

V qd(V a) {
    fo(i, 0, a.si - 2) a[i] = a[i + 1] * (i + 1) % mo;
    a.re(a.si - 1);
    return a;
}
V jf(V a) {
    a.re(a.si + 1);
    fd(i, a.si - 1, 1) a[i] = a[i - 1] * ksm(i, mo - 2) % mo;
    a[0] = 0;
    return a;
}
 
V ln(V a) {
    int n = a.si;
    a = jf(qd(a) * qni(a));
    a.re(n); 
    return a;
}

V exp(V a) {
    V b; b.re(1); b[0] = 1;
    for(int n = 1; n < a.si * 2; n *= 2) {
        b.re(n);
        V c = a; c.re(n);
        V d = ln(b);
        ff(i, 0, n) d[i] -= c[i];
        d = d * b;
        ff(i, 0, n) b[i] = (b[i] - d[i] + mo) % mo;
    }
    b.re(a.si); return b;
}

const int N = 1e5 + 5;

int n, m, a[N], b[N];
ll fac[N], nf[N];

void build(int n) {
    fac[0] = 1;
    fo(i, 1, n) fac[i] = fac[i - 1] * i % mo;
    nf[n] = ksm(fac[n], mo - 2);
    fd(i, n, 1) nf[i - 1] = nf[i] * i % mo;
}

V p;

V mul(V a, V b) {
    a = a * b;
    a.re(m);
    fd(i, m - 1, 1) a[i] = (a[i] + a[i - 1]) % mo;
    return a;
}

V c;

V ksm(V x, int y) {
    if(y == 1) return x;
    ll xc = x[0]; ll nc = ksm(xc, mo - 2);
    ff(i, 0, m) x[i] = x[i] * nc % mo;
    x = ln(x);
    ff(i, 0, m) x[i] = x[i] * y % mo;
    x = exp(x);
    xc = ksm(xc, y);
    ff(i, 0, m) x[i] = x[i] * xc % mo;
    V d = c;
    ff(i, 0, m) d[i] = d[i] * (y - 1) % mo;
    d = exp(d);
    x = x * d; x.re(m);
    return x;
}

V operator + (V a, V b) {
    if(a.si < b.si) a.re(b.si);
    ff(i, 0, b.si) a[i] = (a[i] + b[i]) % mo;
    return a;
}

V ans;

int main() {
    ntt :: build();
    build(1e5);
    scanf("%d %d", &m, &n);
    c.re(2); c[0] = 1; c[1] = 1;
    c.re(m); c = ln(c);
    fo(i, 1, n) scanf("%d %d", &a[i], &b[i]);
    fo(i, 1, n) {
        p.clear(); p.re(m);
        ll f = 1;
        fo(j, 1, min(b[i], m)) {
            f = f * (b[i] - j + 1) % mo;
            p[j - 1] = f * nf[j] % mo;
        }
        p = ksm(p, a[i]);
        if(i == 1) ans = p; else ans = mul(ans, p);
    }
    ll as = (ans[m - 1] % mo + mo ) % mo;
    pp("%lld\n", as);
}

Guess you like

Origin www.cnblogs.com/coldchair/p/11587807.html