NTT板子
不说别的。
这份NTT跑得比FFT快,不知道为什么。
以下代码针对\(10^5\)的数据范围。
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
inline int read() {
int a = 0, c = getchar(), w = 1;
for(; c < '0' || c > '9'; c = getchar()) if(c == '-') w = -1;
for(; c >= '0' && c <= '9'; c = getchar()) a = a * 10 + c - '0';
return a * w;
}
const int md = 998244353, gmd = 3;
inline int add(int x, int y) {
x += y;
return x >= md ? x - md : x;
}
inline void Add(int& x, int y) {
x += y;
if(x >= md) x -= md;
}
inline int sub(int x, int y) {
x -= y;
return x < 0 ? x + md : x;
}
inline int mul(int x, int y) {
return (long long)x*y%md;
}
inline int qpow(int a, int x) {
int ret = 1;
while(x) {
if(x&1) ret = mul(ret, a);
a = mul(a, a);
x >>= 1;
}
return ret;
}
inline int inv(int x) {
return qpow(x, md-2);
}
const int maxn = 1<<17;
int w[2][1<<19], invn[1<<18];
void nttinit() {
for(int i = 0; i <= 18; i++) {
w[1][1<<i] = w[0][1<<i] = 1;
int wn = qpow(gmd, (md-1)/(1<<i+1)), invwn = inv(wn);
for(int j = (1<<i)+1; j < 1<<i+1; j++) {
w[1][j] = mul(w[1][j-1], wn);
w[0][j] = mul(w[0][j-1], invwn);
}
}
for(int i = 1; i <= 1<<18; i <<= 1) invn[i] = inv(i);
}
void ntt(int a[], int n, bool typ) {
for(int i = 1, j = n>>1; i < n; i++) {
if(i < j) swap(a[i], a[j]);
for(int k = n>>1; (j^=k) < k; k >>= 1);
}
for(int i = 1; i < n; i <<= 1) for(int j = 0; j < n; j += i<<1) for(int k = 0; k < i; k++) {
int u = a[j+k], v = mul(w[typ][i+k], a[j+i+k]);
a[j+k] = add(u, v);
a[j+i+k] = sub(u, v);
}
if(!typ) for(int i = 0; i < n; i++) a[i] = mul(a[i], invn[n]);
}
int tmp[maxn<<1];
void Mul(int a[], int an, int b[], int bn) {
if(an <= 48 || bn <= 48) {
memset(tmp, 0, (an+bn-1)*sizeof(int));
for(int i = 0; i < an; i++) for(int j = 0; j < bn; j++) Add(tmp[i+j], mul(a[i], b[j]));
memcpy(a, tmp, (an+bn-1)*sizeof(int));
return;
}
int n = 1;
while(n < an+bn-1) n <<= 1;
ntt(a, n, 1); ntt(b, n, 1);
for(int i = 0; i < n; i++) a[i] = mul(a[i], b[i]);
ntt(a, n, 0);
}
int n, m;
int a[maxn<<1], b[maxn<<1];
int main() {
n = read(); m = read();
nttinit();
for(int i = 0; i < n+1; i++) a[i] = read();
for(int i = 0; i < m+1; i++) b[i] = read();
Mul(a, n+1, b, m+1);
for(int i = 0; i < n+m+1; i++) printf("%d ", a[i]);
printf("\n");
return 0;
}
1640ms。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
inline int read() {
int a = 0, c = getchar(), w = 1;
for(; c < '0' || c > '9'; c = getchar()) if(c == '-') w = -1;
for(; c >= '0' && c <= '9'; c = getchar()) a = a * 10 + c - '0';
return a * w;
}
const double pi = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
struct complex {
double r, v;
complex() {}
complex(double rr, double vv) {r = rr; v = vv;}
};
inline complex operator + (complex a, complex b) {
return complex(a.r+b.r, a.v+b.v);
}
inline complex operator - (complex a, complex b) {
return complex(a.r-b.r, a.v-b.v);
}
inline complex operator - (complex x) {
return complex(-x.r, -x.v);
}
inline complex operator * (complex a, complex b) {
return complex(a.r*b.r-a.v*b.v, a.r*b.v+a.v*b.r);
}
const int maxn = 1<<17;
void fft(complex a[], int n, bool typ) {
for(int i = 1, j = n>>1; i < n; i++) {
if(i < j) swap(a[i], a[j]);
for(int k = n>>1; (j^=k) < k; k >>= 1);
}
for(int i = 1; i < n; i <<= 1) for(int j = 0; j < n; j += i<<1) {
complex w = complex(1, 0), wn = complex(cos(pi/(double)i), (typ?1:-1)*sin(pi/(double)i));
for(int k = 0; k < i; k++) {
complex u = a[j+k], v = w * a[j+i+k];
a[j+k] = u + v;
a[j+i+k] = u - v;
w = w * wn;
}
}
if(!typ) for(int i = 0; i < n; i++) a[i].r /= n;
}
complex tmp[maxn<<1];
void Mul(complex a[], int an, complex b[], int bn) {
if(an <= 48 || bn <= 48) {
for(int i = 0; i < an+bn-1; i++) tmp[i] = complex(0, 0);
for(int i = 0; i < an; i++) for(int j = 0; j < bn; j++) tmp[i+j] = tmp[i+j] + a[i] * b[j];
for(int i = 0; i < an+bn-1; i++) a[i] = tmp[i];
return;
}
int n = 1;
while(n < an+bn-1) n <<= 1;
fft(a, n, 1); fft(b, n, 1);
for(int i = 0; i < n; i++) a[i] = a[i] * b[i];
fft(a, n, 0);
}
int n, m;
complex a[maxn<<1], b[maxn<<1];
int main() {
n = read(); m = read();
for(int i = 0; i < n+1; i++) a[i] = complex(read(), 0);
for(int i = 0; i < m+1; i++) b[i] = complex(read(), 0);
Mul(a, n+1, b, m+1);
for(int i = 0; i < n+m+1; i++) printf("%d ", int(a[i].r+0.5));
printf("\n");
return 0;
}
1919ms。
NTT板子的更多相关文章
- FFT && NTT板子
贴板子啦-- FFT板子:luogu P3803 [模板]多项式乘法(FFT) #include<cstdio> #include<iostream> #include< ...
- Codeforces1096G Lucky Tickets(NTT优化dp)
设\(f[i][j]\)表示填了\(i\)个数,数位和为\(j\)的方案数 于是方程为: \[f[i][j]=\sum_{k=0}^9 f[i-1][j-k]*[CanUse[k]==1]\] 其中\ ...
- CF632E: Thief in a Shop(快速幂+NTT)(存疑)
A thief made his way to a shop. As usual he has his lucky knapsack with him. The knapsack can contai ...
- xjoi 2082: 小明的序列
本文为博主原创文章,未均允许…… 反正我也没法管对吧 www点cnblogs点com/AwD-/ 维护一个序列,初始全为\(1\) 支持两种操作: 1.对于所有的位置\(i\),将它的值乘上\(i + ...
- 【比赛游记】FJOI2019瞎打记
\(\mathrm{day}\) \(-4\) 又是睡到中午才起来,这样下去省选会睡迟的. 然后下午在补 WF2019 的题目,很快就能补完的(大雾). \(\mathrm{day}\) \(-3\) ...
- BZOJ 3992 【SDOI2015】 序列统计
题目链接:序列统计 我来复习板子了……这道题也是我写的第一发求原根啊? 求原根方法: 从小到大依次枚举原根.设当前枚举的原根为\(x\),模数为\(p\),\(p-1\)的质因数分别为\(p_1,p_ ...
- PKUWC 2019 自闭记
PKUWC 2019 自闭记 Day -1 考前天天在隔壁的物竞教室划水(雀魂,能和吉老师一起玩的游戏都是好游戏),没有做题. Day 0 早上8:16的高铁,到广州南居然要6个小时...不知道福州和 ...
- 51Nod 1028 大数乘法 V2
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028 分析: FFT/NTT板子题... 代码: NTT板子: #inc ...
- P5162 WD与积木(多项式求逆+生成函数)
传送门 题解 比赛的时候光顾着算某一个\(n\)的答案是多少忘了考虑不同的\(n\)之间的联系了--而且我也很想知道为什么推着推着会变成一个二项式反演-- 设\(f_n\)为\(n\)块积木时的总的层 ...
随机推荐
- 修复UEFI模式下Manjaro Linux启动问题
上周在更新Manjaro Linux的时候误触了电源键,导致内核更新了一半系统强制关机,重启时正常进入grub但无法正常引导进入系统. 由于不想重装系统(一大堆环境和工具的配置还是相当繁琐的),加上初 ...
- 第一册:lesson 105.
原文: Full of mistakes. Where's Sandra,Bob? I want her. Do you want to speak to her? Yes I do. I want ...
- PHP中的__get和__set理解
先来了解一下PHP类中的__get和__set函数 当我们试图获取一个不可达属性时(比如private),类会自动调用__get函数.当试图设置一个不可达属性时(比如private),类会自动调用__ ...
- python学习笔记(十 一)、GUI图形用户界面
python图形用户界面就是包含按钮.输入框.选择框等组件的窗口.主要依赖与工具包进行代码编写.python GUI工具包并发互斥的,你可以选择多个工具包进行安装,有极大选择空间.每个工具包都有不同用 ...
- Java开发笔记(八十)利用反射技术操作私有方法
前面介绍了如何利用反射技术读写私有属性,不单是私有属性,就连私有方法也能通过反射技术来调用.为了演示反射的逆天功能,首先给Chicken鸡类增加下列几个私有方法,简单起见弄来了set***/get** ...
- Flask 系列之 Migration
说明 操作系统:Windows 10 Python 版本:3.7x 虚拟环境管理器:virtualenv 代码编辑器:VS Code 实验目标 通过使用 flask-migrate 实现数据库的迁移操 ...
- vuex2中使用mapGetters/mapActions报错解决方法
解决方案 可以安装整个stage2的预置器或者安装 Object Rest Operator 的babel插件 babel-plugin-transform-object-rest-spread . ...
- python运行逻辑
Python程序在解释器上执行分两个过程: 编译:如果Python进程在机器上拥有写入权限,那么它会把程序的字节码保存为一个以 .pyc 为扩展名的文件.当程序运行后,会在源代码的同一个目录下看到 . ...
- noi.ac #289. 电梯(单调队列)
题意 题目链接 Sol 傻叉的我以为给出的\(t\)是单调递增的,然后\(100\rightarrow0\) 首先可以按\(t\)排序,那么转移方程为 \(f[i] = min_{j=0}^{i-1} ...
- 缓存ABC
缓存ABC Intro 缓存是一种比较常见的用来将提高系统性能的方式.从线程缓存.进程缓存.到内存缓存再到分布式缓存再到CDN,都是属于缓存的范畴. 缓存的本质是空间换时间以提高读的效率,牺牲一些内存 ...