FFT && NTT板子
贴板子啦……
FFT板子:luogu P3803 【模板】多项式乘法(FFT)
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
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 db PI = acos(-1);
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, m, len = 1, lim = 0, rev[maxn];
struct Comp
{
db x, y;
In Comp operator + (const Comp& oth)const
{
return (Comp){x + oth.x, y + oth.y};
}
In Comp operator - (const Comp& oth)const
{
return (Comp){x - oth.x, y - oth.y};
}
In Comp operator * (const Comp& oth)const
{
return (Comp){x * oth.x - y * oth.y, x * oth.y + y * oth.x};
}
friend In void swap(Comp& a, Comp& b)
{
swap(a.x, b.x); swap(a.y, b.y);
}
}a[maxn], b[maxn];
In void fft(Comp* a, 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)
{
Comp omg = (Comp){cos(PI / i), sin(PI / i) * flg};
for(int j = 0; j < len; j += (i << 1))
{
Comp o = (Comp){1, 0};
for(int k = 0; k < i; ++k, o = o * omg)
{
Comp tp1 = a[k + j], tp2 = o * a[k + j + i];
a[k + j] = tp1 + tp2, a[k + j + i] = tp1 - tp2;
}
}
}
}
int main()
{
n = read(); m = read();
for(int i = 0; i <= n; ++i) a[i].x = read();
for(int i = 0; i <= m; ++i) b[i].x = 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));
fft(a, 1); fft(b, 1);
for(int i = 0; i < len; ++i) a[i] = a[i] * b[i];
fft(a, -1);
for(int i = 0; i <= n + m; ++i) write((int)(a[i].x / len + 0.5)), space; enter;
return 0;
}
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;
}
</br>
高精fft这里走:[[CQOI2018]九连环 题解](https://www.cnblogs.com/mrclr/p/10376699.html)
FFT && NTT板子的更多相关文章
- [学习笔记&教程] 信号, 集合, 多项式, 以及各种卷积性变换 (FFT,NTT,FWT,FMT)
目录 信号, 集合, 多项式, 以及卷积性变换 卷积 卷积性变换 傅里叶变换与信号 引入: 信号分析 变换的基础: 复数 傅里叶变换 离散傅里叶变换 FFT 与多项式 \(n\) 次单位复根 消去引理 ...
- FFT/NTT/MTT学习笔记
FFT/NTT/MTT Tags:数学 作业部落 评论地址 前言 这是网上的优秀博客 并不建议初学者看我的博客,因为我也不是很了解FFT的具体原理 一.概述 两个多项式相乘,不用\(N^2\),通过\ ...
- FFT/NTT复习笔记&多项式&生成函数学习笔记Ⅰ
众所周知,tzc 在 2019 年(12 月 31 日)就第一次开始接触多项式相关算法,可到 2021 年(1 月 1 日)才开始写这篇 blog. 感觉自己开了个大坑( 多项式 多项式乘法 好吧这个 ...
- FFT \ NTT总结(多项式的构造方法)
前言.FFT NTT 算法 网上有很多,这里不再赘述. 模板见我的代码库: FFT:戳我 NTT:戳我 正经向:FFT题目解题思路 \(FFT\)这个玩意不可能直接裸考的..... 其实一般\(FF ...
- FFT&NTT总结
FFT&NTT总结 一些概念 \(DFT:\)离散傅里叶变换\(\rightarrow O(n^2)\)计算多项式卷积 \(FFT:\)快速傅里叶变换\(\rightarrow O(nlogn ...
- 快速构造FFT/NTT
@(学习笔记)[FFT, NTT] 问题概述 给出两个次数为\(n\)的多项式\(A\)和\(B\), 要求在\(O(n \log n)\)内求出它们的卷积, 即对于结果\(C\)的每一项, 都有\[ ...
- FFT/NTT模板 既 HDU1402 A * B Problem Plus
@(学习笔记)[FFT, NTT] Problem Description Calculate A * B. Input Each line will contain two integers A a ...
- FFT/NTT基础题总结
在学各种数各种反演之前把以前做的$FFT$/$NTT$的题整理一遍 还请数论$dalao$口下留情 T1快速傅立叶之二 题目中要求求出 $c_k=\sum\limits_{i=k}^{n-1}a_i* ...
- $FFT/NTT/FWT$题单&简要题解
打算写一个多项式总结. 虽然自己菜得太真实了. 好像四级标题太小了,下次写博客的时候再考虑一下. 模板 \(FFT\)模板 #include <iostream> #include < ...
随机推荐
- hdu 1568 (log取对数 / Fib数通项公式)
hdu 1568 (log取对数 / Fib数通项公式) 2007年到来了.经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列 (f[0]=0,f[1]= ...
- CentOS压力测试 ab 命令安装与使用
Apache安装包中自带的压力测试工具 Apache Benchmark(简称ab) 简单易用,这里就采用 ab作为压力测试工具了. 1.独立安装 ab运行需要依赖apr-util包,安装命令为: y ...
- Linux安装mysql教程
安装之前需要先卸载mysql 1.1.下载压缩包 [root@guohaien package]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7 ...
- 【学习笔记】--- 老男孩学Python,day2
今天正式上课的第一天,好害怕.好紧张.好激动.好兴奋! 第一天的课程还算是比较简单吧,因为之前断断续续学的就是这点东西,算是这些都比较熟了吧 今天学习了如下课程: 1. 简单计算机组成 2. pyth ...
- linux下将本地文件上传到github中?
今天编写一份Python基础代码,经过Linux上传到github上,遇到点问题,已经解决 1.首先sudo su 进入root 用户 2.ls 检查出当前文件下有什么文件 3. cd 进入你将要上传 ...
- webpack4.0在Mac下的安装配置及踩到的坑
一.什么是webpack是一个前端资源加载/打包工具.它将根据模块的依赖关系进行静态分析,然后将这些模块按照指定的规则生成对应的静态资源.它做的事情是,分析你的项目结构,找到JavaScript模块以 ...
- jQuery同步Ajax带来的UI线程阻塞问题
一.需求 在调ajax接口的时候因为有时间延迟,想要做一个loading加载的效果,等数据返回时再把loading的效果去掉. 所以我在调ajax的代码块前面加了显示loading效果的代码,ajax ...
- [Android] 压缩图片并保存
不难,但用的时候有时候突然会想不起来..记录一下吧 原文地址请保留http://www.cnblogs.com/rossoneri/p/3995096.html 先加权限 <uses-permi ...
- 类与接口(三)java中的接口与嵌套接口
一.接口 1. 接口简介 接口: 是java的一种抽象类型,是抽象方法的集合.接口比抽象类更加抽象的抽象类型. 接口语法: [修饰符] [abstract] interface 接口名 [extend ...
- 【Python】keras卷积神经网络识别mnist
卷积神经网络的结构我随意设了一个. 结构大概是下面这个样子: 代码如下: import numpy as np from keras.preprocessing import image from k ...