贴板子啦……




FFT板子:luogu P3803 【模板】多项式乘法(FFT)

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cmath>
  4. #include<algorithm>
  5. #include<cstring>
  6. #include<cstdlib>
  7. #include<cctype>
  8. #include<vector>
  9. #include<stack>
  10. #include<queue>
  11. using namespace std;
  12. #define enter puts("")
  13. #define space putchar(' ')
  14. #define Mem(a, x) memset(a, x, sizeof(a))
  15. #define In inline
  16. typedef long long ll;
  17. typedef double db;
  18. const int INF = 0x3f3f3f3f;
  19. const db eps = 1e-8;
  20. const int maxn = 4e6 + 5;
  21. const db PI = acos(-1);
  22. inline ll read()
  23. {
  24. ll ans = 0;
  25. char ch = getchar(), last = ' ';
  26. while(!isdigit(ch)) last = ch, ch = getchar();
  27. while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  28. if(last == '-') ans = -ans;
  29. return ans;
  30. }
  31. inline void write(ll x)
  32. {
  33. if(x < 0) x = -x, putchar('-');
  34. if(x >= 10) write(x / 10);
  35. putchar(x % 10 + '0');
  36. }
  37. int n, m, len = 1, lim = 0, rev[maxn];
  38. struct Comp
  39. {
  40. db x, y;
  41. In Comp operator + (const Comp& oth)const
  42. {
  43. return (Comp){x + oth.x, y + oth.y};
  44. }
  45. In Comp operator - (const Comp& oth)const
  46. {
  47. return (Comp){x - oth.x, y - oth.y};
  48. }
  49. In Comp operator * (const Comp& oth)const
  50. {
  51. return (Comp){x * oth.x - y * oth.y, x * oth.y + y * oth.x};
  52. }
  53. friend In void swap(Comp& a, Comp& b)
  54. {
  55. swap(a.x, b.x); swap(a.y, b.y);
  56. }
  57. }a[maxn], b[maxn];
  58. In void fft(Comp* a, int flg)
  59. {
  60. for(int i = 0; i < len; ++i) if(i < rev[i]) swap(a[i], a[rev[i]]);
  61. for(int i = 1; i < len; i <<= 1)
  62. {
  63. Comp omg = (Comp){cos(PI / i), sin(PI / i) * flg};
  64. for(int j = 0; j < len; j += (i << 1))
  65. {
  66. Comp o = (Comp){1, 0};
  67. for(int k = 0; k < i; ++k, o = o * omg)
  68. {
  69. Comp tp1 = a[k + j], tp2 = o * a[k + j + i];
  70. a[k + j] = tp1 + tp2, a[k + j + i] = tp1 - tp2;
  71. }
  72. }
  73. }
  74. }
  75. int main()
  76. {
  77. n = read(); m = read();
  78. for(int i = 0; i <= n; ++i) a[i].x = read();
  79. for(int i = 0; i <= m; ++i) b[i].x = read();
  80. while(len <= n + m) len <<= 1, ++lim;
  81. for(int i = 0; i < len; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lim - 1));
  82. fft(a, 1); fft(b, 1);
  83. for(int i = 0; i < len; ++i) a[i] = a[i] * b[i];
  84. fft(a, -1);
  85. for(int i = 0; i <= n + m; ++i) write((int)(a[i].x / len + 0.5)), space; enter;
  86. return 0;
  87. }



