https://www.luogu.org/problemnew/show/P3803

看别人偏偏就是要用NTT去过。实验证明大概是这样用。求0n的多项式和0m的多项式的乘积。注意MAXN取值。A数组的大小必须足以容纳大于等于A+B总size的最小的2的幂次。干脆就直接取4倍?

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; const int MAXN = 4e6, mod = 998244353; inline int pow_mod(ll x, int n) {
ll res;
for(res = 1; n; n >>= 1, x = x * x % mod)
if(n & 1)
res = res * x % mod;
return res;
} inline int add_mod(int x, int y) {
x += y;
return x >= mod ? x - mod : x;
} inline int sub_mod(int x, int y) {
x -= y;
return x < 0 ? x + mod : x;
} void NTT(int a[], int n, int op) {
for(int i = 1, j = n >> 1; i < n - 1; ++i) {
if(i < j)
swap(a[i], a[j]);
int k = n >> 1;
while(k <= j) {
j -= k;
k >>= 1;
}
j += k;
}
for(int len = 2; len <= n; len <<= 1) {
int g = pow_mod(3, (mod - 1) / len);
for(int i = 0; i < n; i += len) {
int w = 1;
for(int j = i; j < i + (len >> 1); ++j) {
int u = a[j], t = 1ll * a[j + (len >> 1)] * w % mod;
a[j] = add_mod(u, t), a[j + (len >> 1)] = sub_mod(u, t);
w = 1ll * w * g % mod;
}
}
}
if(op == -1) {
reverse(a + 1, a + n);
int inv = pow_mod(n, mod - 2);
for(int i = 0; i < n; ++i)
a[i] = 1ll * a[i] * inv % mod;
}
} int A[MAXN + 5], B[MAXN + 5]; int pow2(int x) {
int res = 1;
while(res < x)
res <<= 1;
return res;
} void convolution(int A[], int B[], int Asize, int Bsize) {
int n = pow2(Asize + Bsize - 1);
for(int i = Asize; i < n; ++i)
A[i] = 0;
for(int i = Bsize; i < n; ++i)
B[i] = 0;
NTT(A, n, 1);
NTT(B, n, 1);
for(int i = 0; i < n; ++i)
A[i] = 1ll * A[i] * B[i] % mod;
NTT(A, n, -1);
return;
} int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0; i <= n; ++i) {
scanf("%d", &A[i]);
A[i] = add_mod(A[i], mod);
}
for(int i = 0; i <= m; ++i) {
scanf("%d", &B[i]);
B[i] = add_mod(B[i], mod);
}
convolution(A, B, n + 1, m + 1);
for(int i = 0; i <= n + m; i++) {
printf("%d%c", A[i], " \n"[i == n + m]);
}
return 0;
}

反向学习FFT!不知道为什么不用反过来呢。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; const int MAXN = 4e6;
const double PI = acos(-1.0); struct Complex {
double x, y;
Complex() {}
Complex(double x, double y): x(x), y(y) {}
friend Complex operator+(const Complex &a, const Complex &b) {
return Complex(a.x + b.x, a.y + b.y);
}
friend Complex operator-(const Complex &a, const Complex &b) {
return Complex(a.x - b.x, a.y - b.y);
}
friend Complex operator*(const Complex &a, const Complex &b) {
return Complex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
} A[MAXN + 5], B[MAXN + 5]; void FFT(Complex a[], int n, int op) {
for(int i = 1, j = n >> 1; i < n - 1; ++i) {
if(i < j)
swap(a[i], a[j]);
int k = n >> 1;
while(k <= j) {
j -= k;
k >>= 1;
}
j += k;
}
for(int len = 2; len <= n; len <<= 1) {
Complex wn(cos(2.0 * PI / len), sin(2.0 * PI / len)*op);
for(int i = 0; i < n; i += len) {
Complex w(1.0, 0.0);
for(int j = i; j < i + (len >> 1); ++j) {
Complex u = a[j], t = a[j + (len >> 1)] * w ;
a[j] = u + t, a[j + (len >> 1)] = u - t;
w = w * wn;
}
}
}
if(op == -1) {
for(int i = 0; i < n; ++i)
a[i].x = (int)(a[i].x / n + 0.5);
}
} int pow2(int x) {
int res = 1;
while(res < x)
res <<= 1;
return res;
} void convolution(Complex A[], Complex B[], int Asize, int Bsize) {
int n = pow2(Asize + Bsize - 1);
for(int i = 0; i < n; ++i) {
A[i].y = 0.0;
B[i].y = 0.0;
}
for(int i = Asize; i < n; ++i)
A[i].x = 0;
for(int i = Bsize; i < n; ++i)
B[i].x = 0;
FFT(A, n, 1);
FFT(B, n, 1);
for(int i = 0; i < n; ++i)
A[i] = A[i] * B[i];
FFT(A, n, -1);
return;
} int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
int n, m;
scanf("%d%d", &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);
}
convolution(A, B, n + 1, m + 1);
for(int i = 0; i <= n + m; i++) {
printf("%d%c", (int)A[i].x, " \n"[i == n + m]);
}
return 0;
}

