結果

問題 No.1302 Random Tree Score
ユーザー 👑 ygussanyygussany
提出日時 2020-12-01 16:43:22
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1,049 ms / 3,000 ms
コード長 3,275 bytes
コンパイル時間 559 ms
コンパイル使用メモリ 34,688 KB
実行使用メモリ 14,260 KB
最終ジャッジ日時 2024-09-13 03:02:25
合計ジャッジ時間 8,955 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 180 ms
7,296 KB
testcase_03 AC 447 ms
9,492 KB
testcase_04 AC 172 ms
7,172 KB
testcase_05 AC 1,049 ms
14,172 KB
testcase_06 AC 830 ms
14,260 KB
testcase_07 AC 164 ms
7,172 KB
testcase_08 AC 496 ms
9,616 KB
testcase_09 AC 798 ms
14,200 KB
testcase_10 AC 866 ms
14,104 KB
testcase_11 AC 162 ms
6,944 KB
testcase_12 AC 972 ms
14,172 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 797 ms
14,192 KB
testcase_15 AC 936 ms
14,076 KB
testcase_16 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int Mod = 998244353;
int bit[21], bit_inv[21], 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(int 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, int a[], int z[])
{
	if (k == 0) {
		z[0] = a[0];
		return;
	}
	
	int i, d = bit[k-1], tmpp;
	long long tmp;
	static int *b[21] = {}, *c[21] = {}, *x[21] = {}, *y[21] = {};
	if (b[k] == NULL) {
		b[k] = (int*)malloc(sizeof(int) * d);
		c[k] = (int*)malloc(sizeof(int) * d);
		x[k] = (int*)malloc(sizeof(int) * d);
		y[k] = (int*)malloc(sizeof(int) * d);
	}
	for (i = 0; i < d; i++) {
		b[k][i] = a[i*2];
		c[k][i] = a[i*2+1];
	}
	NTT(k - 1, b[k], x[k]);
	NTT(k - 1, c[k], y[k]);
	for (i = 0, tmp = 1; i < d; i++, tmp = tmp * root[k] % Mod) {
		tmpp = tmp * y[k][i] % Mod;
		z[i] = x[k][i] + tmpp;
		if (z[i] >= Mod) z[i] -= Mod;
		z[i+d] = x[k][i] - tmpp;
		if (z[i+d] < 0) z[i+d] += Mod;
	}
}

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

int main()
{
	int N;
	scanf("%d", &N);
	
	int i, j, k, a[262145], x[262145], y[262145], ans[262145] = {};
	long long fact, fact_inv;
	for (i = 1, fact = 1; i < N - 1; i++) fact = fact * i % Mod;
	fact_inv = div_mod(1, fact, Mod);
	for (i = N - 2; i >= 0; fact_inv = fact_inv * (i--) % Mod) a[i] = fact_inv * (i + 1) % Mod;
	for (k = 0, bit[0] = 1; bit[k] < N * 2; 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] = (long long)root[i+1] * root[i+1] % Mod;
		root_inv[i] = (long long)root_inv[i+1] * root_inv[i+1] % Mod;
	}
	
	for (j = N, ans[0] = fact; j > 0; j >>= 1) {
		for (i = N - 1; i < bit[k]; i++) a[i] = 0;
		NTT(k, a, x);
		if ((j & 1) == 1) {
			NTT(k, ans, y);
			for (i = 0; i < bit[k]; i++) y[i] = (long long)y[i] * x[i] % Mod;
			NTT_reverse(k, y, ans);
			for (i = 0; i < N - 1; i++) ans[i] = (long long)ans[i] * bit_inv[k] % Mod;
			for (; i < bit[k]; i++) ans[i] = 0;
		}
		if (j > 1) {
			for (i = 0; i < bit[k]; i++) x[i] = (long long)x[i] * x[i] % Mod;
			NTT_reverse(k, x, a);
			for (i = 0; i < N - 1; i++) a[i] = (long long)a[i] * bit_inv[k] % Mod;
		}
	}
	printf("%lld\n", div_mod(ans[N-2], pow_mod(N, N - 2), Mod));
	fflush(stdout);
	return 0;
}
0