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\)块积木时的总的层 ...
随机推荐
- Odd-e CSD Course Day 2
首先在第二天中其實談的更多的是在於 Test-Driven 的部分,而第一天談的偏向如何寫出一個好的 A-TDD 案例 但在第二天開始,就不太會照固定的 Topic 進行講述,而且讓團隊成員就像一個真 ...
- .net core mvc 区域路由设置(配置)
写博客原因:添加了区域(用作后台)后,报错: An unhandled exception occurred while processing the request.AmbiguousActionE ...
- 基于C#&.net2.0的windows服务创建与安装
起因:一台服务器中部署的程序,停电后未按照计划任务正常启动. 一.创建并配置Windows服务程序 开发工具VS2015 Framework版本2.0 1.新建Windows服务 2.在Service ...
- 学JAVA第七天,循环深入了解
因为星期五放假,所以今天补回. 上次已经解释过循环了,现在我们来进一步了解. 例如for循环:for( int i=0 : i<10 : i++ ){需要循环的内容},这样就会循环10次了 如果 ...
- Java开发笔记(三十八)利用正则表达式校验字符串
前面多次提到了正则串.正则表达式,那么正则表达式究竟是符合什么定义的字符串呢?正则表达式是编程语言处理字符串格式的一种逻辑式子,它利用若干保留字符定义了形形色色的匹配规则,从而通过一个式子来覆盖满足了 ...
- Spring框架基础(下)
log4J 导入log4J.jar 创建log4J.properties # Create a file called log4j.properties as shown below and plac ...
- [ SHELL编程 ] 字符串空格和文件空行删除
1.删除字符串中空格 (1)删除行首空格 (2)删除行尾空格 (3)删除前.后空格,不删除中间空格 (4) 删除字符串中所有空格 echo " 123 567 " | sed 's ...
- iOS----------检测app进入后台或前台
开发播放器的时候,经常需要检测app进入后台(暂停播放)或者进入前台(开始播放).方法非常简单. 1.检测app进入后台 // 在AppDelete实现该方法 - (void)applicationD ...
- Emmet 简介
Emmet 简介 Intro 什么是 Emmet? Emmet is a plugin for many popular text editors which greatly improves HTM ...
- android SDK 无法更新
android-windows-sdk无法更新解决办法: 1.在host文件新增如下配置 (host文件位置:c:\Windows\System32\drivers\etc文件夹下面,用文本编辑器 ...