参考资料

picks

miskcoo

menci

胡小兔

unname

自为风月马前卒

上面是FFT的,学完了就来看NTT

原根

例题:luogu3803

fft优化后模板

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. using namespace std;
  5. int n, m, lim=1, rev[2100005];
  6. const double PI=acos(-1.0);
  7. struct Complex{
  8. double x, y;
  9. Complex(double xx=0.0, double yy=0.0){
  10. x = xx;
  11. y = yy;
  12. }
  13. Complex operator+(const Complex &u)const{
  14. return Complex(x+u.x, y+u.y);
  15. }
  16. Complex operator-(const Complex &u)const{
  17. return Complex(x-u.x, y-u.y);
  18. }
  19. Complex operator*(const Complex &u)const{
  20. return Complex(x*u.x-y*u.y, x*u.y+y*u.x);
  21. }
  22. }a[2100005], b[2100005];
  23. template<typename T> void rn(T &x){
  24. x = 0;
  25. char ch=getchar();
  26. while(ch<'0' || ch>'9') ch = getchar();
  27. while(ch>='0' && ch<='9'){
  28. x = x * 10 + ch - '0';
  29. ch = getchar();
  30. }
  31. }
  32. void fft(Complex a[], int opt){
  33. for(int i=0; i<lim; i++)
  34. if(i<rev[i])
  35. swap(a[i], a[rev[i]]);
  36. for(int i=2; i<=lim; i<<=1){
  37. int tmp=i>>1;
  38. Complex wn=Complex(cos(PI*2.0/i), opt*sin(PI*2.0/i));
  39. for(int j=0; j<lim; j+=i){
  40. Complex w=Complex(1.0, 0.0);
  41. for(int k=0; k<tmp; k++){
  42. Complex tmp1=a[j+k], tmp2=w*a[j+k+tmp];
  43. a[j+k] = tmp1 + tmp2;
  44. a[j+k+tmp] = tmp1 - tmp2;
  45. w = w * wn;
  46. }
  47. }
  48. }
  49. if(opt==-1)
  50. for(int i=0; i<lim; i++)
  51. a[i].x /= lim;
  52. }
  53. int main(){
  54. cin>>n>>m;
  55. for(int i=0; i<=n; i++) rn(a[i].x);
  56. for(int i=0; i<=m; i++) rn(b[i].x);
  57. int tmpcnt=0;
  58. while(lim<=n+m) lim <<= 1, tmpcnt++;
  59. for(int i=0; i<lim; i++)
  60. rev[i] = (rev[i>>1]>>1) | ((i&1)<<(tmpcnt-1));
  61. fft(a, 1);
  62. fft(b, 1);
  63. for(int i=0; i<lim; i++)
  64. a[i] = a[i] * b[i];
  65. fft(a, -1);
  66. for(int i=0; i<=n+m; i++)
  67. printf("%d ", (int)(a[i].x+0.5));
  68. printf("\n");
  69. return 0;
  70. }

NTT

  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. typedef long long ll;
  5. int n, m, a[2100005], b[2100005], lim=1, limcnt, rev[2100005];
  6. const int mod=998244353, gg=3, gi=332748118;
  7. void rn(int &x){
  8. char ch=getchar();
  9. x = 0;
  10. while(ch<'0' || ch>'9') ch = getchar();
  11. while(ch>='0' && ch<='9'){
  12. x = x * 10 + ch - '0';
  13. ch = getchar();
  14. }
  15. }
  16. int ksm(int a, int b){
  17. int re=1;
  18. while(b){
  19. if(b&1) re = (ll)re * a % mod;
  20. a = (ll)a * a % mod;
  21. b >>= 1;
  22. }
  23. return re;
  24. }
  25. void ntt(int a[], int opt){
  26. for(int i=0; i<lim; i++)
  27. if(i<rev[i])
  28. swap(a[i], a[rev[i]]);
  29. for(int i=2; i<=lim; i<<=1){
  30. int tmp=i>>1, wn=ksm(opt==1?gg:gi, (mod-1)/i);
  31. for(int j=0; j<lim; j+=i){
  32. int w=1;
  33. for(int k=0; k<tmp; k++){
  34. int tmp1=a[j+k], tmp2=(ll)w*a[j+k+tmp]%mod;
  35. a[j+k] = (tmp1 + tmp2) % mod;
  36. a[j+k+tmp] = (tmp1 - tmp2 + mod) % mod;
  37. w = (ll)w * wn % mod;
  38. }
  39. }
  40. }
  41. if(opt==-1){
  42. int inv=ksm(lim, mod-2);
  43. for(int i=0; i<lim; i++)
  44. a[i] = (ll)a[i] * inv % mod;
  45. }
  46. }
  47. int main(){
  48. cin>>n>>m;
  49. for(int i=0; i<=n; i++) rn(a[i]);
  50. for(int i=0; i<=m; i++) rn(b[i]);
  51. while(lim<=n+m) lim <<= 1, limcnt++;
  52. for(int i=0; i<lim; i++)
  53. rev[i] = (rev[i>>1]>>1) | ((i&1)<<(limcnt-1));
  54. ntt(a, 1);
  55. ntt(b, 1);
  56. for(int i=0; i<lim; i++)
  57. a[i] = (ll)a[i] * b[i] % mod;
  58. ntt(a, -1);
  59. for(int i=0; i<=n+m; i++)
  60. printf("%d ", a[i]);
  61. printf("\n");
  62. return 0;
  63. }

