結果

問題 No.2108 Red or Blue and Purple Tree
ユーザー ygussanyygussany
提出日時 2022-10-10 15:09:38
言語 C
(gcc 12.3.0)
結果
RE  
実行時間 -
コード長 5,756 bytes
コンパイル時間 1,268 ms
コンパイル使用メモリ 34,688 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-24 10:02:45
合計ジャッジ時間 30,741 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 245 ms
5,248 KB
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 TLE -
testcase_07 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <sys/time.h>

struct timeval tt;
double beg, cur;

const int Mod = 998244353,
	bit[21] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576},
	bit_inv[21] = {1, 499122177, 748683265, 873463809, 935854081, 967049217, 982646785, 990445569, 994344961, 996294657, 997269505, 997756929, 998000641, 998122497, 998183425, 998213889, 998229121, 998236737, 998240545, 998242449, 998243401},
	root[21] = {1, 998244352, 911660635, 372528824, 929031873, 452798380, 922799308, 781712469, 476477967, 166035806, 258648936, 584193783, 63912897, 350007156, 666702199, 968855178, 629671588, 24514907, 996173970, 363395222, 565042129},
	root_inv[21] = {1, 998244352, 86583718, 509520358, 337190230, 87557064, 609441965, 135236158, 304459705, 685443576, 381598368, 335559352, 129292727, 358024708, 814576206, 708402881, 283043518, 3707709, 121392023, 704923114, 950391366};

void NTT_inline(int kk, int a[], int x[])
{
	int h, hh, i, ii, j, jj, k, l, r = bit[kk], d = bit[kk-1], tmpp, cur, prev;
	int *pi, *pii, *pj, *pjj;
	static int y[2][4096];
	long long tmp;
	for (i = 0; i < r; i++) y[0][i] = a[i];
	for (k = 1, kk--, cur = 1, prev = 0; kk >= 0; k++, kk--, cur ^= 1, prev ^= 1) {
		for (h = 0, tmp = 1; h << (kk + 1) < r; h++, tmp = tmp * root[k] % Mod) {
			for (hh = 0, pi = &(y[cur][h<<kk]), pii = pi + d, pj = &(y[prev][h<<(kk+1)]), pjj = pj + bit[kk]; hh < bit[kk]; hh++, pi++, pii++, pj++, pjj++) {
				tmpp = tmp * (*pjj) % Mod;
				*pi = *pj + tmpp;
				if (*pi >= Mod) *pi -= Mod;
				*pii = *pj - tmpp;
				if (*pii < 0) *pii += Mod;
			}
		}
	}
	for (i = 0; i < r; i++) x[i] = y[prev][i];
}

void NTT_reverse_inline(int kk, int a[], int x[])
{
	int h, hh, i, ii, j, jj, k, l, r = bit[kk], d = bit[kk-1], tmpp, cur, prev;
	int *pi, *pii, *pj, *pjj;
	static int y[2][4096];
	long long tmp;
	for (i = 0; i < r; i++) y[0][i] = a[i];
	for (k = 1, kk--, cur = 1, prev = 0; kk >= 0; k++, kk--, cur ^= 1, prev ^= 1) {
		for (h = 0, tmp = 1; h << (kk + 1) < r; h++, tmp = tmp * root_inv[k] % Mod) {
			for (hh = 0, pi = &(y[cur][h<<kk]), pii = pi + d, pj = &(y[prev][h<<(kk+1)]), pjj = pj + bit[kk]; hh < bit[kk]; hh++, pi++, pii++, pj++, pjj++) {
				tmpp = tmp * (*pjj) % Mod;
				*pi = *pj + tmpp;
				if (*pi >= Mod) *pi -= Mod;
				*pii = *pj - tmpp;
				if (*pii < 0) *pii += Mod;
			}
		}
	}
	for (i = 0; i < r; i++) x[i] = y[prev][i];
}

