結果

問題 No.1302 Random Tree Score
ユーザー 👑 ygussanyygussany
提出日時 2020-12-01 11:30:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,460 bytes
コンパイル時間 511 ms
コンパイル使用メモリ 36,864 KB
実行使用メモリ 25,812 KB
最終ジャッジ日時 2024-09-13 02:55:01
合計ジャッジ時間 30,158 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,428 KB
testcase_01 AC 3 ms
9,424 KB
testcase_02 AC 668 ms
14,632 KB
testcase_03 AC 1,647 ms
16,884 KB
testcase_04 AC 639 ms
13,100 KB
testcase_05 TLE -
testcase_06 TLE -
testcase_07 AC 609 ms
12,788 KB
testcase_08 AC 1,825 ms
17,132 KB
testcase_09 AC 2,941 ms
25,460 KB
testcase_10 TLE -
testcase_11 AC 607 ms
12,728 KB
testcase_12 TLE -
testcase_13 AC 3 ms
9,172 KB
testcase_14 AC 2,915 ms
25,588 KB
testcase_15 TLE -
testcase_16 AC 3 ms
9,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>

const int Mod = 998244353;
int bit[21], bit_inv[21];
long long root[21], root_inv[21];

long long div_mod(long long x, long long y, long long z)
{
	if (x % y == 0) return x / y;
	else return (div_mod((1 + x / y) * y - x, (z % y), y) * z + x) / y;
}

long long pow_mod(long long n, int k)
{
	long long N, ans = 1;
	for (N = n; k > 0; k >>= 1, N = N * N % Mod) if (k & 1) ans = ans * N % Mod;
	return ans;
}

void NTT(int k, long long a[], long long z[])
{
	if (k == 0) {
		z[0] = a[0];
		return;
	}
	
	int i, d = bit[k-1];
	long long tmp, *b = (long long*)malloc(sizeof(long long) * d), *c = (long long*)malloc(sizeof(long long) * d), *x = (long long*)malloc(sizeof(long long) * d), *y = (long long*)malloc(sizeof(long long) * d);
	for (i = 0; i < d; i++) {
		b[i] = a[i*2];
		c[i] = a[i*2+1];
	}
	NTT(k - 1, b, x);
	NTT(k - 1, c, y);
	for (i = 0, tmp = 1; i < d; i++, tmp = tmp * root[k] % Mod) {
		z[i] = (x[i] + y[i] * tmp) % Mod;
		z[i+d] = (x[i] - y[i] * tmp % Mod + Mod) % Mod;
	}
	
	free(b);
	free(c);
	free(x);
	free(y);
}

void NTT_reverse(int k, long long z[], long long a[])
{
	if (k == 0) {
		a[0] = z[0];
		return;
	}
	
	int i, d = bit[k-1];
	long long tmp, *b = (long long*)malloc(sizeof(long long) * d), *c = (long long*)malloc(sizeof(long long) * d), *x = (long long*)malloc(sizeof(long long) * d), *y = (long long*)malloc(sizeof(long long) * d);
	for (i = 0; i < d; i++) {
		x[i] = z[i*2];
		y[i] = z[i*2+1];
	}
	NTT_reverse(k - 1, x, b);
	NTT_reverse(k - 1, y, c);
	for (i = 0, tmp = 1; i < d; i++, tmp = tmp * root_inv[k] % Mod) {
		a[i] = (b[i] + c[i] * tmp) % Mod;
		a[i+d] = (b[i] - c[i] * tmp % Mod + Mod) % Mod;
	}
	
	free(b);
	free(c);
	free(x);
	free(y);
}

void prod_poly_NTT(int da, int db, long long a[], long long b[], long long c[])
{
	int i, k;
	for (k = 0, bit[0] = 1; bit[k] < da + db; k++) bit[k+1] = bit[k] * 2;
	for (i = k - 1, bit_inv[k] = div_mod(1, bit[k], Mod); i >= 0; i--) bit_inv[i] = bit_inv[i+1] * 2 % Mod;
	for (i = k - 1, root[k] = pow_mod(3, (Mod - 1) / bit[k]), root_inv[k] = pow_mod(3, Mod - 1 - (Mod - 1) / bit[k]); i >= 0; i--) {
		root[i] = root[i+1] * root[i+1] % Mod;
		root_inv[i] = root_inv[i+1] * root_inv[i+1] % Mod;
	}
	for (i = da; i < bit[k]; i++) a[i] = 0;
	for (i = db; i < bit[k]; i++) b[i] = 0;
	
	long long *x = (long long*)malloc(sizeof(long long) * bit[k]), *y = (long long*)malloc(sizeof(long long) * bit[k]), *z = (long long*)malloc(sizeof(long long) * bit[k]);
	NTT(k, a, x);
	NTT(k, b, y);
	for (i = 0; i < bit[k]; i++) z[i] = x[i] * y[i] % Mod;
	NTT_reverse(k, z, c);
	for (i = 0; i < da + db; i++) c[i] = c[i] * bit_inv[k] % Mod;	
	free(x);
	free(y);
	free(z);
}

int main()
{
	int N;
	scanf("%d", &N);
	
	int i, j, k;
	long long a[262145], b[262145], c[262145], ans[100001], fact[100001], fact_inv[100001];
	for (i = 1, fact[0] = 1; i <= N; i++) fact[i] = fact[i-1] * i % Mod;
	for (i = N - 1, fact_inv[N] = div_mod(1, fact[N], Mod); i >= 0; i--) fact_inv[i] = fact_inv[i+1] * (i + 1) % Mod;
	for (i = 0; i < N - 1; i++) a[i] = fact_inv[i] * (i + 1) % Mod;
	for (k = N, ans[0] = fact[N-2]; k > 0; k >>= 1) {
		if ((k & 1) == 1) {
			for (i = 0; i < N - 1; i++) b[i] = ans[i];
			prod_poly_NTT(N - 1, N - 1, a, b, c);
			for (i = 0; i < N - 1; i++) ans[i] = c[i];
		}
		prod_poly_NTT(N - 1, N - 1, a, a, b);
		for (i = 0; i < N - 1; i++) a[i] = b[i];
	}
	printf("%lld\n", div_mod(ans[N-2], pow_mod(N, N - 2), Mod));
	fflush(stdout);
	return 0;
}
0