题目大意:给定长度为$n-1$的数组$g_{[1,n)}$,求$f_{[0,n)}$,要求:

$$
f_i=\sum_{j=1}^if_{i-j}g_j\\
f_0=1
$$

题解:直接求复杂度是$O(n^2)$,明显不可以通过此题

分治$FFT$,可以用$CDQ$分治,先求出$f_{[l,mid)}$,可以发现这部分对区间的$f_{[mid,r)}$的贡献是$f_{[l,mid)}*g_{[0,r-l)}$,卷出来加到对应位置就行了,复杂度$O(n\log_2^2n)​$

卡点:

C++ Code:

#include <algorithm>
#include <cstdio>
#include <cctype>
namespace std {struct istream {
#define M (1 << 21 | 3)char buf[M], *ch = buf - 1;inline istream() {
#ifndef ONLINE_JUDGEfreopen("input.txt", "r", stdin);
#endiffread(buf, 1, M, stdin);}inline istream& operator >> (int &x) {while (isspace(*++ch));for (x = *ch & 15; isdigit(*++ch); ) x = x * 10 + (*ch & 15);return *this;}
#undef M} cin;struct ostream {
#define M (1 << 21 | 3)char buf[M], *ch = buf - 1;int w;inline ostream& operator << (int x) {if (!x) {*++ch = '0';return *this;}for (w = 1; w <= x; w *= 10);for (w /= 10; w; w /= 10) *++ch = (x / w) ^ 48, x %= w;return *this;}inline ostream& operator << (const char x) {*++ch = x; return *this;}inline ~ostream() {
#ifndef ONLINE_JUDGEfreopen("output.txt", "w", stdout);
#endiffwrite(buf, 1, ch - buf + 1, stdout);}
#undef M} cout;
}#define maxn 131072 | 3
const int mod = 998244353, G = 3;namespace Math {inline int pw(int base, int p) {static int res;for (res = 1; p; p >>= 1, base = static_cast<long long> (base) * base % mod) if (p & 1) res = static_cast<long long> (res) * base % mod;return res;}inline int inv(int x) {return pw(x, mod - 2);}
}int n;
int f[maxn], g[maxn];
namespace Poly {
#define N 131072 | 3int s, lim, ilim, rev[N];int Wn[N + 1];inline void reduce(int &x) {x += x >> 31 & mod;}inline void clear(register int *l, const int *r) {if (l >= r) return ;while (l != r) *l++ = 0;}inline void init(const int n) {s = -1, lim = 1; while (lim <= n) lim <<= 1, s++; ilim = Math::inv(lim);for (int i = 1; i < lim; i++) rev[i] = rev[i >> 1] >> 1 | (i & 1) << s;const int t = Math::pw(G, (mod - 1) / lim);*Wn = 1; for (register int *i = Wn; i != Wn + lim; ++i) *(i + 1) = static_cast<long long> (*i) * t % mod;}inline void NTT(int *A, const int op = 1) {for (register int i = 1; i < lim; i++) if (i < rev[i]) std::swap(A[i], A[rev[i]]);for (register int mid = 1; mid < lim; mid <<= 1) {const int t = lim / mid >> 1;for (register int i = 0; i < lim; i += mid << 1) {for (register int j = 0; j < mid; j++) {const int W = op ? Wn[t * j] : Wn[lim - t * j];const int X = A[i + j], Y = static_cast<long long> (A[i + j + mid]) * W % mod;reduce(A[i + j] += Y - mod), reduce(A[i + j + mid] = X - Y);}}}if (!op) for (int i = 0; i < lim; i++) A[i] = static_cast<long long> (A[i]) * ilim % mod;}int A[N], B[N];void CDQ_NTT(const int l, const int r) {if (r - l < 2) return ;const int mid = l + r >> 1;CDQ_NTT(l, mid); init(r - l);std::copy(f + l, f + mid, A); clear(A + mid - l, A + lim);std::copy(g, g + r - l, B); clear(B + r - l, B + lim);NTT(A), NTT(B);for (int i = 0; i < lim; i++) A[i] = static_cast<long long> (A[i]) * B[i] % mod;NTT(A, 0);for (int i = mid; i < r; i++) reduce(f[i] += A[i - l] - mod);CDQ_NTT(mid, r);}
#undef N
}int main() {std::cin >> n;for (int i = 1; i < n; i++) std::cin >> g[i];*f = 1;Poly::CDQ_NTT(0, n);for (int i = 0; i < n; i++) std::cout << f[i] << ' ';std::cout << '\n';return 0;
}

  

转载于:https://www.cnblogs.com/Memory-of-winter/p/10127918.html

[洛谷P4721]【模板】分治 FFT相关推荐

