[多项式ln][多项式exp][生成函数] LOJ #6268. 分拆数

Solution S o l u t i o n

设分拆数的生成函数

F(x)=k0fkxk F ( x ) = ∑ k ≥ 0 f k x k
几个显然的等式就是
F(x)lnF(x)===k=111xkk=1ln11xkk=1i1xkii F ( x ) = ∏ k = 1 ∞ 1 1 − x k ln ⁡ F ( x ) = ∑ k = 1 ∞ ln ⁡ 1 1 − x k = ∑ k = 1 ∞ ∑ i ≥ 1 x k i i
直接 O(nlnn) O ( n ln ⁡ n ) 预处理右边的式子, exp exp 回去就好了。
O(nlogn) O ( n log ⁡ n )

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

const int N = 505050;
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;
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);
    int l, L;
    for (l = 1, L = 0; l <= n; l <<= 1) ++L; --L;
    for (int k = 1; k < l; k++)
        for (int i = k; i < l; i += k)
            add(f[i], inv[i / k]);
    getExp(f, g, l);
    for (int i = 1; i <= n; i++)
        printf("%d\n", g[i]);
    return 0;
}

你可能感兴趣的:(生成函数,DFT)