HDU - 5136(计数dp)

首先题目意思是,给定一个数N,让求树的直径上的点为N,且每个节点最多有三条连边的所有同构树的数目。

首先,说一下同构树: 设 x,y为两个不同的子树 , 那么有    x - y , y - x ( - :代表相连)

那么,可以对本题目,可以分奇数偶数两类分别统计。

偶数,记直径上点为n的所有“ 一半”的子树数目为 tem[n/2], 那么所有的为d[n] =  C(tem[n/2] ,2)+tem[n/2];  tem是一个递推计算的量,记sum[i]数组代表k (1 -> i)所有tem[k]的和、

那么tem[ i+1 ] = (C(tem[i],2)+tem[i]) + tem[i]*sum[i-1];

奇数,考虑中间的点岔开的第三条边所有放置情况,当第三条边最长链上点数<= n/2-1,那么此时的个数为sum[n/2-1]*(C(tem[n/2] ,2)+tem[n/2]) , 当为n/2时,可能会与其他两支同构,个数总数为 (C(tem[n/2],3)+tem[n/2]*(tem[n/2]-1)+tem; (要特别注意这里的第二个加试,tem[n/2]*(tem[n/2]-1))

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <list>
#include <cstdlib>
#include <queue>
#include <stack>
#include<cmath>
#define ALL(a) a.begin(), a.end()
#define clr(a, x) memset(a, x, sizeof a)
#define fst first
#define snd second
#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define rep1(i,x,y) for(int i=x;i<=(int)y;i++)
using namespace std;
typedef long long LL;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1e9+7;
const int N = 101010;
LL d[N];

LL quick_mod(LL a, LL b)
{
    LL ans = 1;
    a %= mod;
    while(b)
    {
        if(b & 1){
            ans = ans * a % mod;
            b--;
        }
        b >>= 1;
        a = a * a % mod;
    }
    return ans;
}
LL C(LL n, LL m)
{
    if(m > n) return 0;
    LL ans = 1;
    for(int i=1; i<=m; i++)
    {
        LL a = (n + i - m) % mod;
        LL b = i % mod;
        ans = ans * (a * quick_mod(b, mod-2) % mod) % mod;
    }
    return ans;
}

LL Lucas(LL n, LL m)
{
    if(m > n) return 0;
    if(m == 0) return 1;
    return C(n % mod, m % mod) * Lucas(n / mod, m / mod) % mod;
}
LL sum[N];
int main()
{
    d[1] = 1; d[2] = 1;
    sum[0]=0; sum[1] = 1; sum[2]=2;
    LL  tem = 1;
    for(int i=3;i<N;i++){
         if(i % 2 == 0){
              LL te = tem*(sum[i/2-2]+1);
              tem = (Lucas(tem,2)+tem+te)%mod;
              d[i]=(Lucas(tem,2)+tem)%mod;
              sum[i/2]=(sum[i/2-1]+tem)%mod;
         }
         else {
              ll te=(Lucas(tem,2)+tem)%mod;
              d[i]=(Lucas(tem,3)+(tem*tem)%mod+(te*(sum[i/2-1]+1)))%mod;
         }
    }
    int n;
    while(scanf("%d",&n)==1 && n){
          cout<<d[n]<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值