\(\text{FFT}\) 模板

#include <cstdio>
#include <iostream>
#include <cmath>
#define re register
using namespace std; const int N = 2e6 + 1e5;
int rev[N], n, m; inline int read()
{
char ch = getchar(); int f = 1, x = 0;
while (ch < '0' || ch > '9') f = (ch == '-' ? -1 : f), ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
} const double Pi = acos(-1.0);
struct complex{
double x, y;
inline complex operator + (const complex &a) const {return complex{x + a.x, y + a.y};}
inline complex operator - (const complex &a) const {return complex{x - a.x, y - a.y};}
inline complex operator * (const complex &a) const {return complex{x * a.x - y * a.y, x * a.y + y * a.x};}
}a[N], b[N]; inline void FFT(complex *a, int lim, int inv)
{
if (lim == 1) return;
for(re int i = 0; i < lim; i++)
if (i < rev[i]) swap(a[i], a[rev[i]]);
for(re int mid = 1; mid < lim; mid <<= 1)
{
complex I = complex{cos(Pi / mid), inv * sin(Pi / mid)};
for(re int i = 0; i < lim; i += (mid << 1))
{
complex W = complex{1, 0};
for(re int j = 0; j < mid; j++, W = W * I)
{
complex x = a[i + j], y = W * a[i + j + mid];
a[i + j] = x + y, a[i + j + mid] = x - y;
}
}
}
} int main()
{
n = read(), m = read();
for(re int i = 0; i <= n; i++) a[i].x = read();
for(re int i = 0; i <= m; i++) b[i].x = read(); int limit = 1;
while (limit <= n + m) limit <<= 1;
int bit = 0;
while ((1 << bit) < limit) ++bit;
for(re int i = 0; i < limit; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (bit - 1)); FFT(a, limit, 1), FFT(b, limit, 1);
for(re int i = 0; i < limit; i++) a[i] = a[i] * b[i];
FFT(a, limit, -1);
for(re int i = 0; i <= n + m; i++) printf("%d ", (int)(a[i].x / limit + 0.5));
}

\(\text{NTT}\) 模板

#include <cstdio>
#include <iostream>
#define LL long long
#define re register
using namespace std; const int N = 2e6 + 1e5;
const int P = 998244353, g = 3;
int n, m, rev[N], a[N], b[N]; inline void read(int &x)
{
x = 0; char ch = getchar(); int f = 1;
while (ch < '0' || ch > '9') f = (ch == '-' ? -1 : f), ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
x *= f;
} inline int fpow(int x, int y)
{
int res = 1;
for(; y; y >>= 1)
{
if (y & 1) res = 1LL * res * x % P;
x = 1LL * x * x % P;
}
return res;
} inline void NTT(int *a, int lim, int inv)
{
if (lim == 1) return;
for(re int i = 0; i < lim; i++)
if (i < rev[i]) swap(a[i], a[rev[i]]);
for(re int mid = 1; mid < lim; mid <<= 1)
{
int I = fpow(g, (P - 1) / (mid << 1));
if (inv == -1) I = fpow(I, P - 2);
for(re int i = 0; i < lim; i += (mid << 1))
{
int W = 1;
for(re int j = 0; j < mid; j++, W = 1LL * W * I % P)
{
LL x = a[i + j], y = 1LL * W * a[i + j + mid] % P;
a[i + j] = (x + y) % P, a[i + j + mid] = (x - y + P) % P;
}
}
}
} int main()
{
read(n), read(m);
for(re int i = 0; i <= n; i++) read(a[i]);
for(re int i = 0; i <= m; i++) read(b[i]); int limit = 1;
while (limit <= n + m) limit <<= 1;
int bit = 0;
while ((1 << bit) < limit) ++bit;
for(re int i = 0; i < limit; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (bit - 1)); NTT(a, limit, 1), NTT(b, limit, 1);
for(re int i = 0; i < limit; i++) a[i] = 1LL * a[i] * b[i] % P;
NTT(a, limit, -1);
int inv = fpow(limit, P - 2);
for(re int i = 0; i <= n + m; i++) printf("%d ", 1LL * a[i] * inv % P);
}

