提交记录 7931


用户 题目 状态 得分 用时 内存 语言 代码长度
zjp_shadow 1002i. 【模板题】多项式乘法 Runtime Error 0 71.442 ms 16704 KB C++11 2.77 KB
提交时间 评测时间
2019-01-25 20:01:19 2020-08-01 01:10:27
#include <bits/stdc++.h>

#define For(i, l, r) for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Rep(i, r) for (register int i = (0), i##end = (int)(r); i < i##end; ++i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl

using namespace std;

using VI = vector<int>;

template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }

template<typename T = int>
inline T read() {
	T x(0), sgn(1); char ch(getchar());
	for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
	for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
	return x * sgn;
}

struct Complex {
	double re, im;
	inline Complex friend operator + (const Complex &lhs, const Complex &rhs) {
		return (Complex) {lhs.re + rhs.re, lhs.im + rhs.im};
	}
	inline Complex friend operator - (const Complex &lhs, const Complex &rhs) {
		return (Complex) {lhs.re - rhs.re, lhs.im - rhs.im};
	}
	inline Complex friend operator * (const Complex &lhs, const Complex &rhs) {
		return (Complex) {lhs.re * rhs.re - lhs.im * rhs.im, lhs.re * rhs.im + lhs.im * rhs.re};
	}
};

const double Pi = acos(-1.0);

template<int Maxn>
struct Poly {

	int rev[Maxn], len;

	void FFT(Complex *P, int opt) {
		Rep (i, len) if (i < rev[i]) swap(P[i], P[rev[i]]);
		for (int i = 2, p = 1; i <= len; p = i, i <<= 1) {
			Complex Wi = (Complex) {cos(2 * Pi / i), opt * sin(2 * Pi / i)};
			for (int j = 0; j < len; j += i) {
				Complex x = (Complex) {1, 0};
				Rep (k, p) {
					Complex u = P[j + k], v = x * P[j + k + p];
					P[j + k] = u + v; P[j + k + p] = u - v;
					x = x * Wi;
				}
			}
		}
		if (!~opt) Rep (i, len) P[i].re /= len;
	}

	Complex A[Maxn], B[Maxn], C[Maxn];

	void Mult(const VI &a, const VI &b, VI &c) {
		int na = a.size() - 1, nb = b.size() - 1, nc = na + nb, cnt = 0;

		for (len = 1; len <= nc; len <<= 1) ++ cnt;
		Rep (i, len) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (cnt - 1));

		Rep (i, len) A[i] = (Complex) {i <= na ? 1.0 * a[i] : 0, 0};
		Rep (i, len) B[i] = (Complex) {i <= nb ? 1.0 * b[i] : 0, 0};

		FFT(A, 1); FFT(B, 1);
		Rep (i, len) C[i] = A[i] * B[i];
		FFT(C, -1);

		c.clear(); c.resize(nc + 1);

		Rep (i, len) c[i] = int(C[i].re + 0.5);
	}

};

void File() {
#ifndef ONLINE_JUDGE
	freopen ("fft.in", "r", stdin);
	freopen ("fft.out", "w", stdout);
#endif
}

Poly<1 << 21> T;

VI a, b, c;

int main() {

	File();

	a.resize(read() + 1);
	b.resize(read() + 1);
	Rep (i, a.size()) a[i] = read();
	Rep (i, b.size()) b[i] = read();

	T.Mult(a, b, c);
	Rep (i, c.size())
		printf ("%d%c", c[i], i == c.size() - 1 ? '\n' : ' ');

	return 0;

}

CompilationN/AN/ACompile OKScore: N/A

Subtask #1 Testcase #137.42 us52 KBAcceptedScore: 0

Subtask #1 Testcase #271.17 ms16 MB + 240 KBRuntime ErrorScore: 0

Subtask #1 Testcase #332.412 ms7 MB + 724 KBRuntime ErrorScore: 0

Subtask #1 Testcase #432.501 ms7 MB + 712 KBRuntime ErrorScore: 0

Subtask #1 Testcase #540.63 us52 KBRuntime ErrorScore: 0

Subtask #1 Testcase #639.09 us52 KBAcceptedScore: 0

Subtask #1 Testcase #739.59 us52 KBAcceptedScore: 0

Subtask #1 Testcase #864.199 ms15 MB + 860 KBRuntime ErrorScore: 0

Subtask #1 Testcase #964.195 ms15 MB + 860 KBRuntime ErrorScore: 0

Subtask #1 Testcase #1057.152 ms15 MB + 456 KBRuntime ErrorScore: 0

Subtask #1 Testcase #1171.442 ms16 MB + 320 KBRuntime ErrorScore: 0

Subtask #1 Testcase #1271.377 ms15 MB + 200 KBRuntime ErrorScore: 0

Subtask #1 Testcase #1336.31 us52 KBAcceptedScore: 0


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