递归版裸fft没什么优化

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. using namespace std;
  5. int n, m;
  6. const double PI=acos(-1.0);
  7. struct Complex{
  8. double x, y;
  9. Complex(double xx=0.0, double yy=0.0){
  10. x = xx;
  11. y = yy;
  12. }
  13. Complex operator+(const Complex &u)const{
  14. return Complex(x+u.x, y+u.y);
  15. }
  16. Complex operator-(const Complex &u)const{
  17. return Complex(x-u.x, y-u.y);
  18. }
  19. Complex operator*(const Complex &u)const{
  20. return Complex(x*u.x-y*u.y, x*u.y+y*u.x);
  21. }
  22. }a[4000005], b[4000005], buf[4000005];
  23. void fft(Complex a[], int lim, int opt){
  24. if(lim==1) return ;
  25. int tmp=lim/2;
  26. for(int i=0; i<tmp; i++){
  27. buf[i] = a[2*i];
  28. buf[i+tmp] = a[2*i+1];
  29. }
  30. for(int i=0; i<lim; i++)
  31. a[i] = buf[i];
  32. fft(a, tmp, opt);
  33. fft(a+tmp, tmp, opt);
  34. Complex wn=Complex(cos(PI*2.0/lim), opt*sin(PI*2.0/lim)), w=Complex(1.0, 0.0);
  35. for(int i=0; i<tmp; i++){
  36. buf[i] = a[i] + w * a[i+tmp];
  37. buf[i+tmp] = a[i] - w * a[i+tmp];
  38. w = w * wn;
  39. }
  40. for(int i=0; i<lim; i++)
  41. a[i] = buf[i];
  42. }
  43. int main(){
  44. cin>>n>>m;
  45. for(int i=0; i<=n; i++) scanf("%lf", &a[i].x);
  46. for(int i=0; i<=m; i++) scanf("%lf", &b[i].x);
  47. int lim=1;
  48. while(lim<=n+m) lim <<= 1;
  49. fft(a, lim, 1);
  50. fft(b, lim, 1);
  51. for(int i=0; i<=lim; i++)
  52. a[i] = a[i] * b[i];
  53. fft(a, lim, -1);
  54. for(int i=0; i<=n+m; i++)
  55. printf("%d ", (int)(a[i].x/lim+0.5));
  56. printf("\n");
  57. return 0;
  58. }

