提交记录 6995


用户 题目 状态 得分 用时 内存 语言 代码长度
negiizhao 1002i. 【模板题】多项式乘法 Accepted 100 21.762 ms 20904 KB C++11 3.55 KB
提交时间 评测时间
2018-12-06 23:12:48 2020-08-01 00:56:44
#include <cstdio>
#include <cctype>
#include <cmath>
#include <algorithm>

typedef long long int64;

struct IO_Tp
{
	const static int _I_Buffer_Size = 4 << 20;
	char _I_Buffer[_I_Buffer_Size], *_I_pos = _I_Buffer;
	
	const static int _O_Buffer_Size = 4 << 20;
	char _O_Buffer[_O_Buffer_Size], *_O_pos = _O_Buffer;
	
	IO_Tp()
	{
		fread(_I_Buffer, 1, _I_Buffer_Size, stdin);
	}
	
	~IO_Tp()
	{
		fwrite(_O_Buffer, 1, _O_pos - _O_Buffer, stdout);
	}
	
	IO_Tp &operator>>(int &res)
	{
		while (!isdigit(*_I_pos))
			++_I_pos;
		res = *_I_pos++ & 15;
		while (isdigit(*_I_pos))
			(res *= 10) += *_I_pos++ & 15;
		return *this;
	}
	
	IO_Tp &operator<<(int n)
	{
		static char _buf[10];
		char* _pos = _buf;
		do
			*_pos++ = '0' + n % 10;
		while (n /= 10);
		while (_pos != _buf)
			*_O_pos++ = *--_pos;
		return *this;
	}
	
	IO_Tp &operator<<(char ch)
	{
		*_O_pos++ = ch;
		return *this;
	}
} IO;

constexpr double Pi = 3.1415926535897932384626433832795;
constexpr int Max_size = 1 << 18 | 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];

int main(int argc, char **argv)
{
	IO >> N >> M;
	for (int i = 0, x; i <= N; ++i)
		IO >> x, A[i].R = x;
	for (int i = 0, x; i <= M; ++i)
		IO >> x, B[i].R = x;
	
	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)
		IO << static_cast<int>(A[i].R + 0.5) << ' ';
	
	return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Subtask #1 Testcase #113.49 us56 KBAcceptedScore: 0

Subtask #1 Testcase #221.5 ms20 MB + 264 KBAcceptedScore: 100

Subtask #1 Testcase #38.755 ms9 MB + 300 KBAcceptedScore: 0

Subtask #1 Testcase #48.741 ms9 MB + 280 KBAcceptedScore: 0

Subtask #1 Testcase #514.21 us56 KBAcceptedScore: 0

Subtask #1 Testcase #613.54 us56 KBAcceptedScore: 0

Subtask #1 Testcase #713.88 us56 KBAcceptedScore: 0

Subtask #1 Testcase #820.726 ms19 MB + 684 KBAcceptedScore: 0

Subtask #1 Testcase #920.762 ms19 MB + 684 KBAcceptedScore: 0

Subtask #1 Testcase #1020.008 ms19 MB + 80 KBAcceptedScore: 0

Subtask #1 Testcase #1121.762 ms20 MB + 424 KBAcceptedScore: 0

Subtask #1 Testcase #1218.676 ms18 MB + 180 KBAcceptedScore: 0

Subtask #1 Testcase #1312.37 us56 KBAcceptedScore: 0


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