// Compute the product of two polynomials a[0-da] and b[0-db] using NTT in O(d * log d) time
void prod_poly_NTT(int da, int db, int a[], int b[], int c[])
{
	int i, k;
	static int kk = -1, aa[4096], bb[4096], cc[4096];
	for (k = 0; bit[k] <= da + db; k++);
	for (i = 0; i <= da; i++) aa[i] = a[i];
	for (i = da + 1; i < bit[k]; i++) aa[i] = 0;
	for (i = 0; i <= db; i++) bb[i] = b[i];
	for (i = db + 1; i < bit[k]; i++) bb[i] = 0;
	
	static int x[4096], y[4096], z[4096];
	NTT_inline(k, aa, x);
	if (kk != k) {
	    NTT_inline(k, bb, y);
	    kk = k;
	}
	for (i = 0; i < bit[k]; i++) z[i] = (long long)x[i] * y[i] % Mod;
	NTT_reverse_inline(k, z, cc);
	for (i = 0; i <= da + db; i++) c[i] = (long long)cc[i] * bit_inv[k] % Mod;
}

// Compute the product of two polynomials a[0-da] and b[0-db] naively in O(da * db) time
void prod_poly_naive(int da, int db, int a[], int b[], int c[])
{
	int i, j;
	static long long tmp[4096];
	for (i = 0; i <= da + db; i++) tmp[i] = 0;
	for (i = 0; i <= da; i++) for (j = 0; j <= db; j++) tmp[i+j] += (long long)a[i] * b[j] % Mod;
	for (i = 0; i <= da + db; i++) c[i] = tmp[i] % Mod;
}

// Compute the product of two polynomials a[0-da] and b[0-db] in an appropriate way
void prod_polynomial(int da, int db, int a[], int b[], int c[])
{
	if (da <= 70 || db <= 70) prod_poly_naive(da, db, a, b, c);
	else prod_poly_NTT(da, db, a, b, c);
}

long long fact[2001], fact_inv[2001], selfpow[2001];

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, long long 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;
}

long long combination(int n, int k)
{
	if (k < 0 || n < k) return 0;
	return fact[n] * fact_inv[k] % Mod * fact_inv[n-k] % Mod;
}

int memo[2001][2001] = {};

void solve_all(int N, int ans[])
{
	int i, j, k;
	static int a[2001], b[2001], c[4001], g[2001];
	long long tmp;
	for (i = 1, a[0] = 1; i <= N; i++) a[i] = 0;
	for (i = 1, b[0] = 0; i <= N; i++) b[i] = fact_inv[i-1] * selfpow[i] % Mod;
	for (k = 0, tmp = fact_inv[N] * fact[N-1] % Mod; k < N; k++, tmp = tmp * N % Mod) {
		for (i = 0; i < N - k; i++) a[i] = a[i] * fact[N-k-i-1] % Mod;
		prod_polynomial(N - k - 1, N - k, a, b, c);
		for (i = 0; i < N - k; i++) a[i] = c[i+1] * fact_inv[N-k-i-1] % Mod;
		g[N-k-1] = a[N-k-1] * tmp % Mod * tmp % Mod;
	}
	for (i = N - 2, ans[N-1] = g[N-1]; i >= 0; i--) {
		for (j = i + 1, ans[i] = g[i]; j < N; j++) {
			ans[i] -= ans[j] * combination(j, i) % Mod;
			if (ans[i] < 0) ans[i] += Mod;
		}
	}
}

int solve(int N, int K)
{
	if (memo[N][N-1] == 0) solve_all(N, memo[N]);
	return memo[N][K];
}

int main()
{
	gettimeofday(&tt, NULL);
	beg = tt.tv_sec + (double)tt.tv_usec / 1000000;
	
	int i, T, N = 2000, K;
	scanf("%d", &T);
	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; i++) selfpow[i] = pow_mod(i, i);
	while (T--) {
		gettimeofday(&tt, NULL);
		cur = tt.tv_sec + (double)tt.tv_usec / 1000000;
		if (cur - beg > 3.9) return -1;
		scanf("%d %d", &N, &K);
		printf("%d\n", solve(N, K));
	}
	fflush(stdout);
	return 0;
}
0