Hdu 5378 Leader in the Tree land (dp+ inverse)

Source: Internet
Author: User

Title Link: Hdu 5378 Leader in Tree land


The problem can be understood as a tree of n nodes, with the probability of K ministers, and finally by n!. The probability that each node is ministers is 1/son (the number of nodes in the subtree that the node is the root node), and the probability of not being Ministers is (son-1)/son. So there is no need to consider the structure of the tree, direct root sentence node number transfer dp[i][j]

DP[I][J] = dp[i-1][j-1] * 1/son[u]

DP[I][J] = dp[i-1][j] * (son[u]-1])/son[u]

The part of the modulo can be used as an inverse element.


#include <cstdio> #include <cstring> #include <vector> #include <algorithm>using namespace std; typedef long LONG ll;const int mod = 1000000007;const int maxn = 1005;int fac[maxn];int N, K, Son[maxn];ll DP[MAXN][MAXN];  vector<int> g[maxn];void dfs (int u, int f) {Son[u] = 1;for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];if (v = = f) Continue;dfs (V, u); son[u] + = Son[v];}}  void Init () {scanf ("%d%d", &n, &k); for (int i = 1; I <= N; i++) g[i].clear (); int u, v;for (int i = 1; i < n; i++) {scanf ("%d%d", &u, &v); G[u].push_back (v); G[v].push_back (u);} DFS (1, 0);} void Exgcd (ll A, ll B, ll& D, ll& x, Ll&y) {if (!b) d = A, x = 1, y = 0;else {exgcd (b, a%b, D, y, x); y-= x * (A/b);}} LL INV (ll a) {ll d, X, Y;EXGCD (A, mod, d, X, y); return d = = 1? (x + MoD)% mod:-1;} int solve () {memset (DP, 0, sizeof (DP));DP [0][0] = 1;for (int i = 1; I <= N; i++) {for (int j = 0; J <= K; j + +) {if (Dp[i-1][j] = = 0) continue;dp[i][j] = (Dp[i][j]+ dp[i-1][j] * (Son[i]-1)% mod * INV (son[i]))% mod;dp[i][j+1] = (dp[i][j+1] + dp[i-1][j] * INV (son[i]))% MoD;} return dp[n][k] * fac[n]% MoD;} int main () {fac[1] = 1;for (int i = 2; I <=; i++) Fac[i] = 1LL * i * fac[i-1]% mod;int cas;scanf ("%d", &cas); for (int kcas = 1; kcas <= cas; kcas++) {init ();p rintf ("Case #%d:%d\n", Kcas, Solve ());} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdu 5378 Leader in the Tree land (dp+ inverse)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.