FFT、NTT学习笔记的更多相关文章

  1. FFT&NTT学习笔记

    具体原理就不讲了qwq,毕竟证明我也不太懂 FFT(快速傅立叶变换)&NTT(快速数论变换) FFT //求多项式乘积 //要求多项式A和多项式B的积多项式C //具体操作就是 //DFT(A ...

  2. FFT/NTT 学习笔记

    0. 前置芝士 基础群论 复数 \(\mathbb C = \mathbb R[x^2+1]\) 则有 \(i^2+1=(-i)^2+1=0\),\(i \in \mathbb C - \mathbb ...

  3. FFT和NTT学习笔记_基础

    FFT和NTT学习笔记 算法导论 参考(贺) http://picks.logdown.com/posts/177631-fast-fourier-transform https://blog.csd ...

  4. FFT/NTT复习笔记&多项式&生成函数学习笔记Ⅲ

    第三波,走起~~ FFT/NTT复习笔记&多项式&生成函数学习笔记Ⅰ FFT/NTT复习笔记&多项式&生成函数学习笔记Ⅱ 单位根反演 今天打多校时 1002 被卡科技了 ...

  5. FFT/NTT复习笔记&多项式&生成函数学习笔记Ⅰ

    众所周知,tzc 在 2019 年(12 月 31 日)就第一次开始接触多项式相关算法,可到 2021 年(1 月 1 日)才开始写这篇 blog. 感觉自己开了个大坑( 多项式 多项式乘法 好吧这个 ...

  6. FFT/NTT复习笔记&多项式&生成函数学习笔记Ⅱ

    因为垃圾电脑太卡了就重开了一个... 前传:多项式Ⅰ u1s1 我预感还会有Ⅲ 多项式基础操作: 例题: 26. CF438E The Child and Binary Tree 感觉这题作为第一题还 ...

  7. 快速傅里叶变换(FFT)学习笔记(未完待续)

    目录 参考资料 FFT 吹水 例题 普通做法 更高大尚的做法 定义与一部分性质 系数表达式 点值表达式 点值相乘??? 卷积 复数 单位根 DFT IDFT 蝴蝶迭代优化 单位根求法 实现.细节与小优 ...

  8. NTT学习笔记

    和\(FFT\)相对应的,把单位根换成了原根,把共轭复数换成了原根的逆元,最后输出的时候记得乘以原\(N\)的逆元即可. #include <bits/stdc++.h> using na ...

  9. NTT 学习笔记

    引入 \(\tt NTT\) 和 \(\tt FFT\) 有什么不一样呢? 就是 \(\tt NTT\) 是可以用来取模的,而且没有复数带来的精度误差. 最最重要的是据说 \(\tt NTT\) 常数 ...

随机推荐

  1. NIO学习之Channel

    一.Channel基础 通道是一个对象,通过它可以读取和写入数据,Channel就是通向什么的道路,为数据的流向提供渠道: 在传统IO中,我们要读取一个文件中的内容使用Inputstream,该str ...

  2. Xiaocms 去版权

    Xiaocms 去版权 后台去版权: 1.  登录页面 修改文件:\admin\template\login.tpl.php 代码: <td width="190" rows ...

  3. div高度不能自适应(子级使用float浮动,父级div高度不能自适应)

    1.问题截图: 2.问题描述: 由于地址.公司名长度的不定性,所以每一条地址所在的父级div高度不定,但是需要设置一个最小的高度min-height:48px;但是当内容增加的时候,父级div高度却不 ...

  4. 【转载】UWP应用设置和文件设置:科普

    数据有两个基本的分类,应用数据和用户数据,而用户数据则为由用户拥有的数据,如文档,音乐或电子邮件等,下面将大致的介绍一下应用数据的基本操作. 应用数据:应用数据包含APP的状态信息(如运行时状态,用户 ...

  5. BZOJ 3992: [SDOI2015]序列统计 NTT+快速幂

    3992: [SDOI2015]序列统计 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 1155  Solved: 532[Submit][Statu ...

  6. Android(java)学习笔记124:利用Service在后台播放背景音乐

    1. 在android应用程序里,有一种没有UI的类(android.app.Service)——Service.简单来说,Service是一个 background process(背景程序),通过 ...

  7. 校招准备-关系型数据库与nosql

    深入理解常见的数据库的设计架构, 其中用到的数据结构, 算法等 SQL执行流程和优化, 可以了解一下calcite: https://calcite.apache.org/

  8. 基于arcgis api for js高速公路智能化智慧公路养护WebGIS开源系统

    伴随着高速公路建设进程加快,其涉及信息量增大.类型多样.地点分布广,传统的信息管理方式已不适应公路建设迅速发展的需要,而目前能对高速公路在设计.施工.养护等阶段的各类信息综合进行管理的信息系统尚较少见 ...

  9. 【最大权闭合子图 最小割】bzoj1497: [NOI2006]最大获利

    最大权闭合子图的模型:今天才发现dinic板子是一直挂的…… Description 新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战.THU集团旗下的CS&T通讯公司在 ...

  10. Angular - angularjs2 一些报错的概览(数据为json格式)

    {"Unterminated string literal.": "未终止的字符串文本.","Identifier expected.": ...