1. 程式人生 > >Luogu4191:[CTSC2010]效能優化

Luogu4191:[CTSC2010]效能優化

傳送門
題目翻譯:給定兩個 \(n\) 次多項式 \(A,B\) 和一個整數 \(C\),求 \(A\times B^C\) 在模 \(x^n\) 意義下的卷積
顯然就是個迴圈卷積,所以只要代入 \(\omega_n^{k}\) 進去求出點值,然後插值就好了
???\(n\) 不是 \(2^k\) 的形式,不能直接 \(NTT\)
怎麼辦呢?
根據題目性質,可以把 \(n\) 拆成 \(2^{a_1}3^{a_2}5^{a_3}7^{a_4}\) 的形式
這啟示我們每次不是每次分成兩半而是拆分成 \(3/5/7\) 次,然後再合併點值
\(F(x)=\sum a_ix^i,F_r(x)=\sum a_{ip+r}x^i\)


那麼 \(F(x)=\sum x^rF(x^p)\)
根據單位複數的性質(消去引理和折半引理)那麼
\[F(\omega_n^{an+b})=\sum \omega_{np}^{(an+b)r}F_r(w_n^b)\]
那麼只需要寫一個每次分 \(p\) 份的 \(FFT\) 就好了

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn(5e5 + 5);

int n, c, a[maxn], b[maxn], tmp[maxn], g, pri[233333], tot, pw[2][maxn], mod, r[maxn];

inline int Pow(ll x, int y) {
    register ll ret = 1;
    for (; y; y >>= 1, x = x * x % mod)
        if (y & 1) ret = ret * x % mod;
    return ret;
}

inline void Inc(int &x, int y) {
    x = x + y >= mod ? x + y - mod : x + y;
}

int Dfs(int s, int p, int cur, int blk) {
    if (cur == tot + 1) return s + p;
    register int nxt;
    nxt = blk / pri[cur];
    return Dfs(s + nxt * (p % pri[cur]), (p - p % pri[cur]) / pri[cur], cur + 1, nxt);
}

inline void DFT(int *p, int opt) {
    register int i, j, k, l, q, t, cur;
    for (i = 0; i < n; ++i) tmp[r[i]] = p[i];
    for (i = 0; i < n; ++i) p[i] = tmp[i], tmp[i] = 0;
    for (i = 1, cur = tot; i < n; i *= pri[cur], --cur) {
        for (t = i * pri[cur], j = 0; j < n; j += t)
            for (k = 0; k < t; k += i)
                for (l = 0; l < i; ++l)
                    for (q = 0; q < pri[cur]; ++q)
                        Inc(tmp[j + k + l], (ll)pw[opt == -1][n / t * (k + l) * q % n] * p[j + i * q + l] % mod);
        for (j = 0; j < n; ++j) p[j] = tmp[j], tmp[j] = 0;
    }
    if (opt == -1) for (c = Pow(n, mod - 2), i = 0; i < n; ++i) p[i] = (ll)p[i] * c % mod;
}

int main() {
    register int i, j, x;
    scanf("%d%d", &n, &c), mod = n + 1;
    for (x = n, i = 2; i * i <= x; ++i)
        while (x % i == 0) pri[++tot] = i, x /= i;
    if (x > 1) pri[++tot] = x;
    for (i = 2; ; ++i) {
        for (g = i, j = 1; g && j <= tot; ++j)
            if (Pow(g, n / pri[j]) == 1) g = 0;
        if (g) break;
    }
    for (i = 0; i < n; ++i) scanf("%d", &a[i]);
    for (i = 0; i < n; ++i) scanf("%d", &b[i]);
    pw[0][0] = pw[1][0] = 1, pw[0][1] = g, pw[1][1] = Pow(g, mod - 2);
    for (i = 2; i < n; ++i) pw[0][i] = (ll)pw[0][i - 1] * g % mod, pw[1][i] = (ll)pw[1][i - 1] * pw[1][1] % mod;
    for (i = 0; i < n; ++i) r[i] = Dfs(0, i, 1, n);
    DFT(a, 1), DFT(b, 1);
    for (i = 0; i < n; ++i) a[i] = (ll)a[i] * Pow(b[i], c) % mod;
    DFT(a, -1);
    for (i = 0; i < n; ++i) printf("%d\n", a[i]);
    return 0;
}