LG P3803 【模板】多项式乘法的更多相关文章

  1. P3803 [模板] 多项式乘法 (FFT)

    Rt 注意len要为2的幂 #include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); inli ...

  2. 洛谷.3803.[模板]多项式乘法(FFT)

    题目链接:洛谷.LOJ. FFT相关:快速傅里叶变换(FFT)详解.FFT总结.从多项式乘法到快速傅里叶变换. 5.4 又看了一遍,这个也不错. 2019.3.7 叕看了一遍,推荐这个. #inclu ...

  3. [模板] 多项式: 乘法/求逆/分治fft/微积分/ln/exp/幂

    多项式 代码 const int nsz=(int)4e5+50; const ll nmod=998244353,g=3,ginv=332748118ll; //basic math ll qp(l ...

  4. 洛谷.3803.[模板]多项式乘法(NTT)

    题目链接:洛谷.LOJ. 为什么和那些差那么多啊.. 在这里记一下原根 Definition 阶 若\(a,p\)互质,且\(p>1\),我们称使\(a^n\equiv 1\ (mod\ p)\ ...

  5. FFT/NTT总结+洛谷P3803 【模板】多项式乘法(FFT)(FFT/NTT)

    前言 众所周知,这两个东西都是用来算多项式乘法的. 对于这种常人思维难以理解的东西,就少些理解,多背板子吧! 因此只总结一下思路和代码,什么概念和推式子就靠巨佬们吧 推荐自为风月马前卒巨佬的概念和定理 ...

  6. 洛谷P3803 【模板】多项式乘法(FFT)

    P3803 [模板]多项式乘法(FFT) 题目背景 这是一道FFT模板题 题目描述 给定一个n次多项式F(x),和一个m次多项式G(x). 请求出F(x)和G(x)的卷积. 输入输出格式 输入格式: ...

  7. 洛谷 P3803 【模板】多项式乘法(FFT)

    题目链接:P3803 [模板]多项式乘法(FFT) 题意 给定一个 \(n\) 次多项式 \(F(x)\) 和一个 \(m\) 次多项式 \(G(x)\),求 \(F(x)\) 和 \(G(x)\) ...

  8. 【luogu P3803】【模板】多项式乘法(FFT)

    [模板]多项式乘法(FFT) 题目链接:luogu P3803 题目大意 给你两个多项式,要你求这两个多项式乘起来得到的多项式.(卷积) 思路 系数表示法 就是我们一般来表示一个多项式的方法: \(A ...

  9. 洛谷P3803 【模板】多项式乘法 [NTT]

    题目传送门 多项式乘法 题目描述 给定一个n次多项式F(x),和一个m次多项式G(x). 请求出F(x)和G(x)的卷积. 输入输出格式 输入格式: 第一行2个正整数n,m. 接下来一行n+1个数字, ...

  10. 【总结】对FFT的理解 / 【洛谷 P3803】 【模板】多项式乘法(FFT)

    题目链接 \(\Huge\text{无图,慎入}\) \(FFT\)即快速傅里叶变换,用于加速多项式乘法. 如果暴力做卷积的话就是一个多项式的每个单项式去乘另一个多项式然后加起来,时间复杂度为\(O( ...

随机推荐

  1. Permanently added the RSA host key for IP address '192.30.253.113' to the list of known hosts.

    $git push origin master 报错: Warning: Permanently added the RSA host key for IP address '192.30.253.1 ...

  2. JSON提取器中串联一个接口的多个值传给下个接口(compute concatenation var的实际使用场景)

    实际场景:某功能在前端支持选择多条数据后点击提交,表现在接口是,一次传了多个Id. 问题:需要将上个接口的多个Id串联,传给提交接口. 处理方式:通过JSON提取器勾选:compute concate ...

  3. Java第一课Hello World

    java第一课 Hello World 学习 新建文件夹放写的代码 新建.txt文件,并写入java 输出Hello World 的代码  public class Hello{     public ...

  4. Qt的进程间通信,以服务器的形式,手把手教你VS上进行Qt的COM、ActivedQt Server的开发,比保姆还保姆(一)

    Qt的进程间通信,以Active服务器的形式,手把手教你VS上进行Qt的COM.ActivedQt Server的开发,比保姆还保姆 写在前面,文中的ID有部分对不上,因为我中途改了一下,我建议你在实 ...

  5. Qt栅格布局、ScrollArea和用户选择界面

    用户选择界面 就我们在实际开发的时候可能需要面对这样一个界面 做个demo试试看 其实我们可以分解一下这个界面 就是除了控制相关的内容,最主要的就是这个界面之上,有一个个动态的输入的控件,上面都是学生 ...

  6. 制作 Python Docker 镜像的最佳实践

    概述 ️Reference: 制作容器镜像的最佳实践 这篇文章是关于制作 Python Docker 容器镜像的最佳实践.(2022 年 12 月更新) 最佳实践的目的一方面是为了减小镜像体积,提升 ...

  7. docker registry(私库)搭建,使用,WEB可视化管理部署

    Docker Registry 是Docker官方一个镜像,可以用来储存和分发Docker镜像.目前比较流行的两个镜像私库是Docker Registry ,HarBor 其中HarBor最合适企业级 ...

  8. vue项目引入echarts柱状图

    一.components文件下引入 barCharts.vue文件 <template> <div :class="className" :style=" ...

  9. UVA439 Knight Moves

    原题Vjudge 题目大意 有一个骑士,他可以骑马日字型跳跃,问他从A点到B点最少要几步 解题思路 这题就是一个特别裸的广搜板子 它的主要问题在于输入输出 输入的数据我们可以用\(pair\)读入,第 ...

  10. [OpenCV实战]15 基于深度学习的目标跟踪算法GOTURN

    目录 1 什么是对象跟踪和GOTURN 2 在OpenCV中使用GOTURN 3 GOTURN优缺点 4 参考 在这篇文章中,我们将学习一种基于深度学习的目标跟踪算法GOTURN.GOTURN在Caf ...