QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#132359#6323. Range NEQUrgantTeam#AC ✓155ms43532kbC++234.2kb2023-07-29 17:37:182023-07-29 17:37:22

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-29 17:37:22]
  • 评测
  • 测评结果:AC
  • 用时:155ms
  • 内存:43532kb
  • [2023-07-29 17:37:18]
  • 提交

answer

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <cstdint>
#include <algorithm>
using namespace std;
using ll = int64_t;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
const int M = 998244353;
const int N = 2000000;
int sum(int a, int b) {
	if ((a += b) >= M) a -= M;
	return a;
}
int dif(int a, int b) {
	if ((a -= b) < 0) a += M;
	return a;
}
int prod(int a, int b) {
	return ll(a) * b % M;
}
int powmod(int a, int b) {
	int ans = 1;
	while (b) {
		if (b & 1) ans = prod(ans, a);
		b >>= 1;
		a = prod(a, a);
	}
	return ans;
}
int inv(int a, int m) {
	return a == 1 ? 1 : (1 - inv(m % a, a) * ll(m)) / a + m;
}
int factorial(int n, bool inverse) {
	static vi fact, ifact;
	if (fact.empty()) {
		fact.resize(N, 1);
		ifact.resize(N, 1);
		for (int i = 2; i < N; ++i)
			fact[i] = prod(i, fact[i - 1]);
		ifact.back() = inv(fact.back(), M);
		for (int i = N - 1; i >= 3; --i)
			ifact[i - 1] = prod(ifact[i], i);
	}
	return inverse ? ifact[n] : fact[n];
}
namespace FFT {
	struct NTT {
		const int G = 3;
		const int ROOT, size, n;
		vi root, revers, fftA, fftB;
		NTT(const int n = 20) : n(n), size(1 << n), ROOT(powmod(G, (M - 1) >> n)), revers(size), root(size, 1), fftA(size), fftB(size) {
			for (int i = 1; i < size; ++i) root[i] = prod(root[i - 1], ROOT);
		}
		void fft(vi& p, int newN) {
			revers[0] = 0;
			for (int i = 1; i < 1 << newN; ++i) {
				if (i % 2 == 0) revers[i] = revers[i / 2] / 2;
				else revers[i] = revers[i / 2] / 2 + (1 << (newN - 1));
				if (revers[i] < i) swap(p[revers[i]], p[i]);
			}
			for (int l = 1; l <= newN; ++l)
				for (int b = 0; b < (1 << newN - l); ++b)
					for (int step = 0; step < (1 << l - 1); ++step) {
						int n1 = (b << l) + step;
						int n2 = n1 + (1 << l - 1);
						int vA = p[n1];
						int vB = prod(root[step << n - l], p[n2]);
						p[n1] = sum(vA, vB);
						p[n2] = dif(vA, vB);
					}
		}
		void rev_fft(vi& p, int step) {
			fft(p, step);
			reverse(p.begin() + 1, p.begin() + (1 << step));
			int inv_size = inv(1 << step, M);
			for (int i = 0; i < 1 << step; ++i) p[i] = prod(p[i], inv_size);
		}
		template<class Iter>
		vi multiply(Iter Ab, Iter Ae, Iter Bb, Iter Be, int step) {
			if (Ab == Ae || Bb == Be) return {};
			fill(fftA.begin(), fftA.begin() + (1 << step), 0);
			fill(fftB.begin(), fftB.begin() + (1 << step), 0);
			for (auto it = Ab; it != Ae; ++it) fftA[it - Ab] = *it % M;
			for (auto it = Bb; it != Be; ++it) fftB[it - Ab] = *it % M;
			fft(fftA, step);
			fft(fftB, step);
			for (int i = 0; i < (1 << step); ++i) fftA[i] = prod(fftA[i], fftB[i]);
			rev_fft(fftA, step);
			vi result(1 << step);
			copy(fftA.begin(), fftA.begin() + (1 << step), result.begin());
			return result;
		}
		template<class Iter>
		vi power(Iter Ab, Iter Ae, int b, int step) {
			if (Ab == Ae) return {};
			fill(fftA.begin(), fftA.begin() + (1 << step), 0);
			for (auto it = Ab; it != Ae; ++it) fftA[it - Ab] = *it % M;
			fft(fftA, step);
			for (int i = 0; i < (1 << step); ++i) fftA[i] = powmod(fftA[i], b);
			rev_fft(fftA, step);
			vi result(1 << step);
			copy(fftA.begin(), fftA.begin() + (1 << step), result.begin());
			return result;
		}
		vi OGF(vi egf) const {
			int fact = 1, n = egf.size();
			for (int i = 2; i < n; ++i) {
				fact = prod(fact, i);
				egf[i] = prod(egf[i], fact);
			}
			return egf;
		}
	};
}
int find_ans(const int n, const int m) {
	vi poly(m + 1);
	for (int k = 0; k <= m; ++k) {
		poly[k] = prod(factorial(m, false), prod(factorial(m, false), prod(factorial(k, true), prod(factorial(k, true), factorial(m - k, true)))));
		if ((k + m) % 2)
			poly[k] = dif(0, poly[k]);
	}
	FFT::NTT fft;
	vi power_poly = fft.power(poly.begin(), poly.end(), n, 20);
	power_poly = fft.OGF(power_poly);
	int ans = 0;
	for (int i = 0; i <= n * m; ++i)
		ans = sum(ans, power_poly[i]);
	return ans;
}
bool solve_test() {
	int n, m;
	if (!(cin >> n >> m)) return false;
	cout << find_ans(n, m) << '\n';
	return true;
}

