QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#472114#622. 多项式多点求值Galex#0 0ms0kbC++144.4kb2024-07-11 14:35:262024-07-11 14:35:26

Judging History

你现在查看的是最新测评结果

  • [2024-07-11 14:35:26]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2024-07-11 14:35:26]
  • 提交

answer

#include <bits/stdc++.h>
#define LL long long
using namespace std;

int read() {
	int s = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
		f = (ch == '-' ? -1 : 1), ch = getchar();
	while (ch >= '0' && ch <= '9')
		s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
	return s * f;
}

const int mod = 998244353;

int qpow(int a, int b) {
	int res = 1;
	while (b) {
		if (b & 1)
			res = (LL)res * a % mod;
		a = (LL)a * a % mod, b >>= 1;
	}
	return res;
}

auto fplus = [](int x, int y) {return x + y < mod ? x + y : x + y - mod;};
auto fminus = [](int x, int y) {return x >= y ? x - y : x + mod - y;};
auto Fplus = [](int &x, int y) {x = fplus(x, y);};
auto Fminus = [](int &x, int y) {x = fminus(x, y);};

namespace Poly {
	#define poly vector<int>
	const int g = 3;
	inline void upd(int &a) { a += a >> 31 & mod; }
	poly rev, W[25];
	int getlen(int x) {
		int N = 1, pw = 0;
		while (N < x)
			N <<= 1, pw++;
		rev.resize(N);
		for (int i = 0; i < N; i++)
			rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (pw - 1));
		return N;
	}
	void NTT(poly &a, int tp) {
		int n = a.size();
		for (int i = 0; i < n; i++)
			if (i < rev[i])
				swap(a[i], a[rev[i]]);
		for (int j = 1, c = 0; j < n; j <<= 1, c++) {
			poly w(j);
			for (int i = 0; i < j; i++)
				w[i] = W[c][i];
			for (int i = 0; i < n; i += (j << 1))
				for (int k = 0; k < j; k++) {
					const int y = 1ll * a[i | k | j] * w[k] % mod;
					upd(a[i | k | j] = a[i | k] - y);
					upd(a[i | k] += y - mod);
				}
		}
		if (tp == -1) {
			reverse(a.begin() + 1, a.begin() + n);
			int invn = qpow(n, mod - 2);
			for (int i = 0; i < n; i++)
				a[i] = (LL)a[i] * invn % mod;
		}
	}
	poly mul(poly a, poly b) {
		int n = a.size(), m = b.size(), len = getlen(n + m - 1);
		a.resize(len), b.resize(len), NTT(a, 1), NTT(b, 1);
		for (int i = 0; i < len; i++)
			a[i] = (LL)a[i] * b[i] % mod;
		NTT(a, -1), a.resize(n + m - 1);
		return a;
	}
	void init() {
		int n = (1 << 18);
		for (int j = 1, pw = 0; j < n; j <<= 1, pw++) {
			int ur = qpow(g, (mod - 1) / (j << 1));
			W[pw].resize(j), W[pw][0] = 1;
			for (int i = 1; i < j; i++)
				W[pw][i] = (LL)W[pw][i - 1] * ur % mod;
		}
	}
	poly MOD(poly f, int n) {
		f.resize(n);
		return f;
	}
	poly inv(poly f) {
		int n = f.size();
		if (n == 1)
			return {qpow(f[0], mod - 2)};
		poly tmp = MOD(f, (n + 1) >> 1), h = inv(tmp);
		int N = getlen(n * 2 - 1);
		h.resize(N), f.resize(N);
		NTT(f, 1), NTT(h, 1);
		poly g(N);
		for (int i = 0; i < N; i++)
			g[i] = fminus(fplus(h[i], h[i]), (LL)f[i] * h[i] % mod * h[i] % mod);
		NTT(g, -1), g.resize(n);
		return g;
	}
	poly minus(poly a, poly b) {
		if (a.size() < b.size())
			a.resize(b.size());
		for (int i = 0; i < (int)b.size(); i++)
			Fminus(a[i], b[i]);
		return a;
	}
	void DIV(poly f, poly g, poly &q, poly &r) {
		int n = f.size(), m = g.size();
		if (n < m) {
			q = {0}, r = f;
			return ;
		}
		poly ff = f, gg = g;
		reverse(gg.begin(), gg.end()), reverse(ff.begin(), ff.end());
		if ((int)gg.size() < n - m + 1)
			gg.resize(n - m + 1);
		gg = inv(gg);
		q = mul(gg, ff), q.resize(n - m + 1), reverse(q.begin(), q.end());
		r = minus(f, mul(g, q)), r.resize(m - 1);
	}
	poly MOD(poly f, poly g) {
		poly q, r;
		DIV(f, g, q, r);
		return r;
	}
}

