参考资料

picks

miskcoo

menci

胡小兔

unname

自为风月马前卒

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

原根

例题:luogu3803

fft优化后模板

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int n, m, lim=1, rev[2100005];
const double PI=acos(-1.0);
struct Complex{
double x, y;
Complex(double xx=0.0, double yy=0.0){
x = xx;
y = yy;
}
Complex operator+(const Complex &u)const{
return Complex(x+u.x, y+u.y);
}
Complex operator-(const Complex &u)const{
return Complex(x-u.x, y-u.y);
}
Complex operator*(const Complex &u)const{
return Complex(x*u.x-y*u.y, x*u.y+y*u.x);
}
}a[2100005], b[2100005];
template<typename T> void rn(T &x){
x = 0;
char ch=getchar();
while(ch<'0' || ch>'9') ch = getchar();
while(ch>='0' && ch<='9'){
x = x * 10 + ch - '0';
ch = getchar();
}
}
void fft(Complex a[], int opt){
for(int i=0; i<lim; i++)
if(i<rev[i])
swap(a[i], a[rev[i]]);
for(int i=2; i<=lim; i<<=1){
int tmp=i>>1;
Complex wn=Complex(cos(PI*2.0/i), opt*sin(PI*2.0/i));
for(int j=0; j<lim; j+=i){
Complex w=Complex(1.0, 0.0);
for(int k=0; k<tmp; k++){
Complex tmp1=a[j+k], tmp2=w*a[j+k+tmp];
a[j+k] = tmp1 + tmp2;
a[j+k+tmp] = tmp1 - tmp2;
w = w * wn;
}
}
}
if(opt==-1)
for(int i=0; i<lim; i++)
a[i].x /= lim;
}
int main(){
cin>>n>>m;
for(int i=0; i<=n; i++) rn(a[i].x);
for(int i=0; i<=m; i++) rn(b[i].x);
int tmpcnt=0;
while(lim<=n+m) lim <<= 1, tmpcnt++;
for(int i=0; i<lim; i++)
rev[i] = (rev[i>>1]>>1) | ((i&1)<<(tmpcnt-1));
fft(a, 1);
fft(b, 1);
for(int i=0; i<lim; i++)
a[i] = a[i] * b[i];
fft(a, -1);
for(int i=0; i<=n+m; i++)
printf("%d ", (int)(a[i].x+0.5));
printf("\n");
return 0;
}