int main() {
#ifdef HOME
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	while (solve_test());
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 121ms
memory: 43532kb

input:

2 2

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 113ms
memory: 43464kb

input:

5 1

output:

44

result:

ok 1 number(s): "44"

Test #3:

score: 0
Accepted
time: 138ms
memory: 43520kb

input:

167 91

output:

284830080

result:

ok 1 number(s): "284830080"

Test #4:

score: 0
Accepted
time: 111ms
memory: 43292kb

input:

2 1

output:

1

result:

ok 1 number(s): "1"

Test #5:

score: 0
Accepted
time: 112ms
memory: 43256kb

input:

2 3

output:

36

result:

ok 1 number(s): "36"

Test #6:

score: 0
Accepted
time: 113ms
memory: 43484kb

input:

2 4

output:

576

result:

ok 1 number(s): "576"

Test #7:

score: 0
Accepted
time: 121ms
memory: 43276kb

input:

3 1

output:

2

result:

ok 1 number(s): "2"

Test #8:

score: 0
Accepted
time: 115ms
memory: 43488kb

input:

3 2

output:

80

result:

ok 1 number(s): "80"

Test #9:

score: 0
Accepted
time: 106ms
memory: 43492kb

input:

3 3

output:

12096

result:

ok 1 number(s): "12096"

Test #10:

score: 0
Accepted
time: 113ms
memory: 43436kb

input:

3 4

output:

4783104

result:

ok 1 number(s): "4783104"

Test #11:

score: 0
Accepted
time: 122ms
memory: 43488kb

input:

4 1

output:

9

result:

ok 1 number(s): "9"

Test #12:

score: 0
Accepted
time: 112ms
memory: 43452kb

input:

4 2

output:

4752

result:

ok 1 number(s): "4752"

Test #13:

score: 0
Accepted
time: 122ms
memory: 43476kb

input:

4 3

output:

17927568

result:

ok 1 number(s): "17927568"

Test #14:

score: 0
Accepted
time: 112ms
memory: 43264kb

input:

4 4

output:

776703752

result:

ok 1 number(s): "776703752"

Test #15:

score: 0
Accepted
time: 110ms
memory: 43472kb

input:

5 2

output:

440192

result:

ok 1 number(s): "440192"

Test #16:

score: 0
Accepted
time: 112ms
memory: 43332kb

input:

5 3

output:

189125068

result:

ok 1 number(s): "189125068"

Test #17:

score: 0
Accepted
time: 109ms
memory: 43444kb

input:

5 4

output:

975434093

result:

ok 1 number(s): "975434093"

Test #18:

score: 0
Accepted
time: 151ms
memory: 43472kb

input:

1000 1000

output:

720037464

result:

ok 1 number(s): "720037464"

Test #19:

score: 0
Accepted
time: 120ms
memory: 43444kb

input:

72 42

output:

638177567

result:

ok 1 number(s): "638177567"

Test #20:

score: 0
Accepted
time: 125ms
memory: 43460kb

input:

15 19

output:

663050288

result:

ok 1 number(s): "663050288"

Test #21:

score: 0
Accepted
time: 119ms
memory: 43488kb

input:

68 89

output:

94365047

result:

ok 1 number(s): "94365047"

Test #22:

score: 0
Accepted
time: 130ms
memory: 43480kb

input:

92 37

output:

652605307

result:

ok 1 number(s): "652605307"

Test #23:

score: 0
Accepted
time: 134ms
memory: 43524kb

input:

61 87

output:

498277867

result:

ok 1 number(s): "498277867"

Test #24:

score: 0
Accepted
time: 124ms
memory: 43264kb

input:

81 40

output:

133095344

result:

ok 1 number(s): "133095344"

Test #25:

score: 0
Accepted
time: 121ms
memory: 43428kb

input:

7 91

output:

524164813

result:

ok 1 number(s): "524164813"

Test #26:

score: 0
Accepted
time: 129ms
memory: 43284kb

input:

31 18

output:

361233485

result:

ok 1 number(s): "361233485"

Test #27:

score: 0
Accepted
time: 131ms
memory: 43440kb

input:

74 54

output:

500686087

result:

ok 1 number(s): "500686087"

Test #28:

score: 0
Accepted
time: 116ms
memory: 43248kb

input:

32 2

output:

586504335

result:

ok 1 number(s): "586504335"

Test #29:

score: 0
Accepted
time: 140ms
memory: 43444kb

input:

656 718

output:

346764298

result:

ok 1 number(s): "346764298"

Test #30:

score: 0
Accepted
time: 132ms
memory: 43432kb

input:

254 689

output:

358078813

result:

ok 1 number(s): "358078813"

Test #31:

score: 0
Accepted
time: 134ms
memory: 43452kb

input:

713 674

output:

914437613

result:

ok 1 number(s): "914437613"

Test #32:

score: 0
Accepted
time: 134ms
memory: 43456kb

input:

136 698

output:

56687290

result:

ok 1 number(s): "56687290"

Test #33:

score: 0
Accepted
time: 151ms
memory: 43456kb

input:

369 401

output:

312325811

result:

ok 1 number(s): "312325811"

Test #34:

score: 0
Accepted
time: 143ms
memory: 43436kb

input:

280 204

output:

280012063

result:

ok 1 number(s): "280012063"

Test #35:

score: 0
Accepted
time: 140ms
memory: 43520kb

input:

904 225

output:

162909174

result:

ok 1 number(s): "162909174"

Test #36:

score: 0
Accepted
time: 155ms
memory: 43532kb

input:

855 928

output:

39885159

result:

ok 1 number(s): "39885159"

Test #37:

score: 0
Accepted
time: 139ms
memory: 43524kb

input:

503 365

output:

745115888

result:

ok 1 number(s): "745115888"

Test #38:

score: 0
Accepted
time: 137ms
memory: 43444kb

input:

646 996

output:

610925577

result:

ok 1 number(s): "610925577"

Test #39:

score: 0
Accepted
time: 150ms
memory: 43480kb

input:

990 918

output:

203469632

result:

ok 1 number(s): "203469632"

Test #40:

score: 0
Accepted
time: 153ms
memory: 43280kb

input:

961 949

output:

169566857

result:

ok 1 number(s): "169566857"

Test #41:

score: 0
Accepted
time: 147ms
memory: 43452kb

input:

946 932

output:

352423195

result:

ok 1 number(s): "352423195"

Test #42:

score: 0
Accepted
time: 141ms
memory: 43456kb

input:

903 981

output:

196309824

result:

ok 1 number(s): "196309824"

Test #43:

score: 0
Accepted
time: 140ms
memory: 43484kb

input:

916 988

output:

487208972

result:

ok 1 number(s): "487208972"

Test #44:

score: 0
Accepted
time: 138ms
memory: 43252kb

input:

982 982

output:

387421488

result:

ok 1 number(s): "387421488"

Test #45:

score: 0
Accepted
time: 153ms
memory: 43472kb

input:

955 911

output:

955637031

result:

ok 1 number(s): "955637031"

Test #46:

score: 0
Accepted
time: 134ms
memory: 43272kb

input:

906 999

output:

798469943

result:

ok 1 number(s): "798469943"

Test #47:

score: 0
Accepted
time: 135ms
memory: 43480kb

input:

982 975

output:

193506289

result:

ok 1 number(s): "193506289"

Test #48:

score: 0
Accepted
time: 140ms
memory: 43456kb

input:

921 991

output:

431202149

result:

ok 1 number(s): "431202149"