NTT板子:[luogu P3803 【模板】多项式乘法(FFT)](https://www.luogu.org/problemnew/show/P3803)
```c++
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 4e6 + 5;
const ll mod = 998244353;
const ll G = 3;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans = 10) write(x / 10);
putchar(x % 10 + '0');
}

int n, m, len = 1, lim = 0, rev[maxn];

ll a[maxn], b[maxn];

In ll quickpow(ll a, ll b)

{

ll ret = 1;

for(; b; b >>= 1, a = a * a % mod)

if(b & 1) ret = ret * a % mod;

return ret;

}

In void ntt(ll* a, int len, int flg)

{

for(int i = 0; i < len; ++i) if(i < rev[i]) swap(a[i], a[rev[i]]);

for(int i = 1; i < len; i <<= 1)

{

ll gn = quickpow(G, (mod - 1) / (i << 1));

for(int j = 0; j < len; j += (i << 1))

{

ll g = 1;

for(int k = 0; k < i; ++k, g = g * gn % mod)

{

ll tp1 = a[k + j], tp2 = g * a[k + j + i] % mod;

a[k + j] = (tp1 + tp2) % mod, a[k + j + i] = (tp1 - tp2 + mod) % mod;

}

}

}

if(flg == 1) return;

ll inv = quickpow(len, mod - 2); reverse(a + 1, a + len);

for(int i = 0; i < len; ++i) a[i] = a[i] * inv % mod;

}

int main()

{

n = read(), m = read();

for(int i = 0; i <= n; ++i) a[i] = read();

for(int i = 0; i <= m; ++i) b[i] = read();

while(len <= n + m) len <<= 1, ++lim;

for(int i = 0; i < len; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lim - 1));

ntt(a, len, 1); ntt(b, len, 1);

for(int i = 0; i < len; ++i) a[i] *= b[i];

ntt(a, len, -1);

for(int i = 0; i <= n + m; ++i) write(a[i]), space; enter;

return 0;

}

  1. </br>
  2. 高精fft这里走:[[CQOI2018]九连环 题解](https://www.cnblogs.com/mrclr/p/10376699.html)

FFT && NTT板子的更多相关文章

  1. [学习笔记&教程] 信号, 集合, 多项式, 以及各种卷积性变换 (FFT,NTT,FWT,FMT)

    目录 信号, 集合, 多项式, 以及卷积性变换 卷积 卷积性变换 傅里叶变换与信号 引入: 信号分析 变换的基础: 复数 傅里叶变换 离散傅里叶变换 FFT 与多项式 \(n\) 次单位复根 消去引理 ...

  2. FFT/NTT/MTT学习笔记

    FFT/NTT/MTT Tags:数学 作业部落 评论地址 前言 这是网上的优秀博客 并不建议初学者看我的博客,因为我也不是很了解FFT的具体原理 一.概述 两个多项式相乘,不用\(N^2\),通过\ ...

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

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

  4. FFT \ NTT总结(多项式的构造方法)

    前言.FFT  NTT 算法 网上有很多,这里不再赘述. 模板见我的代码库: FFT:戳我 NTT:戳我 正经向:FFT题目解题思路 \(FFT\)这个玩意不可能直接裸考的..... 其实一般\(FF ...

  5. FFT&NTT总结

    FFT&NTT总结 一些概念 \(DFT:\)离散傅里叶变换\(\rightarrow O(n^2)\)计算多项式卷积 \(FFT:\)快速傅里叶变换\(\rightarrow O(nlogn ...

  6. 快速构造FFT/NTT

    @(学习笔记)[FFT, NTT] 问题概述 给出两个次数为\(n\)的多项式\(A\)和\(B\), 要求在\(O(n \log n)\)内求出它们的卷积, 即对于结果\(C\)的每一项, 都有\[ ...

  7. FFT/NTT模板 既 HDU1402 A * B Problem Plus

    @(学习笔记)[FFT, NTT] Problem Description Calculate A * B. Input Each line will contain two integers A a ...

  8. FFT/NTT基础题总结

    在学各种数各种反演之前把以前做的$FFT$/$NTT$的题整理一遍 还请数论$dalao$口下留情 T1快速傅立叶之二 题目中要求求出 $c_k=\sum\limits_{i=k}^{n-1}a_i* ...

  9. $FFT/NTT/FWT$题单&简要题解

    打算写一个多项式总结. 虽然自己菜得太真实了. 好像四级标题太小了,下次写博客的时候再考虑一下. 模板 \(FFT\)模板 #include <iostream> #include < ...

随机推荐

  1. Oracle XE快捷版(速成版)的限制

    1.CPU上限:无论把数据库安装在多少核的服务器上,都只会提供一个CPU核心的运算能力 2.安装和执行限制:只能安装一个实例且只能运行一个实例 3.用户数据上限:最大11G的用户数据 4.内存使用上限 ...

  2. C# fileUpload视频上传

    要实现大文件上传必须配置webConfig例如: <system.web> <compilation debug="true" targetFramework=& ...

  3. SourceTree这是一个无效的源路径

    工具->选项:修改一般下面的SSH客户端为OpenSSH

  4. Java - "JUC线程池" Callable与Future

    Java多线程系列--“JUC线程池”06之 Callable和Future Callable 和 Future 简介 Callable 和 Future 是比较有趣的一对组合.当我们需要获取线程的执 ...

  5. C#进行数据筛选(二)

    这里介绍LINQ+Lambda表达式进行数据筛选的方式 这里是第一种方式,还是使用了if条件语句去判断,根据选择的条件去筛选出我所需要的数据 public GxAnaly SelectDay(stri ...

  6. 【12】外观模式(Facade Pattern)

    一.引言 在软件开发过程中,客户端程序经常会与复杂系统的内部子系统进行耦合,从而导致客户端程序随着子系统的变化而变化.然而为了将复杂系统的内部子系统与客户端之间的依赖解耦,从而就有了外观模式,也称作“ ...

  7. Hibernate中的三种数据状态

    Hibernate中的三种数据状态(临时.持久.游离) 1.临时态(瞬时态) 不存在于session中,也不存在于数据库中的数据,被称为临时态. 比如:刚刚使用new关键字创建出的对象. 2.持久态 ...

  8. DVWA的安装

    DVWA(Dam Vulnerable Web Application)是基于PHP+Mysql的一套用于常规Web漏洞教学和检测Web脆弱性测试程序,包含了SQL注入,Xss,盲注等常用的一些安全漏 ...

  9. php简单实现二级联动

    <script type="text/javascript"> //当第一级选项发生变化的时候跳转 function jump() { location.href='? ...

  10. CentOS7上搭建LDAP-PDC并且将windows 2008 R2加入LDAP-PDC域

    由于测试原因,要涉及到将windows机器加入到ldap域,所以查看各种文档进行ldap-pdc域的搭建,并成功将windows 2008r2加入到ldap-pdc域中.下面简单记录一下搭建过程 Li ...