結果

問題 No.1302 Random Tree Score
ユーザー Kite_kumaKite_kuma
提出日時 2020-11-09 12:22:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 753 ms / 3,000 ms
コード長 4,102 bytes
コンパイル時間 1,703 ms
コンパイル使用メモリ 176,092 KB
実行使用メモリ 9,304 KB
最終ジャッジ日時 2024-07-22 16:00:26
合計ジャッジ時間 8,212 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 141 ms
5,376 KB
testcase_03 AC 330 ms
6,436 KB
testcase_04 AC 132 ms
5,376 KB
testcase_05 AC 753 ms
9,276 KB
testcase_06 AC 596 ms
9,276 KB
testcase_07 AC 118 ms
5,376 KB
testcase_08 AC 343 ms
6,472 KB
testcase_09 AC 560 ms
9,296 KB
testcase_10 AC 630 ms
9,304 KB
testcase_11 AC 114 ms
5,376 KB
testcase_12 AC 669 ms
9,120 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 548 ms
9,292 KB
testcase_15 AC 642 ms
9,296 KB
testcase_16 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*	author:  Kite_kuma
	created: 2020.11.09 12:08:40 */

// #ifdef LOCAL
// #define _GLIBCXX_DEBUG
// #endif
#include <bits/stdc++.h>
using namespace std;

const int mod = 998244353;
const int root = 3;

unsigned int add(const unsigned int x, const unsigned int y) { return (x + y < mod) ? x + y : x + y - mod; }
unsigned int sub(const unsigned int x, const unsigned int y) { return (x >= y) ? (x - y) : (mod - y + x); }
unsigned int mul(const unsigned int x, const unsigned int y) { return (unsigned long long)x * y % mod; }
unsigned int mod_pow(unsigned int x, unsigned int n) {
	unsigned int res = 1;
	while(n > 0) {
		if(n & 1) {
			res = mul(res, x);
		}
		x = mul(x, x);
		n >>= 1;
	}
	return res;
}

unsigned int inverse(const unsigned int x) { return mod_pow(x, mod - 2); }
void ntt(vector<int> &a, const bool rev = false) {
	unsigned int i, j, k, l, p, q, r, s;
	const unsigned int size = a.size();
	if(size == 1) return;
	vector<int> b(size);
	r = rev ? (mod - 1 - (mod - 1) / size) : (mod - 1) / size;
	s = mod_pow(root, r);
	vector<unsigned int> kp(size / 2 + 1, 1);
	for(i = 0; i < size / 2; ++i) kp[i + 1] = mul(kp[i], s);
	for(i = 1, l = size / 2; i < size; i <<= 1, l >>= 1) {
		for(j = 0, r = 0; j < l; ++j, r += i) {
			for(k = 0, s = kp[i * j]; k < i; ++k) {
				p = a[k + r], q = a[k + r + size / 2];
				b[k + 2 * r] = add(p, q);
				b[k + 2 * r + i] = mul(sub(p, q), s);
			}
		}
		swap(a, b);
	}
	if(rev) {
		s = inverse(size);
		for(i = 0; i < size; i++) {
			a[i] = mul(a[i], s);
		}
	}
}

vector<int> convolute(const vector<int> &a, const vector<int> &b) {
	const int size = (int)a.size() + (int)b.size() - 1;
	int t = 1;
	while(t < size) {
		t <<= 1;
	}
	vector<int> A(t, 0), B(t, 0);
	for(int i = 0; i < (int)a.size(); i++) {
		A[i] = a[i];
	}
	for(int i = 0; i < (int)b.size(); i++) {
		B[i] = b[i];
	}
	ntt(A), ntt(B);
	for(int i = 0; i < t; i++) {
		A[i] = mul(A[i], B[i]);
	}
	ntt(A, true);
	A.resize(size);
	return A;
}

#pragma region modint

template <int mod>
struct ModInt {
	int x;
	ModInt() : x(0) {}
	ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
	ModInt &operator+=(const ModInt &p) {
		if((x += p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator-=(const ModInt &p) {
		if((x += mod - p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator*=(const ModInt &p) {
		x = (int)(1LL * x * p.x % mod);
		return *this;
	}
	ModInt &operator/=(const ModInt &p) {
		*this *= p.inverse();
		return *this;
	}
	ModInt operator-() const { return ModInt(-x); }
	ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
	ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
	ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
	ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
	bool operator==(const ModInt &p) const { return x == p.x; }
	bool operator!=(const ModInt &p) const { return x != p.x; }
	ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while(b > 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(u -= t * v, v);
		}
		return ModInt(u);
	}
	ModInt pow(long long n) const {
		ModInt ret(1), mul(x);
		while(n > 0) {
			if(n & 1) ret *= mul;
			mul *= mul;
			n >>= 1;
		}
		return ret;
	}
	friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
	friend istream &operator>>(istream &is, ModInt &a) {
		long long t;
		is >> t;
		a = ModInt<mod>(t);
		return (is);
	}
	static int get_mod() { return mod; }
};
using mint = ModInt<mod>;

#pragma endregion

int main() {
	int n;
	cin >> n;

	vector<mint> fac(n + 2);
	fac[0] = 1;
	for(int i = 0; i < n + 1; i++) {
		fac[i + 1] = fac[i] * (i + 1);
	}

	vector<int> fx(n - 1);

	for(int i = 0; i < n - 1; i++) {
		fx[i] = ((mint)(i + 1) / fac[i]).x;
	}

	int k = n;

	vector<int> res = {1};

	while(k) {
		if(k & 1) {
			res = convolute(res, fx);
			if(res.size() >= n) res.resize(n - 1);
		}
		fx = convolute(fx, fx);
		if(fx.size() >= n) fx.resize(n - 1);
		k >>= 1;
	}

	res.resize(n - 1);

	cout << fac[n - 2] * res[n - 2] / mod_pow(n, n - 2) << endl;
	

	return 0;
}
0