提交记录 6999


用户 题目 状态 得分 用时 内存 语言 代码长度
negiizhao 1002. 测测你的多项式乘法 Accepted 100 242.871 ms 147096 KB C++11 2.73 KB
提交时间 评测时间
2018-12-07 09:40:52 2020-08-01 00:56:48
#include <cstdio>
#include <cctype>
#include <cmath>
#include <algorithm>

constexpr double Pi = 3.1415926535897932384626433832795;
constexpr int Max_size = 1 << 21 | 5;

struct Complex { double R, I; };

inline Complex operator+(const Complex &C1, const Complex &C2) { return Complex{C1.R + C2.R, C1.I + C2.I}; }
inline Complex operator-(const Complex &C1, const Complex &C2) { return Complex{C1.R - C2.R, C1.I - C2.I}; }
inline Complex operator*(const Complex &C1, const Complex &C2) { return Complex{C1.R * C2.R - C1.I * C2.I, C1.R * C2.I + C1.I * C2.R}; }
inline Complex operator~(const Complex &C) { return Complex{C.R, -C.I}; }
inline Complex &operator*=(Complex &C1, const Complex &C2) { return C1 = C1 * C2; }

int size;
int bitrev[Max_size];
Complex w[Max_size];

inline void init(int n)
{
	for (size = 1; size < n; size <<= 1)
		;
	for (int i = 0; i != size; ++i)
		bitrev[i] = bitrev[i >> 1] >> 1 | ((i & 1) * (size >> 1));
	size >>= 1;
	for (int i = 0; i != size; ++i)
		w[size + i] = {cos(Pi * i / size), sin(Pi * i / size)};
	for (int i = size - 1; i; --i)
		w[i] = w[i << 1];
	size <<= 1;
}

inline void DFT(Complex A[], int L)
{
	int shift = __builtin_ctz(size) - __builtin_ctz(L);
	for (int i = 0; i != L; ++i)
		if (i < bitrev[i] >> shift)
			std::swap(A[i], A[bitrev[i] >> shift]);
	for (int d = 1; d != L; d <<= 1)
		for (int i = 0; i != L; i += d << 1)
			for (int j = 0; j != d; ++j)
			{
				Complex tmp = A[i + d + j] * w[d + j];
				A[i + d + j] = A[i + j] - tmp, A[i + j] = A[i + j] + tmp;
			}
}

inline void Real_DFT(Complex A[], int L)
{
	if (L == 1)
		return;
	static Complex tmp[Max_size];
	L >>= 1;
	for (int i = 0; i != L; ++i)
		tmp[i] = {A[i << 1].R, A[i << 1 | 1].R};
	DFT(tmp, L);
	tmp[L] = tmp[0];
	for (int i = 0; i != L; ++i)
	{
		Complex t1 = (tmp[i] + ~tmp[L - i]) * Complex{0.5, 0}, t2 = (tmp[i] - ~tmp[L - i]) * Complex{0, -0.5} * w[L + i];
		A[i] = t1 + t2, A[L + i] = t1 - t2;
	}
}

inline void Real_IDFT(Complex A[], int L)
{
	if (L == 1)
		return;
	static Complex tmp[Max_size];
	A[L] = A[0];
	std::reverse(A + 1, A + L);
	L >>= 1;
	for (int i = 0; i != L; ++i)
	{
		Complex t1 = (A[i] + A[L + i]) * Complex{0.5, 0}, t2 = (A[i] - A[L + i]) * Complex{0, 0.5} * w[L + i];
		tmp[i] = t1 + t2;
	}
	DFT(tmp, L);
	for (int i = 0; i != L; ++i)
		A[i << 1] = {tmp[i].R / L, 0}, A[i << 1 | 1] = {tmp[i].I / L, 0};
}

int N, M, L;
Complex A[Max_size], B[Max_size];

void poly_multiply(unsigned *a, int n, unsigned *b, int m, unsigned *c)
{
	N = n, M = m;
	for (int i = 0; i <= N; ++i)
		A[i].R = a[i];
	for (int i = 0; i <= M; ++i)
		B[i].R = b[i];
	
	for (L = 2; L <= N + M; L <<= 1)
		;
	init(L);
	
	Real_DFT(A, L), Real_DFT(B, L);
	for (int i = 0; i != L; ++i)
		A[i] *= B[i];
	Real_IDFT(A, L);
	
	for (int i = 0; i <= N + M; ++i)
		c[i] = A[i].R + 0.5;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #1242.871 ms143 MB + 664 KBAcceptedScore: 100


Judge Duck Online | 评测鸭在线
Server Time: 2026-04-09 04:08:24 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