結果
問題 | No.2108 Red or Blue and Purple Tree |
ユーザー | 👑 ygussany |
提出日時 | 2022-10-10 17:09:27 |
言語 | C (gcc 12.3.0) |
結果 |
AC
|
実行時間 | 2,708 ms / 4,000 ms |
コード長 | 6,591 bytes |
コンパイル時間 | 606 ms |
コンパイル使用メモリ | 36,028 KB |
実行使用メモリ | 47,220 KB |
最終ジャッジ日時 | 2024-06-24 11:22:00 |
合計ジャッジ時間 | 27,318 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2,511 ms
38,528 KB |
testcase_01 | AC | 2,608 ms
47,188 KB |
testcase_02 | AC | 2,708 ms
38,528 KB |
testcase_03 | AC | 2,590 ms
47,220 KB |
testcase_04 | AC | 2,631 ms
38,528 KB |
testcase_05 | AC | 2,575 ms
47,136 KB |
testcase_06 | AC | 2,662 ms
39,244 KB |
testcase_07 | AC | 2,581 ms
47,096 KB |
ソースコード
#include <stdio.h> 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]; } void solve_all2(int N) { int i, j, k, a[2001], b[2001], c[4001], g[2001]; static long long t[2001][2001] = {}, tmp; for (i = 2, t[1][1] = 1; i <= N; i++) { for (j = 0; j < i; j++) a[j] = combination(i - 1, j) * selfpow[i-j] % Mod; for (j = 1; j < i; j++) for (k = 1; k <= j; k++) t[i][k+1] += t[j][k] * a[j] % Mod; for (j = 2, t[i][1] = a[0]; j <= i; j++) t[i][j] %= Mod; } for (i = N; i >= 2; i--) { for (k = 1, tmp = fact_inv[i] * fact[i-1] % Mod; k <= i; k++, tmp = tmp * i % Mod) g[i-k] = t[i][k] * tmp % Mod * tmp % Mod; for (j = 0; j < i; j++) a[j] = g[i-1-j] * fact[i-1-j] % Mod; for (j = 0; j < i; j++) b[j] = (j % 2 == 0)? fact_inv[j]: Mod - fact_inv[j]; prod_polynomial(i - 1, i - 1, a, b, c); for (j = 0; j < i; j++) memo[i][i-1-j] = c[j] * fact_inv[i-1-j] % Mod; } } int solve2(int N, int K) { if (memo[N][N-1] == 0) solve_all2(2000); return memo[N][K]; } int main() { 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--) { scanf("%d %d", &N, &K); printf("%d\n", solve2(N, K)); } fflush(stdout); return 0; }