HDU 4359 Easy Tree DP? 帶權二叉樹的構造方法 dp

來源:互聯網
上載者:User

標籤:io   os   ar   for   sp   on   amp   ef   line   

題意:

給定n deep

1、構造一個n個節點的帶權樹,且最大深度為deep,每個節點最多隻能有2個兒子

2、每個節點的值為2^0, 2^1 ··· 2^(n-1)  任意兩個節點值不能相同

3、對於一個節點,若他有左右兒子,則左子樹的和 < 右子樹的和

問:

有多少種構造方法。

思路:

dp


#include <stdio.h>#include <iostream>#include <algorithm>#include <cstring>using namespace std;template <class T>inline bool rd(T &ret) {    char c; int sgn;    if(c=getchar(),c==EOF) return 0;    while(c!='-'&&(c<'0'||c>'9')) c=getchar();    sgn=(c=='-')?-1:1;    ret=(c=='-')?0:(c-'0');    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');    ret*=sgn;    return 1;}template <class T>inline void pt(T x) {    if (x <0) {        putchar('-');        x = -x;    }    if(x>9) pt(x/10);    putchar(x%10+'0');}///////////////////////////////////typedef long long ll;const int N = 362;const ll mod = 1000000000 + 7;int d[N][N], p[N][N], c[N][N];int main() {memset(d, 0, sizeof d);memset(p, 0, sizeof p);memset(c, 0, sizeof c);for (int i = 0; i < N; ++i) {c[i][0] = c[i][i] = 1;for (int j = 1; j < i; ++j)c[i][j] = (c[i - 1][j] + c[i-1][j-1]) % mod;}for (int i = 0; i < N; ++i)p[0][i] = 1;for (int i = 1; i < N; ++i)p[1][i] = 1;for (int i = 2; i < N; ++i)for (int j = 1; j < N; ++j) {p[i][j] += (ll)p[i-1][j-1] * 2 % mod;p[i][j] %= mod;if (i - 1 >= 2) {for (int k = 1; k <= i - 2; ++k) {p[i][j] += ((ll)c[i-2][k] * p[k][j-1] % mod) * p[i-1-k][j-1] % mod;p[i][j] %= mod;}}p[i][j] = (ll)p[i][j] * i % mod;}//int x, y; while (~scanf("%d%d", &x, &y)) printf("%d\n", p[x][y]);d[1][1] = 1; d[0][0] = 1;for (int i = 2; i < N; ++i)for (int j = i; j < N; ++j) {d[i][j] += (ll)2 * d[i-1][j-1] % mod;d[i][j] %= mod;if (j - 1 >= 2) {for (int k = 1; k <= j - 2; ++k) {d[i][j] += ((ll)c[j-2][k] * p[k][i-2] % mod) * d[i-1][j-1-k] % mod;d[i][j] %= mod;d[i][j] += ((ll)c[j-2][k] * d[i-1][k] % mod) * p[j-1-k][i-2] % mod;d[i][j] %= mod;d[i][j] += ((ll)c[j-2][k] * d[i-1][k] % mod) * d[i-1][j-1-k] % mod;d[i][j] %= mod;}}d[i][j] = (ll)d[i][j] * j % mod;}int T = 0, cas, u, v;scanf("%d", &cas);while (cas -- > 0) {rd(u); rd(v);printf("Case #");pt(++T);putchar(':');putchar(' ');pt(d[v][u]);putchar('\n');}return 0;}


HDU 4359 Easy Tree DP? 帶權二叉樹的構造方法 dp

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.