洛谷 - P3803 -【模板】多项式乘法(FFT) - NTT的更多相关文章

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

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

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

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

  3. 洛谷.4238.[模板]多项式求逆(NTT)

    题目链接 设多项式\(f(x)\)在模\(x^n\)下的逆元为\(g(x)\) \[f(x)g(x)\equiv 1\ (mod\ x^n)\] \[f(x)g(x)-1\equiv 0\ (mod\ ...

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

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

  5. 洛谷.4512.[模板]多项式除法(NTT)

    题目链接 多项式除法 & 取模 很神奇,记录一下. 只是主要部分,更详细的和其它内容看这吧. 给定一个\(n\)次多项式\(A(x)\)和\(m\)次多项式\(D(x)\),求\(deg(Q) ...

  6. 洛谷 P4512 [模板] 多项式除法

    题目:https://www.luogu.org/problemnew/show/P4512 看博客:https://www.cnblogs.com/owenyu/p/6724611.html htt ...

  7. 洛谷 P4238 [模板] 多项式求逆

    题目:https://www.luogu.org/problemnew/show/P4238 看博客:https://www.cnblogs.com/xiefengze1/p/9107752.html ...

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

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

  9. 洛谷p3803 FFT入门

    洛谷p3803 FFT入门 ps:花了我一天的时间弄懂fft的原理,感觉fft的折半很神奇! 大致谈一谈FFT的基本原理: 对于两个多项式的卷积,可以O(n^2)求出来(妥妥的暴力) 显然一个多项式可 ...

  10. 【Uoj34】多项式乘法(NTT,FFT)

    [Uoj34]多项式乘法(NTT,FFT) 题面 uoj 题解 首先多项式乘法用\(FFT\)是一个很久很久以前就写过的东西 直接贴一下代码吧.. #include<iostream> # ...

随机推荐

  1. Node.js 几个重启工程的工具

    pm2, forever, nodemon, supervisor 均可在 npm 查找相关资料和用法. 线上工程推荐 pm2 开发推荐 supervisor

  2. Pollard's Rho算法简单总结

    先贴一份代码在这. 最近几天实在是太忙了没时间更新了. 代码 #include <iostream> #include <cstdio> #include <cstdli ...

  3. 关于TCP/IP,必须知道的十个知识点(转)

    三次握手四次挥手可参考:http三次握手,四次挥手 本文整理了一些TCP/IP协议簇中需要必知必会的十大问题,既是面试高频问题,又是程序员必备基础素养. 一.TCP/IP模型 TCP/IP协议模型(T ...

  4. Java 实现日期 Date 的赋值

    关键的语句也就三句话: (1) SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); (2) Date ...

  5. 使用注解装配Bean

    注解@Component代表Spring Ioc 会把 这个类扫描生产Bean 实例,而其中 value属性代表这个类在Spring 中的id,这就相当于XML方式定义的Bean  的 id 现在有了 ...

  6. pycharm中如何安装使用jieba(结巴)

    PyCharm的安装以及jieba包导入 1.打开Pycharm,点击左上角  >>File  >>Settings 2.在settings界面中点击Project :pyCh ...

  7. Oracle JET mobile cordove navigator.app对象

    在使用 Oracle JET 开发 webapp 时,会使用到 ojrouter ,ojrouter 默认含有历史记录推送功能.在调试 Android 时会发现返回键总是返回到上一次浏览记录(App ...

  8. linux测试某进程占用oi、cpu、内存的使用情况

    pidstat 概述 pidstat是sysstat工具的一个命令,用于监控全部或指定进程的cpu.内存.线程.设备IO等系统资源的占用情况.pidstat首次运行时显示自系统启动开始的各项统计信息, ...

  9. VGA接口一根针折了

    注意!!要由 针 对照着 接口 看!!别看反了! VGA接头图如下: VGA接口,15根针,其对应接口定义如下: 1红基色 red 2 绿基色 green 3 蓝基色 blue 4 地址码 ID Bi ...

  10. 安装 Windows 系统在 NVMe 规范的 M.2 接口的固态硬盘(SSD)上

    作为一个程序员很重要的一项技能就是装系统 @_@,以前我都是随便用网上的工具做个系统盘,每次要用直接随手就搞好了,节省大家时间. 但最近同事装了个贼小的固态,然后我启动盘里的系统果断识别不出来他的固态 ...