FFT与NTT
讲解:http://www.cnblogs.com/poorpool/p/8760748.html
递归版FFT
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
const int MAXN = 4000005;
const double PI = acos(-1);
int init() {
int rv = 0, fh = 1;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') fh = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
rv = (rv<<1) + (rv<<3) + c - '0';
c = getchar();
}
return rv * fh;
}
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[MAXN], b[MAXN], buf[MAXN];
int n, m;
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[i * 2];
buf[tmp + i] = a[i * 2 + 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() {
n = init(); m = init();
for(int i = 0; i <= n; i++) a[i].x = init();
for(int i = 0; i <= m; i++) b[i].x = init();
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int MAXN = 4000005;
const double PI = acos(-1);
int init() {
int rv = 0, fh = 1;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') fh = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
rv = (rv<<1) + (rv<<3) + c - '0';
c = getchar();
}
return rv * fh;
}
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[MAXN], b[MAXN], buf[MAXN];
int n, m, rev[MAXN], lim, limcnt;
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 mid = 1; mid < lim; mid <<= 1) {
Complex wn = Complex(cos(PI / mid), opt * sin(PI / mid));
for(int R = mid << 1, j = 0; j < lim; j += R) {
Complex w = Complex(1.0, 0.0);
for(int k = 0; k < mid; k++) {
Complex x = a[j + k], y = w * a[j + mid + k];
a[j + k] = x + y;
a[j + mid + k] = x - y;
w = w * wn;
}
}
}
}
int main() {
n = init(); m = init();
for(int i = 0; i <= n; i++) a[i].x = init();
for(int i = 0; i <= m; i++) b[i].x = init();
lim = 1;
while(lim <= n + m) {lim <<= 1; limcnt++;}
for(int i = 0; i <= lim; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (limcnt - 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 / lim + 0.5));
}
return 0;
}
NTT
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define ll long long
using namespace std;
const int MAXN = 4000005, MOD = 998244353, gg = 3, gi = 332748118;
ll init() {
ll rv = 0, fh = 1;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') fh = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
rv = (rv<<1) + (rv<<3) + c - '0';
c = getchar();
}
return fh * rv;
}
ll lim = 1, limcnt, rev[MAXN], n, m, a[MAXN], b[MAXN];
ll ksm(ll a, ll k) {
ll ans = 1;
while(k) {
if(k & 1ll) {
(ans *= a) %= MOD;
}
(a *= a) %= MOD;
k >>= 1;
}
return ans;
}
void ntt(ll a[], int opt) {
for(int i = 0; i <= lim; i++) {
if(i < rev[i]) swap(a[i], a[rev[i]]);
}
for(int mid = 1; mid < lim; mid <<= 1) {
ll wn = ksm(opt == 1 ? gg : gi, (MOD - 1) / (mid << 1));
for(int R = mid << 1, j = 0; j < lim; j += R) {
ll w = 1;
for(int k = 0; k < mid; k++) {
ll x = a[j + k], y = w * a[j + mid + k] % MOD;
a[j + k] = (x + y) % MOD;
a[j + mid + k] = (x - y + MOD) % MOD;
(w *= wn) %= MOD;
}
}
}
if(opt == -1) {
ll inv = ksm(lim, MOD - 2);
for(int i = 0; i <= lim; i++) {
(a[i] *= inv) %= MOD;
}
}
}
int main() {
n = init(); m = init();
for(int i = 0; i <= n; i++) {
a[i] = init();
}
for(int i = 0; i <= m; i++) b[i] = init();
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] = a[i] * b[i]) %= MOD;
ntt(a, -1);
for(int i = 0; i <= n + m; i++) {
printf("%lld ", a[i]);
}
printf("\n");
return 0;
}
FFT与NTT的更多相关文章
- 多项式乘法,FFT与NTT
多项式: 多项式?不会 多项式加法: 同类项系数相加: 多项式乘法: A*B=C $A=a_0x^0+a_1x^1+a_2x^2+...+a_ix^i+...+a_{n-1}x^{n-1}$ $B=b ...
- FFT和NTT学习笔记_基础
FFT和NTT学习笔记 算法导论 参考(贺) http://picks.logdown.com/posts/177631-fast-fourier-transform https://blog.csd ...
- fft,ntt总结
一个套路:把式子推成卷积形式,然后用fft或ntt优化求解过程. fft的扩展性不强,不可以在fft函数里多加骚操作--DeepinC T1:多项式乘法 板子题 T2:快速傅立叶之二 另一个板子,小技 ...
- 多项式fft、ntt、fwt 总结
做了四五天的专题,但是并没有刷下多少题.可能一开始就对多项式这块十分困扰,很多细节理解不深. 最简单的形式就是直接两个多项式相乘,也就是多项式卷积,式子是$N^2$的.多项式算法的过程就是把卷积做一种 ...
- FFT与NTT专题
先不管旋转操作,考虑化简这个差异值 $$begin{aligned}sum_{i=1}^n(x_i-y_i-c)^2&=sum_{i=1}^n(x_i-y_i)^2+nc^2-2csum_{i ...
- 卷积FFT、NTT、FWT
先简短几句话说说FFT.... 多项式可用系数和点值表示,n个点可确定一个次数小于n的多项式. 多项式乘积为 f(x)*g(x),显然若已知f(x), g(x)的点值,O(n)可求得多项式乘积的点值. ...
- 多项式的基本运算(FFT和NTT)总结
设参与运算的多项式最高次数是n,那么多项式的加法,减法显然可以在O(n)时间内计算. 所以我们关心的是两个多项式的乘积.朴素的方法需要O(n^2)时间,并不够优秀. 考虑优化. 多项式乘积 方案一:分 ...
- 浅谈FFT、NTT和MTT
前言 \(\text{FFT}\)(快速傅里叶变换)是 \(O(n\log n)\) 解决多项式乘法的一个算法,\(\text{NTT}\)(快速数论变换)则是在模域下的,而 \(\text{MTT} ...
- hdu 1402(FFT乘法 || NTT乘法)
A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 初学Docker
1.基本概念Docker 包括三个基本概念镜像( Image )容器( Container )仓库( Repository )理解了这三个概念,就理解了 Docker 的整个生命周期. 2.Docke ...
- pyhon之99乘法表
1.长方形完整格式 for i in range(1,10): for j in range(1,10): print("%d*%d" %(j,i),end=" &quo ...
- 【TP】TP如何向模板中的js传变量
<input type="hidden" class= "val" value = "{$value}" /> <scri ...
- JZOJ 5461. 【NOIP2017提高A组冲刺11.8】购物
5461. [NOIP2017提高A组冲刺11.8]购物 (File IO): input:shopping.in output:shopping.out Time Limits: 1000 ms ...
- jQuery的select2下拉框的搜索功能(使用select2插件,方便简单)
第一步: 引入我们用使用的插件 jquery: select2: css: js: 第二步: 创建一个html页面,body内容: <div> <select class=" ...
- 数据结构-二叉树(Binary Tree)
#include <stdio.h> #include <string.h> #include <stdlib.h> #define LIST_INIT_SIZE ...
- poj-3253 fence repair(贪心题)
题目描述: Farmer John wants to repair a small length of the fence around the pasture. He measures the fe ...
- Codeforces Round #472 A-D
A题: 题意:就是给你一个长度为n的字符串,有三种颜色,其中有一些‘?’的字符代表未着色,你需要找到至少有两种方法染色,同时满足相邻两个字符间不能相同: 思路:有两种染色方法的前提:首先给定的字符串中 ...
- Java面向对象---泛型
概念 泛型可以解决数据类型的安全问题,主要原理是在类声明的时候通过一个标识表示类中某个属性的类型或者是某个方法的返回值及参数类型. 格式 访问权限 class 类名称<泛型,泛型...>{ ...
- TCP/IP网络编程之基于TCP的服务端/客户端(二)
回声客户端问题 上一章TCP/IP网络编程之基于TCP的服务端/客户端(一)中,我们解释了回声客户端所存在的问题,那么单单是客户端的问题,服务端没有任何问题?是的,服务端没有问题,现在先让我们回顾下服 ...