  1. 洛谷 - P4721 【模板】分治 FFT(分治NTT)

    题目链接:点击查看 题目大意:给出序列 g1,⋯,ng_{1,\cdots,n}g1,⋯,n​,求 f0,⋯,nf_{0,\cdots,n}f0,⋯,n​ 规定 fi=∑j=1ifi−jgjf_i=\ ...

  2. 洛谷·【模板】点分树 | 震波【including 点分树

    初见安-这里是传送门:洛谷P6329 [模板]点分树 | 震波 一.点分树 其实你会点分治的话,点分树就是把点分治时的重心提出来重新连城一棵树. 比如当前点是u,求出子树v的重心root后将root与 ...

  3. 专题·树链剖分【including 洛谷·【模板】树链剖分

    初见安~~~终于学会了树剖~~~ [兴奋]当初机房的大佬在学树剖的时候我反复强调过:"学树剖没有前途的!!!" 恩.真香. 一.重链与重儿子 所谓树剖--树链剖分,就是赋予一个链的 ...

  4. 洛谷 P1919 模板】A*B Problem升级版(FFT快速傅里叶)

    https://www.luogu.com.cn/problem/P1919 题目背景 本题数据已加强,请使用 FFT/NTT,不要再交 Python 代码浪费评测资源. 题目描述 给你两个正整数 a ...

  5. 洛谷.4245.[模板]任意模数NTT(MTT/三模数NTT)

    题目链接 三模数\(NTT\): 就是多模数\(NTT\)最后\(CRT\)一下...下面两篇讲的都挺明白的. https://blog.csdn.net/kscla/article/details/ ...

  6. 洛谷.4897.[模板]最小割树(Dinic)

    题目链接 最小割树模板.具体见:https://www.cnblogs.com/SovietPower/p/9734013.html. ISAP不知为啥T成0分了.. Dinic: //1566ms ...

  7. 强连通分量:洛谷P3387 模板:缩点

    传送门 顾名思义,模板awa #include <cstdio> #include <cstring> #include <cmath> #include < ...

  8. 【后缀数组】洛谷P3809模板题

    题目背景 这是一道模板题. 题目描述 读入一个长度为 n n n 的由大小写英文字母或数字组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符在原串中的位置. ...

  9. 洛谷 p3372 模板-线段树 1

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个 ...

最新文章

  1. python一般用什么编译器-Python常用编译器原理及特点解析
  2. C# 线程池和编程实例
  3. 2021-01-07 matlab数值分析 非线性方程求根 牛顿法
  4. 把Springboot项目部署到服务器上和结束运行
  5. 离散信号的抽取和内插例题_《数字信号处理》学习指导与题解 2011年版
  6. 更小的刘海和更宽的5G天线,下一代iPhone你期待吗?
  7. 疑似华为Mate 30 Pro上手视频曝光 看完更想买了!
  8. onenote复制出来是图片_OneNote入门篇
  9. Atitit stomp.js conn连接activemq 目录 1.1. activemq 启动,已经默认开启了stomp ws的接口。。地址是 1 1.2. Js 客户端代码 1 1.3
  10. ActivityGroup对子Activity的管理
  11. 微信小程序生成海报库
  12. css字体浏览(转)
  13. Vue事件修饰符——.prevent 和.passive
  14. centos解压分卷rar_linux命令:tar分卷压缩与合并解压缩
  15. php获取今天星期几,PHP获取星期几的常用方法小结
  16. c# 基于BouncyCastle.Crypto的国密sm2,sm4封装,与java版本兼容
  17. 大数据分析你不能不懂的6个核心技术
  18. 玩转基因组浏览器之查看MAF文件
  19. Pycharm 恢复到默认设置
  20. Azkban上传文件报错installation Failed.Error chunking

热门文章

  1. LeCun论战Markus:AI是否需要类似人类的认知能力?
  2. 漫画:什么是 “代理模式” ?
  3. “我想在 CSDN 写小说” 评论亮了 | 每日趣闻
  4. MySQL 5.6通过Keepalived+互为主从实现高可用架构
  5. repmgr 4.3 发布,PostgreSQL 复制与故障转移管理工具
  6. [转] mongoose学习笔记(超详细)
  7. 【LibreOJ】#6299. 「CodePlus 2018 3 月赛」白金元首与克劳德斯
  8. 团队编程项目作业3-模块开发过程
  9. Jquery ajax 返回string类型加result.d原因
  10. 基于OHCI的USB主机 —— OHCI(端点)