提交记录 11901


用户 题目 状态 得分 用时 内存 语言 代码长度
negiizhao 1002i. 【模板题】多项式乘法 Accepted 100 18.138 ms 7584 KB C++11 3.91 KB
提交时间 评测时间
2020-02-24 11:08:11 2020-08-01 02:48:57
#include <cstdio>
#include <cctype>
#include <algorithm>

typedef unsigned int uint;
typedef long long unsigned int uint64;

struct IO_Tp
{
	const static int _I_Buffer_Size = 2 << 20;
	char _I_Buffer[_I_Buffer_Size], *_I_pos = _I_Buffer;
	
	const static int _O_Buffer_Size = 2 << 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 = res * 10 + (*_I_pos++ & 15);
		return *this;
	}
	
	IO_Tp &operator>>(uint &res)
	{
		while (!isdigit(*_I_pos))
			++_I_pos;
		res = *_I_pos++ & 15;
		while (isdigit(*_I_pos))
			res = res * 10 + (*_I_pos++ & 15);
		return *this;
	}
	
	IO_Tp &operator<<(uint 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 uint Max_size = 1 << 18 | 5;
constexpr uint g = 3, Mod = 998244353;

struct Z
{
	uint v;
	Z(const uint _v = 0) : v(_v) { }
};

inline Z operator+(const Z &x1, const Z &x2) { return x1.v + x2.v < Mod ? x1.v + x2.v : x1.v + x2.v - Mod; }
inline Z operator-(const Z &x1, const Z &x2) { return x1.v >= x2.v ? x1.v - x2.v : x1.v + Mod - x2.v; }
inline Z operator*(const Z &x1, const Z &x2) { return static_cast<uint64>(x1.v) * x2.v % Mod; }
inline Z &operator*=(Z &x1, const Z &x2) { x1.v = static_cast<uint64>(x1.v) * x2.v % Mod; return x1; }

Z Power(Z Base, int Exp)
{
	Z res = 1;
	for (; Exp; Base *= Base, Exp >>= 1)
		if (Exp & 1)
			res *= Base;
	return res;
}

inline uint mf(uint x)
{
	return (static_cast<uint64>(x) << 32) / Mod;
}

int size;
uint w[Max_size], w_mf[Max_size];

inline void init(int n)
{
	for (size = 2; size < n; size <<= 1)
		;
	Z pr = Power(g, (Mod - 1) / size);
	w[size / 2] = 1, w_mf[size / 2] = mf(w[size / 2]);
	for (int i = 1; i < size / 2; ++i)
		w[size / 2 + i] = (w[size / 2 + i - 1] * pr).v, w_mf[size / 2 + i] = mf(w[size / 2 + i]);
	for (int i = size / 2 - 1; i; --i)
		w[i] = w[i << 1], w_mf[i] = w_mf[i << 1];
}

inline void DIF_NTT_core_2(Z _A[], const int L)
{
	uint *A = reinterpret_cast<uint *>(_A);
	for (int d = L >> 1; d; d >>= 1)
		for (int i = 0; i != L; i += d << 1)
			for (int j = 0; j != d; ++j)
			{
				uint x = A[i + j] + A[i + d + j];
				if (x >= Mod * 2)
					x -= Mod * 2;
				uint64 t = A[i + j] + Mod * 2 - A[i + d + j];
				uint64 q = t * w_mf[d + j] >> 32;
				uint y = t * w[d + j] - q * Mod;
				A[i + j] = x, A[i + d + j] = y;
			}
}

inline void DIT_INTT_core_2(Z _A[], const int L)
{
	uint *A = reinterpret_cast<uint *>(_A);
	for (int d = 1; d != L; d <<= 1)
		for (int i = 0; i != L; i += d << 1)
			for (int j = 0; j != d; ++j)
			{
				uint x = A[i + j];
				if (x >= Mod * 2)
					x -= Mod * 2;
				uint64 y = A[i + d + j];
				uint64 q = y * w_mf[d + j] >> 32;
				uint64 t = y * w[d + j] - q * Mod;
				A[i + j] = x + t, A[i + d + j] = x + Mod * 2 - t;
			}
	std::reverse(A + 1, A + L);
	if (L == 2)
	{
		if (A[0] >= Mod * 2)
			A[0] -= Mod * 2;
		if (A[1] >= Mod * 2)
			A[1] -= Mod * 2;
	}
	int k = __builtin_ctz(L);
	for (int i = 0; i != L; ++i)
	{
		uint64 m = -A[i] & (L - 1);
		A[i] = (A[i] + m * Mod) >> k;
	}
}

inline void normalize_2(Z _A[], const int L)
{
	uint *A = reinterpret_cast<uint *>(_A);
	for (int i = 0; i != L; ++i)
		if (A[i] >= Mod)
			A[i] -= Mod;
}

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

int main(int argc, char **argv)
{
	IO >> N >> M;
	for (int i = 0; i <= N; ++i)
		IO >> A[i].v;
	for (int i = 0; i <= M; ++i)
		IO >> B[i].v;
	
	for (L = 2; L <= N + M; L <<= 1)
		;
	init(L);
	
	DIF_NTT_core_2(A, L), DIF_NTT_core_2(B, L);
	for (int i = 0; i != L; ++i)
		A[i] *= B[i];
	DIT_INTT_core_2(A, L), normalize_2(A, L);
	
	for (int i = 0; i <= N + M; ++i)
		IO << A[i].v << ' ';
	
	return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Subtask #1 Testcase #1295.5 us2 MB + 40 KBAcceptedScore: 0

Subtask #1 Testcase #217.952 ms7 MB + 256 KBAcceptedScore: 100

Subtask #1 Testcase #37.635 ms3 MB + 796 KBAcceptedScore: 0

Subtask #1 Testcase #47.651 ms3 MB + 776 KBAcceptedScore: 0

Subtask #1 Testcase #5295.31 us2 MB + 40 KBAcceptedScore: 0

Subtask #1 Testcase #6296.74 us2 MB + 40 KBAcceptedScore: 0

Subtask #1 Testcase #7296.38 us2 MB + 40 KBAcceptedScore: 0

Subtask #1 Testcase #817.26 ms6 MB + 676 KBAcceptedScore: 0

Subtask #1 Testcase #917.227 ms6 MB + 676 KBAcceptedScore: 0

Subtask #1 Testcase #1016.474 ms6 MB + 72 KBAcceptedScore: 0

Subtask #1 Testcase #1118.138 ms7 MB + 416 KBAcceptedScore: 0

Subtask #1 Testcase #1215.199 ms5 MB + 172 KBAcceptedScore: 0

Subtask #1 Testcase #13296.4 us2 MB + 40 KBAcceptedScore: 0


Judge Duck Online | 评测鸭在线
Server Time: 2024-04-19 13:52:32 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用