NTT

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
int n, m, a[2100005], b[2100005], lim=1, limcnt, rev[2100005];
const int mod=998244353, gg=3, gi=332748118;
void rn(int &x){
char ch=getchar();
x = 0;
while(ch<'0' || ch>'9') ch = getchar();
while(ch>='0' && ch<='9'){
x = x * 10 + ch - '0';
ch = getchar();
}
}
int ksm(int a, int b){
int re=1;
while(b){
if(b&1) re = (ll)re * a % mod;
a = (ll)a * a % mod;
b >>= 1;
}
return re;
}
void ntt(int a[], int opt){
for(int i=0; i<lim; i++)
if(i<rev[i])
swap(a[i], a[rev[i]]);
for(int i=2; i<=lim; i<<=1){
int tmp=i>>1, wn=ksm(opt==1?gg:gi, (mod-1)/i);
for(int j=0; j<lim; j+=i){
int w=1;
for(int k=0; k<tmp; k++){
int tmp1=a[j+k], tmp2=(ll)w*a[j+k+tmp]%mod;
a[j+k] = (tmp1 + tmp2) % mod;
a[j+k+tmp] = (tmp1 - tmp2 + mod) % mod;
w = (ll)w * wn % mod;
}
}
}
if(opt==-1){
int inv=ksm(lim, mod-2);
for(int i=0; i<lim; i++)
a[i] = (ll)a[i] * inv % mod;
}
}
int main(){
cin>>n>>m;
for(int i=0; i<=n; i++) rn(a[i]);
for(int i=0; i<=m; i++) rn(b[i]);
while(lim<=n+m) lim <<= 1, limcnt++;
for(int i=0; i<lim; i++)
rev[i] = (rev[i>>1]>>1) | ((i&1)<<(limcnt-1));
ntt(a, 1);
ntt(b, 1);
for(int i=0; i<lim; i++)
a[i] = (ll)a[i] * b[i] % mod;
ntt(a, -1);
for(int i=0; i<=n+m; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}

递归版裸fft没什么优化

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int n, m;
const double PI=acos(-1.0);
struct Complex{
double x, y;
Complex(double xx=0.0, double yy=0.0){
x = xx;
y = yy;
}
Complex operator+(const Complex &u)const{
return Complex(x+u.x, y+u.y);
}
Complex operator-(const Complex &u)const{
return Complex(x-u.x, y-u.y);
}
Complex operator*(const Complex &u)const{
return Complex(x*u.x-y*u.y, x*u.y+y*u.x);
}
}a[4000005], b[4000005], buf[4000005];
void fft(Complex a[], int lim, int opt){
if(lim==1) return ;
int tmp=lim/2;
for(int i=0; i<tmp; i++){
buf[i] = a[2*i];
buf[i+tmp] = a[2*i+1];
}
for(int i=0; i<lim; i++)
a[i] = buf[i];
fft(a, tmp, opt);
fft(a+tmp, tmp, opt);
Complex wn=Complex(cos(PI*2.0/lim), opt*sin(PI*2.0/lim)), w=Complex(1.0, 0.0);
for(int i=0; i<tmp; i++){
buf[i] = a[i] + w * a[i+tmp];
buf[i+tmp] = a[i] - w * a[i+tmp];
w = w * wn;
}
for(int i=0; i<lim; i++)
a[i] = buf[i];
}
int main(){
cin>>n>>m;
for(int i=0; i<=n; i++) scanf("%lf", &a[i].x);
for(int i=0; i<=m; i++) scanf("%lf", &b[i].x);
int lim=1;
while(lim<=n+m) lim <<= 1;
fft(a, lim, 1);
fft(b, lim, 1);
for(int i=0; i<=lim; i++)
a[i] = a[i] * b[i];
fft(a, lim, -1);
for(int i=0; i<=n+m; i++)
printf("%d ", (int)(a[i].x/lim+0.5));
printf("\n");
return 0;
}

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. Java基本语法和变量

    1基本语法 1.1 标识符.关键字 在程序中用于定义名称的都为标识符,如文件名称.类名称.方法名称或变量名称等. 在Java中标识符的定义格式由字母.数字._(下划线),$所组成,不能以数字开头, 不 ...

  2. hive中select中DISTINCT的技巧和使用

    hive中select中DISTINCT的技巧和使用 单表的唯一查询用:distinct 多表的唯一查询用:group by 在使用MySQL时,有时需要查询出某个字段不重复的记录,虽然mysql提供 ...

  3. 我们为什么要看《超实用的HTML代码段》

    不知道自己HTML水平如何,不知道HTML5如何进化?看这张图 如果一半以上的你都不会,必须看这本书,阿里一线工程师用代码和功能页面来告诉你每一个技术点. 都会一点,但不知道如何检验自己,看看本书提供 ...

  4. eclipse的垂直选择功能

    快捷键:Alt+Shift+A切换. 光标会变成十字,就可以垂直选择了.

  5. 拷贝文件至U盘——提示:对于目标系统文件过大

    一.问题描述: 在制作U盘启动工具的时候,通常制作出的U盘文件系统是FAT32,但是当需要拷贝进去的系统文件大小超过4GB时,就会提示上述问题 二.解决办法: 1.格式化U盘,在格式化界面“文件系统” ...

  6. 火狐浏览器不支持location.reload()(以改变页面大小时重新刷新页面为例)

    背景:当页面大小改变时需要重新刷新页面,以适应相应的尺寸 解决方法: var url = window.location.href; var parm = parseInt(Math.random() ...

  7. HDU - 5491 The Next 2015 ACM/ICPC Asia Regional Hefei Online

    从D+1开始,对于一个数x,区间[x,x+lowbit(x))内的数字的二进制位上1的数量整体来说是单调不减的,因此可快速得出1在这个区间的取值范围. 每次判断一下有没有和[s1,s2]有没有交集,一 ...

  8. opencv中mat矩阵如何debug

    mat img 看type:img.type() rows:img.rows cols:img.cols channels:img.channels(),channels()这是一个函数,和上面两个有 ...

  9. linux基本命令及使用方法

    shell环境: shell:命令解释器,是Linux 系统的用户界面,提供了用户与内核进行交互操作的一种接口,它接收用户输入的命令并把它送入内核去执行. bash:是GNU 计划中重要的工具软件之一 ...

  10. HTML5<aside>元素

    HTML5<aside>元素用来定义页面文档中主区域内容之外的内容,但之外的内容是与主区域内容相关的. 实例: <article> <h1>这个页面是我开始用htm ...