[多项式ln][多项式exp][背包DP][生成函数] LOJ #556. 咱们去烧菜吧

20 篇文章 0 订阅
8 篇文章 0 订阅

Solution S o l u t i o n

就是个背包DP咯。
答案长这个样子

ans=[xn]i=1m1xaibi+ai1xai a n s = [ x n ] ∏ i = 1 m 1 − x a i b i + a i 1 − x a i
考虑取个 ln ln
lnans=i=1m(ln(1xaibi+ai)ln(1xai)) ln ⁡ a n s = ∑ i = 1 m ( ln ⁡ ( 1 − x a i b i + a i ) − ln ⁡ ( 1 − x a i ) )
根据
ln(1xk)=i1xkii ln ⁡ ( 1 − x k ) = − ∑ i ≥ 1 x k i i
展开,用一个桶存一下每个式子的贡献,展开的复杂度就是 O(nlnn) O ( n ln ⁡ n ) 的。
最后求个 exp exp 就好了。 O(nlogn) O ( n log ⁡ n )

#include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pairs;

const int N = 303030;
const int MOD = 998244353;

inline char get(void) {
    static char buf[100000], *S = buf, *T = buf;
    if (S == T) {
        T = (S = buf) + fread(buf, 1, 100000, stdin);
        if (S == T) return EOF;
    }
    return *S++;
}
template<typename T>
inline void read(T &x) {
    static char c; x = 0; int sgn = 0;
    for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;
    for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
    if (sgn) x = -x;
}

inline int pwr(int a, int b) {
    int c = 1;
    while (b) {
        if (b & 1) c = (ll)c * a % MOD;
        b >>= 1; a = (ll)a * a % MOD;
    }
    return c;
}
inline int ivs(int x) {
    return pwr(x, MOD - 2);
}
inline int sum(int a, int b) {
    a += b;
    return a >= MOD ? a - MOD : a;
}
inline int sub(int a, int b) {
    return a < b ? a - b + MOD : a - b;
}
inline void add(int &x, int a) {
    x = sum(x, a);
}

namespace FNT {
    const int MAXN = 303030;
    int ww[MAXN], iw[MAXN];
    int rev[MAXN];
    int num;
    inline void pre(int n) {
        num = n;
        int g = pwr(3, (MOD - 1) / n);
        ww[0] = iw[0] = 1;
        for (int i = 1; i < num; i++)
            iw[n - i] = ww[i] = (ll)ww[i - 1] * g % MOD;
    }
    inline void fnt(int *a, int n, int f) {
        static int x, y, *w;
        w = (f == 1) ? ww : iw;
        for (int i = 0; i < n; i++)
            if (rev[i] > i)
                swap(a[rev[i]], a[i]);
        for (int i = 1; i < n; i <<= 1)
            for (int j = 0; j < n; j += (i << 1))
                for (int k = 0; k < i; k++) {
                    x = a[j + k];
                    y = (ll)a[j + k + i] * w[num / (i << 1) * k] % MOD;
                    a[j + k] = sum(x, y);
                    a[j + k + i] = sub(x, y);
                }
        if (f == -1){
            int in = ivs(n);
            for (int i = 0; i < n; i++)
                a[i] = (ll)a[i] * in % MOD;
        }
    }
}

int inv[N];

inline void pre(int n) {
    inv[1] = 1;
    for (int i = 2; i <= n; i++)
        inv[i] = (ll)(MOD - MOD / i) * inv[MOD % i] % MOD;
}

void getInv(int *a, int *b, int n) {
    using namespace FNT;
    static int tmp[N];
    if (n == 1) return (void)(b[0] = ivs(a[0]));
    getInv(a, b, n >> 1);
    for (int i = 0; i < n; i++) {
        tmp[i] = a[i]; tmp[i + n] = 0;
    }
    int L = 0; while (!(n >> L & 1)) L++;
    for (int i = 0; i < (n << 1); i++)
        rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << L);
    fnt(tmp, n << 1, 1); fnt(b, n << 1, 1);
    for (int i = 0; i < (n << 1); i++)
        tmp[i] = (ll)b[i] * sub(2, (ll)tmp[i] * b[i] % MOD) % MOD;
    fnt(tmp, n << 1, -1);
    for (int i = 0; i < n; i++) {
        b[i] = tmp[i]; b[n + i] = 0;
    }
}
inline void getLn(int *a, int *b, int n) {
    using namespace FNT;
    static int da[N], ia[N], tmp[N];
    for (int i = 0; i < (n << 1); i++)
        tmp[i] = da[i] = ia[i] = 0;
    getInv(a, ia, n);
    for (int i = 1; i < n; i++)
        da[i - 1] = (ll)a[i] * i % MOD;
    int L = 0; while (!(n >> L & 1)) L++;
    for (int i = 0; i < (n << 1); i++)
        rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << L);
    fnt(da, n << 1, 1); fnt(ia, n << 1, 1);
    for (int i = 0; i < (n << 1); i++)
        tmp[i] = (ll)da[i] * ia[i] % MOD;
    fnt(tmp, n << 1, -1);
    b[0] = b[n] = 0;
    for (int i = 1; i < n; i++) {
        b[i] = (ll)tmp[i - 1] * inv[i] % MOD;
        b[n + i] = 0;
    }
}
inline void getExp(int *a, int *b, int n) {
    using namespace FNT;
    static int lb[N];
    if (n == 1) return (void)(b[0] = 1);
    getExp(a, b, n >> 1);
    for (int i = 0; i < (n << 1); i++) lb[i] = 0;
    getLn(b, lb, n);
    for (int i = 0; i < n; i++)
        lb[i] = sub(a[i], lb[i]);
    lb[0] = sum(lb[0], 1);
    int L = 0; while (!(n >> L & 1)) L++;
    for (int i = 0; i < (n << 1); i++)
        rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << L);
    fnt(b, n << 1, 1); fnt(lb, n << 1, 1);
    for (int i = 0; i < (n << 1); i++)
        b[i] = (ll)b[i] * lb[i] % MOD;
    fnt(b, n << 1, -1);
    for (int i = 0; i < n; i++) b[i + n] = 0;
}

int n, m, l, x, y;
int buc[N], f[N], g[N];

int main(void) {
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
    FNT::pre(1 << 18);
    pre(1 << 18);
    read(n); read(m);
    for (l = 1; l <= n; l <<= 1);
    for (int i = 1; i <= m; i++) {
        read(x); read(y);
        if (!x) continue;
        if (!y) y = n / x;
        if ((ll)x * y + x < l) buc[x * y + x]--;
        if (x < l) buc[x]++;
    }
    for (int i = 0; i < l; i++)
        buc[i] = sum(buc[i], MOD);
    for (int i = 1; i < l; i++)
        for (int j = i; j < l; j += i)
            f[j] = sum(f[j], (ll)buc[i] * inv[j / i] % MOD);
    getExp(f, g, l);
    for (int i = 1; i <= n; i++)
        printf("%d\n", g[i]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值