[洛谷P4721]分治FFT

NTT入门,放个板子

// luogu-judger-enable-o2
#include <bits/stdc++.h>
using namespace std;
#define fr(i, a, b) for ( int i = a; i <= b; ++ i)
#define mid ( l + r >> 1)
typedef long long ll;
const ll P = 998244353, mod = 998244353, Ge = 3;
template <class T> void G(T &x) {
    x = 0; char o = getchar(); int f = 1; for ( ; !isdigit(o); o = getchar()) if( o == '-') f = -1;
    for ( ; isdigit(o); o = getchar()) x = (x << 1) + (x << 3) + (o & 15); x *= f;
}
ll n, g[400010], f[400010], a[400010], b[400010], rev[400010];
ll w[2][400010];
ll _pow(ll x, ll n) { ll ans = 1; for ( ; n; n>>=1, x = x * x % P) if( n&1) ans = ans * x % P; return ans;}
inline void NTT(ll *a, int n, int f) {
    for ( int i = 0; i < n; ++ i) if( i < rev[i]) swap(a[i], a[rev[i]]);
    w[0][0] = w[1][0] = 1;
    for (int i = 1; i < n; i++) {
        w[0][i] = w[0][i - 1] * _pow(Ge, (P - 1) / n) % P;
        w[1][i] = _pow(w[0][i], P - 2);
    }
 
    for (int i = 1; i < n; i <<= 1)
    for (int j = 0, l = n / (i << 1); j < n; j += (i << 1))
    for (int k = 0, t = 0; k < i; k += 1, t += l) {
        ll x = a[j + k], y = w[f][t] * a[i + j + k] % P;
        a[j + k] = (x + y) % P, a[i + j + k] = (x - y + P) % P;
    }
 
    for (int i = 0; f && i < n; i++)
        a[i] = a[i] * _pow(n, P - 2) % P;
}
inline void cdq(int l, int r) {
    if( l == r) return ;
    cdq(l, mid); 
    int Len = 1, L = 0;
    for ( ; Len < (r - l + 1) << 1; Len <<= 1) ++ L;
    fr(i, 1, Len) rev[i] = (rev[i >> 1] >> 1)|((i&1) << L-1);
    fr(i, 0, Len) a[i] = b[i] = 0;
    fr(i, l, mid) a[i-l] = f[i];
    fr(i, 0, r-l) b[i] = g[i];
    NTT(a, Len, 0); NTT(b, Len, 0);
    fr(i, 0, Len) a[i] = a[i] * b[i] % P;
    NTT(a, Len, 1);
    fr(i, mid+1, r) f[i] = (f[i] + a[i-l]) % mod;
    cdq(mid+1, r);
}

int main() {
    G(n); fr(i, 1, n-1) G(g[i]);
    f[0] = 1; cdq(0, n-1);
    fr(i, 0, n-1) printf("%lld ", f[i]);
}
View Code

 

转载于:https://www.cnblogs.com/miecoku/p/9757357.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值