【Luogu4239】多项式求逆 加强版(多项式求逆,任意模数NTT)

Description

多项式求逆,对 10 9 + 7 取模。


Solution

将多项式求逆的中的乘法换成任意模数NTT即可。


Code

/**************************************
 * Au: Hany01
 * Prob: [Luogu4239] 多项式求逆(加强版)
 * Date: Jul 26th, 2018
 * Email: [email protected]
**************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = j; i < i##_end_; ++ i)
#define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define y1 wozenmezhemecaia 
#ifdef hany01
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    register char c_; register int _, __;
    for (_ = 0, __ = 1, c_ = getchar(); !isdigit(c_); c_ = getchar()) if (c_ == '-')  __ = -1;
    for ( ; isdigit(c_); c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 1e5 + 7, MOD = 1e9 + 7;
const long double Pi = acos(-1.);

int rev[maxn << 2];

inline int ad(int x, int y) { if ((x += y) >= MOD) return x - MOD; return x; }
inline LL Pow(LL a, LL b) {
    static LL Ans;
    for (Ans = 1, a %= MOD; b; b >>= 1, (a *= a) %= MOD) if (b & 1) (Ans *= a) %= MOD;
    return Ans;
}

struct Complex { long double x, y; };
inline Complex operator + (const Complex &A, const Complex &B) { return (Complex){A.x + B.x, A.y + B.y}; }
inline Complex operator - (const Complex &A, const Complex &B) { return (Complex){A.x - B.x, A.y - B.y}; }
inline Complex operator * (const Complex &A, const Complex &B) { return (Complex){A.x * B.x - A.y * B.y, A.x * B.y + A.y * B.x}; }

inline void getRev(int n) {
    static int cnt, N;
    for (cnt = 0, N = 1; N < n; N <<= 1, ++ cnt);
    rep(i, n) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (cnt - 1));
}

inline void FFT(Complex* a, int n, int ty) {
    rep(i, n) if (i < rev[i]) swap(a[i], a[rev[i]]);
    for (register int i = 2, p = 1; i <= n; p = i, i <<= 1) {
        register Complex w0 = Complex{cos(2 * Pi / i), sin(2 * Pi / i) * ty};
        for (register int j = 0; j < n; j += i) {
            register Complex w = Complex{1, 0};
            rep(k, p) {
                register Complex x = a[j + k], y = a[j + k + p] * w;
                a[j + k] = x + y, a[j + k + p] = x - y;
                w = w * w0;
            }
        }
    }
    if (ty < 0) rep(i, n) a[i].x /= n;
}

inline void Mult(int* A, int* B, int* C, int n) {
    static Complex a[maxn << 2], b[maxn << 2], c[maxn << 2], d[maxn << 2];
    rep(i, n)
        a[i].x = A[i] >> 15, b[i].x = A[i] & 32767,
        c[i].x = B[i] >> 15, d[i].x = B[i] & 32767,
        a[i].y = 0, b[i].y = 0, c[i].y = 0, d[i].y = 0;
    FFT(a, n, 1), FFT(b, n, 1), FFT(c, n, 1), FFT(d, n, 1);
    rep(i, n) {
        register Complex ta = a[i], tb = b[i], tc = c[i], td = d[i];
        a[i] = ta * tc, b[i] = tb * tc + ta * td, c[i] = tb * td;
    }
    FFT(a, n, -1), FFT(b, n, -1), FFT(c, n, -1);
    rep(i, n) C[i] = ad(ad((LL)round(a[i].x) % MOD * ((1ll << 30) % MOD) % MOD, (LL)round(b[i].x) % MOD * (1ll << 15) % MOD), (LL)round(c[i].x) % MOD);
}

int A[maxn << 2], B[maxn << 2], C[maxn << 2];
void Inv(int* a, int* b, int len) {
    if (len == 1) { b[0] = Pow(a[0], MOD - 2); return; }
    Inv(a, b, len >> 1);
    rep(i, len) A[i] = a[i], B[i] = b[i];
    For(i, len, (len << 1)) A[i] = B[i] = C[i] = 0;
    getRev(len << 1), Mult(A, B, C, len << 1), Mult(C, B, C, len << 1);
    rep(i, len) b[i] = ad(ad(B[i], B[i]), MOD - C[i]);
}

int main()
{
#ifdef hany01
    File("luogu4239");
#endif

    static int n, N;
    static int a[maxn << 2], b[maxn << 2];

    n = read();
    rep(i, n) a[i] = read();
    for (N = 1; N <= n; N <<= 1);
    Inv(a, b, N);
    rep(i, n) printf("%d ", b[i]);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/hhaannyyii/article/details/81237021