結果
問題 | No.1614 Majority Painting on Tree |
ユーザー | ygussany |
提出日時 | 2021-05-22 17:07:11 |
言語 | C (gcc 12.3.0) |
結果 |
WA
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 2,251 bytes |
コンパイル時間 | 269 ms |
コンパイル使用メモリ | 33,596 KB |
実行使用メモリ | 8,932 KB |
最終ジャッジ日時 | 2024-07-17 15:08:21 |
合計ジャッジ時間 | 4,062 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
testcase_40 | WA | - |
testcase_41 | WA | - |
testcase_42 | WA | - |
testcase_43 | WA | - |
testcase_44 | WA | - |
testcase_45 | AC | 2 ms
6,944 KB |
testcase_46 | WA | - |
testcase_47 | AC | 2 ms
6,940 KB |
testcase_48 | WA | - |
ソースコード
#include <stdio.h> const int Mod = 998244353; long long fact[100001], fact_inv[100001]; 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; } long long combination(int n, int k) { return fact[n] * fact_inv[k] % Mod * fact_inv[n-k] % Mod; } typedef struct List { struct List *next; int v; } list; long long solve(int N, int C, list* adj[]) { if (C == 1) return 1; int i; 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; int r = 1, u, w, par[100001] = {}, q[100001], head, tail; list *p; par[r] = r; q[0] = r; for (head = 0, tail = 1; head < tail; head++) { u = q[head]; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (par[w] == 0) { par[w] = u; q[tail++] = w; } } } int k; long long dp[100001], prod; for (head--; head > 0; head--) { u = q[head]; for (p = adj[u], k = 0, prod = 1; p != NULL; p = p->next) { w = p->v; if (w == par[u]) continue; prod = prod * dp[w] % Mod; k++; } dp[u] = pow_mod(C, k); for (i = (k + 1) / 2; i < k; i++) dp[u] += Mod - combination(k, i) * pow_mod(C - 1, k - i) % Mod; for (i = (k + 3) / 2; i <= k; i++) dp[u] += Mod - combination(k, i) * pow_mod(C - 1, k - i + 1) % Mod; dp[u] = dp[u] % Mod * prod % Mod; } for (p = adj[r], k = 0, prod = 1; p != NULL; p = p->next) { w = p->v; prod = prod * dp[w] % Mod; k++; } dp[r] = pow_mod(C, k - 1); for (i = k / 2 + 1; i < k; i++) dp[r] += Mod - combination(k, i) * pow_mod(C - 1, k - i) % Mod; return dp[r] % Mod * prod % Mod * C % Mod; } int main() { int i, N, C, u, w; list *adj[100001] = {}, e[200001]; scanf("%d %d", &N, &C); for (i = 0; i < N - 1; i++) { scanf("%d %d", &u, &w); e[i*2].v = w; e[i*2+1].v = u; e[i*2].next = adj[u]; e[i*2+1].next = adj[w]; adj[u] = &(e[i*2]); adj[w] = &(e[i*2+1]); } printf("%lld\n", solve(N, C, adj)); fflush(stdout); return 0; }