ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LG P3803 【模板】多项式乘法(FFT)

2021-08-13 20:01:23  阅读:155  来源: 互联网

标签:LG ch int FFT getchar while include 模板 P3803


贴模板

#include <cstdio> 
#include <iostream>
#include <cmath>
#define re register
using namespace std;

const int N = 2e6 + 1e5;
int rev[N], n, m;

inline int read()
{
	char ch = getchar(); int f = 1, x = 0;
	while (ch < '0' || ch > '9') f = (ch == '-' ? -1 : f), ch = getchar();
	while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
	return x * f;
}

const double Pi = acos(-1.0);
struct complex{
	double x, y;
	inline complex operator + (const complex &a) const {return complex{x + a.x, y + a.y};}
	inline complex operator - (const complex &a) const {return complex{x - a.x, y - a.y};}
	inline complex operator * (const complex &a) const {return complex{x * a.x - y * a.y, x * a.y + y * a.x};}
}a[N], b[N];

inline void FFT(complex *a, int lim, int inv)
{
	if (lim == 1) return;
	for(re int i = 0; i < lim; i++)
	if (i < rev[i]) swap(a[i], a[rev[i]]);
	for(re int mid = 1; mid < lim; mid <<= 1)
	{
		complex I = complex{cos(Pi / mid), inv * sin(Pi / mid)};
		for(re int i = 0; i < lim; i += (mid << 1))
		{
			complex W = complex{1, 0};
			for(re int j = 0; j < mid; j++, W = W * I)
			{
				complex x = a[i + j], y = W * a[i + j + mid];
				a[i + j] = x + y, a[i + j + mid] = x - y;
			}
		}
	}
}

int main()
{
	n = read(), m = read();
	for(re int i = 0; i <= n; i++) a[i].x = read();
	for(re int i = 0; i <= m; i++) b[i].x = read();
	
	int limit = 1;
	while (limit <= n + m) limit <<= 1;
	int bit = 0;
	while ((1 << bit) < limit) ++bit;
	for(re int i = 0; i < limit; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (bit - 1));
	
	FFT(a, limit, 1), FFT(b, limit, 1);
	for(re int i = 0; i < limit; i++) a[i] = a[i] * b[i];
	FFT(a, limit, -1);
	for(re int i = 0; i <= n + m; i++) printf("%d ", (int)(a[i].x / limit + 0.5));
}

标签:LG,ch,int,FFT,getchar,while,include,模板,P3803
来源: https://www.cnblogs.com/leiyuanze/p/15138928.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有