提交记录 5661


用户 题目 状态 得分 用时 内存 语言 代码长度
ciwomuli 1002. 测测你的多项式乘法 Accepted 100 424.922 ms 106140 KB C++ 1.63 KB
提交时间 评测时间
2018-09-01 19:32:02 2020-08-01 00:21:09
#include <algorithm>
#include <cmath>
#include <cstdio>
const double PI = acos(-1);
using namespace std;
struct Virt
{
	double r, i;
	Virt(double a, double b) : r(a), i(b) {}
	Virt()
	{
		Virt(0, 0);
	}
	Virt operator+(const Virt &rch)
	{
		return Virt(r + rch.r, i + rch.i);
	}
	Virt operator-(const Virt &rch)
	{
		return Virt(r - rch.r, i - rch.i);
	}
	Virt operator*(const Virt &rch)
	{
		return Virt(r * rch.r - i * rch.i, r * rch.i + i * rch.r);
	}
};
Virt a[3000100], b[3000100], c[3000100];
void Rader(Virt *F, int len)
{
	int j = len >> 1;
	for (int i = 1; i < len - 1; i++)
	{
		if (i < j)
			swap(F[i], F[j]);
		int k = len >> 1;
		while (j >= k)
		{
			j -= k;
			k >>= 1;
		}
		if (j < k)
			j += k;
	}
}
void DFT(Virt *F, int len, int on)
{
	Rader(F, len);
	for (int h = 2; h <= len; h <<= 1)
	{
		Virt wn(cos(-on * 2 * PI / h), sin(-on * 2 * PI / h));
		for (int j = 0; j < len; j += h)
		{
			Virt w(1, 0);
			for (int k = j; k < j + h / 2; k++)
			{
				Virt u = F[k];
				Virt t = w * F[k + h / 2];
				F[k] = u + t;
				F[k + h / 2] = u - t;
				w = w * wn;
			}
		}
	}
	if (on == -1)
	{
		for (int i = 0; i < len; i++)
		{
			F[i].r /= len;
		}
	}
}
void FFT(Virt *A, Virt *B, Virt *C, int len)
{
	DFT(A, len, 1);
	DFT(B, len, 1);
	for (int i = 0; i < len; i++)
	{
		C[i] = A[i] * B[i];
	}
	DFT(C, len, -1);
}

void poly_multiply(unsigned *aa, int n, unsigned *bb, int m, unsigned *cc)
{
//	scanf("%d%d", &n, &m);
	for (int i = 0; i <= n; i++)
	{
		a[i].r=aa[i];
	}
	for (int i = 0; i <= m; i++)
	{
		b[i].r=bb[i];
	}
	int Lim = 1;
	while (Lim <= n + m)
		Lim <<= 1;
	FFT(a, b, c, Lim);
	for (int i = 0; i <= n + m; i++)
		cc[i]=(int)(c[i].r + 0.5);
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #1424.922 ms103 MB + 668 KBAcceptedScore: 100


Judge Duck Online | 评测鸭在线
Server Time: 2024-11-23 05:28:50 | Loaded in 0 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