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\)块积木时的总的层 ...
随机推荐
- git http服务免登录实现(免去每次请求用户名密码输入,Visual Studio可用)
最近用了Bonobo搭起了Git服务,弄了个批处理文件来避免每次都要输入用户名密码. 此脚本分为三个步骤:1.添加用户变量HOME:2.添加用户_netrc文件:3.添加windows普通凭据(因为V ...
- [转]eShopOnContainers 看微服务 ①:总体概览
本文转自:https://www.cnblogs.com/tianyamoon/p/10081177.html 一.简介 eShopOnContainers是一个简化版的基于.NET Core和Doc ...
- PhpStorm 安装ApiDebugger
ApiDebugger,是一个开源的接口调试IntelliJ IDEA插件,具有与IDEA一致的界面,无需切换程序即可完成网络API请求,让你的code更加沉浸式. 安装 File->Setti ...
- java Calendar的学习分享
前言: 在我们的日常生活中,常常能看见时间.如:在我们的手机里,在一些网站上也能随处看到时间.那我们在项目的开发中,也常常涉及到时间的处理,对于我们经常会遇到和处理的问题.Java中专门为我们处理时间 ...
- @RequestParam Map<String, Object> paramMap
@RequestParam 请求方式 url = "/edit?device=${device}&type=${type}" Controller @RequestMapp ...
- MySQL高级特性——绑定变量
从MySQL 4.1 版本开始,就支持服务器端的绑定变量,这大大提高了客户端和服务器端数据传输的效率 介绍 当创建一个绑定变量 SQL 时,客户端会向服务器发送一个SQL语句的原型.服务器端收到这个S ...
- 详解Vue Native
译者按: 一家叫GeekyAnts的印度公司开发了Vue Native,基于React Native实现. 原文: Introducing Vue Native 译者: Fundebug 为了保证可读 ...
- java框架之springboot
快速入门 一.helloworld示例 二.springboot单元测试 三.springboot热部署 web开发 整合redis thymeleaf使用 spring-data-jpa使用 整合m ...
- 从Linux上传到Git过程
1.1 实验内容 本次课程讲的是在实验楼的在线环境中,如何使用 Github 去管理在在线环境中使用的代码.配置.资源等实验相关文件,怎样去添加.同步和下拉在远程仓库中的实验文件,以此来维持自身的实验 ...
- Android 中使用 dlib+opencv 实现动态人脸检测
1 概述 完成 Android 相机预览功能以后,在此基础上我使用 dlib 与 opencv 库做了一个关于人脸检测的 demo.该 demo 在相机预览过程中对人脸进行实时检测,并将检测到的人脸用 ...