洛谷 - P3803 【模板】多项式乘法(FFT/NTT)
题目链接:点击查看
题目大意:给出两个多项式 F( x ) 和 G( x ) 的系数,求其卷积后的系数
题目分析:存一个FFT的模板,原理学不明白,数论和dp都扔给队友了,当个快乐的fw
代码:
// Problem: P3803 【模板】多项式乘法(FFT)
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3803
// Memory Limit: 500 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) (x&-x)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f;
}
template<typename T>
inline void write(T x)
{if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
const double Pi=acos(-1.0);
struct complex
{double x,y;complex (double xx=0,double yy=0){x=xx,y=yy;}
}A[N<<2],B[N<<2];
complex operator + (complex a,complex b){ return complex(a.x+b.x , a.y+b.y);}
complex operator - (complex a,complex b){ return complex(a.x-b.x , a.y-b.y);}
complex operator * (complex a,complex b){ return complex(a.x*b.x-a.y*b.y , a.x*b.y+a.y*b.x);}
int limit,r[N<<2],res[N<<2];
void FFT(complex *A,int type)
{for(int i=0;i<limit;i++) if(i<r[i]) swap(A[i],A[r[i]]); for(int mid=1;mid<limit;mid<<=1) {complex Wn( cos(Pi/mid) , type*sin(Pi/mid) );for(int R=mid<<1,j=0;j<limit;j+=R) {complex w(1,0);for(int k=0;k<mid;k++,w=w*Wn) {complex x=A[j+k],y=w*A[j+mid+k];A[j+k]=x+y;A[j+mid+k]=x-y;}}}
}
void init(int n) {limit=1;while(limit<=n) limit<<=1;for(int i=1;i<limit;i++) r[i]=r[i>>1]>>1|((i&1)?limit>>1:0);
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int n,m;read(n),read(m);n++,m++;init(n+m);for(int i=0,x;i<n;i++) {read(x);A[i].x=x;}for(int i=0,x;i<m;i++) {read(x);B[i].x=x;}FFT(A,1),FFT(B,1);for(int i=0;i<limit;i++) {A[i]=A[i]*B[i];}FFT(A,-1);for(int i=0;i<limit;i++) {res[i]=floor(A[i].x/limit+0.5);}for(int i=0;i<n+m-1;i++) {printf("%d ",res[i]);}return 0;
}
// Problem: P3803 【模板】多项式乘法(FFT)
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3803
// Memory Limit: 500 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) (x&-x)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f;
}
template<typename T>
inline void write(T x)
{if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=5e6+100;
const int mod=998244353,G=3,Gi=(mod+1)/3;
int limit,L,r[N];
int a[N],b[N];
int q_pow(int a,int b) {int ans=1;while(b) {if(b&1) ans=1LL*ans*a%mod;a=1LL*a*a%mod,b>>=1;}return ans;
}
void NTT(int *A,int type) {for(int i=0;i<limit;i++) if(i<r[i]) swap(A[i],A[r[i]]);for(int mid=1;mid<limit;mid<<=1) { int Wn=q_pow(type==1?G:Gi,(mod-1)/(mid<<1));for(int j=0;j<limit;j+=(mid<<1)) {int w=1;for(int k=0;k<mid;k++,w=1LL*w*Wn%mod) {int x=A[j+k],y=1LL*w*A[j+k+mid]%mod;A[j+k]=(x+y)%mod,A[j+k+mid]=(x-y+mod)%mod;}}}if(type==-1) {int inv=q_pow(limit,mod-2);for(int i=0;i<limit;i++) {A[i]=1LL*A[i]*inv%mod;}}
}
void init(int n,int m) {limit=1;L=0;while(limit<=n+m) limit<<=1,L++;for(int i=0;i<limit;i++) r[i]=(r[i>>1]>>1)|((i&1)<<(L-1));
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int n,m;read(n),read(m);n++,m++;init(n,m);for(int i=0;i<n;i++) {read(a[i]);}for(int i=0;i<m;i++) {read(b[i]);}NTT(a,1),NTT(b,1);for(int i=0;i<limit;i++) {a[i]=1LL*a[i]*b[i]%mod;}NTT(a,-1);for(int i=0;i<n+m-1;i++) {printf("%d ",a[i]);}return 0;
}
洛谷 - P3803 【模板】多项式乘法(FFT/NTT)相关推荐
- 【luogu P3803】【模板】多项式乘法(NTT)
[模板]多项式乘法(NTT) 题目链接:luogu P3803 题目大意 给你两个多项式,要你求它们的卷积. 思路 这次我们写 NTT 的做法. 它的优点就是它可以取模,而且不会有精度问题,而且会比 ...
- python高精度乘法_洛谷P1919--A*B Problem升级版(NTT优化高精度乘法)
题目背景 本题数据已加强,请使用 FFT/NTT,不要再交 Python 代码浪费评测资源. 题目描述 给你两个正整数 a,b,求$ a \times b$. 输入格式 第一行一个正整数,表示 a: ...
- 洛谷 P1919 模板】A*B Problem升级版(FFT快速傅里叶)
https://www.luogu.com.cn/problem/P1919 题目背景 本题数据已加强,请使用 FFT/NTT,不要再交 Python 代码浪费评测资源. 题目描述 给你两个正整数 a ...
- 多项式乘法 FFT模板
题目传送门 在下只是来存个板子,,(板子还是洛谷找的2333) 证明的话,太(wo)难(bu)写(hui),就先留个坑吧,,, NOIP后,如果没退役,我会回来填坑的,, #include<bi ...
- 专题·树链剖分【including 洛谷·【模板】树链剖分
初见安~~~终于学会了树剖~~~ [兴奋]当初机房的大佬在学树剖的时候我反复强调过:"学树剖没有前途的!!!" 恩.真香. 一.重链与重儿子 所谓树剖--树链剖分,就是赋予一个链的 ...
- 洛谷·【模板】点分树 | 震波【including 点分树
初见安-这里是传送门:洛谷P6329 [模板]点分树 | 震波 一.点分树 其实你会点分治的话,点分树就是把点分治时的重心提出来重新连城一棵树. 比如当前点是u,求出子树v的重心root后将root与 ...
- 洛谷P3803 【模板】多项式乘法(FFT)
传送门 FFT我啥都不会,先坑着 1 //minamoto 2 #include<iostream> 3 #include<cstdio> 4 #include<cmat ...
- 洛谷 P3803 多项式乘法
题目背景 这是一道FFT模板题 题目描述 给定一个n次多项式F(x),和一个m次多项式G(x). 请求出F(x)和G(x)的卷积. 输入输出格式 输入格式: 第一行2个正整数n,m. 接下来一行n+1 ...
- 洛谷P3803 fft模板
概念: 先引入多项式的两个表示方法: 1.1.1.系数表示法,唯一确定一个nnn次的多项式需要每一项的系数,从低次项到高次项依次写成向量的形式,这个向量就能唯一确定一个多项式.即向量r=[a0,a1, ...
最新文章
- 2021年大数据Hadoop(二十九):​​​​​​​关于YARN常用参数设置
- Windows 中 FS 段寄存器
- EonerCMS——做一个仿桌面系统的CMS(三)
- Spring学习9之静态代理再理解
- 汇编语言——16位寄存器实现32位二进制数左移4位。
- pytorch注意事项
- Python爬虫1-----------placekitten 入门
- magicAjax问题
- Ubuntu镜像下载地址
- 用jQuery实现banner图片切换
- CGI,FASTCGI,PHP-CGI,PHP-FPM 概念
- html5兼容包,webpack4搭建现代Hybird-h5工程
- Android8.0快捷方式之Shortcuts
- 打开 mhtml 文件 显示不全_解决 Nginx autoindex 显示文件名不全的问题
- 快搜搜:在网上找工作如何防骗!
- websocket + node 手把手实现简陋聊天室
- js ajax上传文件功能
- 【论文翻译】Convolutional Oriented Boundaries
- AIUI的技能工作室使用
- 第690期 | 意大利为什么会出现黑手党?(解决问题不能只看当下)
热门文章
- php 文件名汉字utf8,php utf8编码上传中文文件名出现乱码_PHP教程
- MySQL安全等于的介绍
- MySQL高级 - 案例 - 系统性能优化 - 数据源配置
- MySQL 高级 - 触发器 - 查看及删除
- ActiveMQ入门-ActiveMQ跟SpringBoot整合发送接收Queue
- keepAliveTime和线程工厂
- 创建provider服务
- 解决 Maven 报错 Non-resolvable parent POM ... was cached in the local repository, resolution will not be
- 200827C阶段一_C++基础
- myeclipse中的一些设置