const int MAXN = 64005;

poly P[MAXN << 2];

#define ls (p << 1)
#define rs (p << 1 | 1)
#define mid ((l + r) >> 1)

void build(int l, int r, int p, poly &a) {
	if (l == r) {
		P[p] = {fminus(0, a[l]), 1};
		return ;
	}
	build(l, mid, ls, a), build(mid + 1, r, rs, a);
	P[p] = Poly::mul(P[ls], P[rs]);
}

void calc(int l, int r, int p, poly A, poly &ans, poly &a) {
	A = Poly::MOD(A, P[p]);
	if (r - l <= 200) {
		for (int i = l; i <= r; i++)
			for (int j = 0, pw = 1; j < (int)A.size(); j++, pw = 1ll * pw * a[i] % mod)
				Fplus(ans[i], 1ll * pw * A[j] % mod);
		return ;
	}
	calc(l, mid, ls, A, ans, a), calc(mid + 1, r, rs, A, ans, a);
}

poly calc(poly f, poly a) {
	int m = a.size();
	poly ans(m);
	build(0, m - 1, 1, a);
	calc(0, m - 1, 1, f, ans, a);
	return ans;
}

signed main() {
	Poly::init();
	int n = read() + 1, m = read();
	poly f(n), a(m);
	for (int i = 0; i < n; i++)
		f[i] = read();
	for (int i = 0; i < m; i++)
		a[i] = read();
	poly ans = calc(f, a);
	for (int i = 0; i < m; i++)
		printf("%d\n", ans[i]);
	cerr << 1.0 * clock() / CLOCKS_PER_SEC << endl;
	return 0;
}

詳細信息

Test #1:

score: 0
Time Limit Exceeded

input:

100 94
575336069 33153864 90977269 80162592 25195235 334936051 108161572 14050526 356949084 797375084 805865808 286113858 995555121 938794582 458465004 379862532 563357556 293989886 273730531 13531923 113366106 126368162 405344025 443053020 475686818 734878619 338356543 661401660 834651229 527993675...

output:


result:


Test #2:

score: 0
Time Limit Exceeded

input:

5000 4999
410683245 925831211 726803342 144364185 955318244 291646122 334752751 893945905 484134283 203760731 533867267 813509277 491860093 413174124 584582617 594704162 976489328 978500071 196938934 628117769 169796671 858963950 562124570 582491326 647830593 238623335 20782490 674939336 656529076 2...

output:


result:


Test #3:

score: 0
Time Limit Exceeded

input:

30000 29995
536696866 881441742 356233606 594487396 991820796 695996817 7219464 149265950 843761437 329761701 260625152 80366362 598729314 133794090 12808683 67477659 320740422 878134577 879383179 940923483 660160621 18082378 886078389 524050341 35092018 137623841 988429688 258507355 138475726 75726...

output:


result:


Test #4:

score: 0
Time Limit Exceeded

input:

100000 99989
703908936 826436271 431732352 607460686 960390248 897906950 506352459 662618885 172508812 713410533 704313866 156459539 879660919 98030681 46358006 400134234 121190289 498201666 616888945 210891377 39623412 687350951 269444705 980768130 381802923 553892268 644461704 287608268 554761733 ...

output:


result:


Test #5:

score: 0
Time Limit Exceeded

input:

1000000 999998
326289172 459965021 432610030 381274775 890620650 133203219 755508578 820410129 100497878 978894337 34545975 484258543 341383383 556328539 705716773 985485996 201697555 806763870 456757110 445252781 501965590 655584951 516373423 475444481 554722275 106826011 433893131 385018453 687541...

output:


result: