zoukankan      html  css  js  c++  java
  • Day9

    Yue Fei is one of the most famous military general in Chinese history.He led Southern Song army in the wars against the Jin dynasty of northern China. Yue Fei achieved a lot of victory and hopefully could retake Kaifeng ,the former capital of Song occupied by Jin. Fearing that retaking Kaifeng might cause the Jin to release former Emperor Song Qinzong, threatening his throne, Emperor Song Gaozong took some corrupted officers' advice, sending 12 urgent orders in the form of 12 gold plaques to Yue Fei, recalling him back to the capital.

    Then Yue Fei was put into prison and was killed under a charge of "maybe there is" treason. But later Yue Fei was posthumously pardoned and rehabilitated, and became a symbol of loyalty to the country. The four corrupted officers who set him up were Qin Hui,Qin Hui's wife Lady Wang, Moqi Xie and Zhang Jun. People made kneeling iron statues of them and put the statues before Yue Fei's tomb (located by the West Lake, Hangzhou). For centuries, these statues have been cursed, spat and urinated upon by people. (Now please don't do that if you go to Hangzhou and see the statues.)

    One of the most important battle Yue Fei won is the battle in Zhuxian town. In Zhuxian town, Yue Fei wanted to deploy some barracks, and connected those barracks with roads. Yue Fei needed all the barracks to be connected, and in order to save money, he wanted to build as less roads as possible. There couldn't be a barrack which is too important, or else it would be attacked by enemies. So Yue Fei required that NO barrack could connect with more than 3 roads. According to his battle theory, Yue Fei also required that the length of the longest route among the barracks is exactly K. Note that the length of a route is defined as the number of barracks lied on it and there may be several longest routes with the same length K.

    Yue Fei wanted to know, in how many different ways could he deploy the barracks and roads. All barracks could be considered as no different. Yue Fei could deploy as many barracks as he wanted.

    For example, if K is 3,Yue Fei had 2 ways to deploy the barracks and roads as shown in figure1. If K is 4, the 3 kinds of layouts is shown in figure 2. (Thick dots stand for barracks, and segments stand for roads):


    Please bring your computer and go back to Yue Fei's time to help him so that you may change the history.

    InputThe input consists of no more than 25 test cases.

    For each test, there is only one line containing a integer K(1<=K<=100,000) denoting the length of the longest route.

    The input ends by K = 0.OutputFor each test case, print an integer denoting the number of different ways modulo 1000000007.Sample Input

    3
    4
    0

    Sample Output

    2
    3

    思路:dp计数问题,要求一棵直径为k的树有多少种,每个节点最多有3个分支,可以将其分为一个深度为k/2的二叉树,设dp[i]为深度为i的二叉树不同构意义下的数目,则有2种情况,深度为i的子树深度都是i-1,若两者形态相等,就是dp[i-1],不相等,就是C(dp[i-1],2),若只有一棵是i-1,则数目为dp[i-1]*sum[i-2],sum为dp的前缀和,看统计答案时,若k为偶数,则k对半分,在中间设一个虚拟节点,两边深度都是k/2,若两者相同,则是dp[k/2],若不同,则是C(dp[k/2], 2),当k为奇数的时候,就有2种情况,选一个节点当父节点,其有2个或3个子树,且至少有2个子树的深度为k/2,当2个子树深度为k/2时,与偶数情况相同,多了一个小于k/2的子树,就是(C(dp[k/2], 2)+dp[k/2])*sum[k/2-1], 当3个子树深度都是k/2时,又有三种情况,三者形态相等,dp[k/2],两个相等,C(2,1)*C(dp[k/2],2),都不相等,C(dp[k/2],3)
    typedef long long LL;
    typedef pair<LL, LL> PLL;
    
    const LL MOD = 1e9+7;
    const LL inv2 = 500000004;
    const LL inv3 = 333333336;
    const LL inv6 = 166666668;
    const int maxm = 1e5+5;
    
    LL sum[maxm], dp[maxm];
    
    void init() {
        dp[0] = dp[1] = sum[0] = 1;
        dp[2] = sum[1] = 2, sum[2] = 4;
        for(int i = 3; i < maxm; ++i) {
            dp[i] = (dp[i-1]+1)*dp[i-1]%MOD*inv2%MOD;
            dp[i] = (dp[i] + dp[i-1]*sum[i-2]%MOD)%MOD;
            sum[i] = (sum[i-1] + dp[i]) % MOD;
        }
    }
    
    
    void run_case(int k) {
        LL ans = 0;
        if(k & 1) {
            k /= 2;
            ans = dp[k]*(dp[k]+1)%MOD*inv2%MOD*sum[k-1]%MOD;
            ans = (ans + dp[k]*(dp[k]-1+MOD)%MOD*(dp[k]-2+MOD)%MOD*inv6%MOD)%MOD;
            ans = (ans + dp[k]*(dp[k]-1+MOD)%MOD+dp[k])%MOD;
        } else {
            k /= 2;
            ans = dp[k]*(dp[k]+1)%MOD*inv2%MOD;
        }
        cout << ans << endl;
    }
    
    int main() {
        ios::sync_with_stdio(false), cin.tie(0);
        init();
        int k;
        while(cin >> k && k) 
            run_case(k);
        return 0;
    }
    View Code
    
    
  • 相关阅读:
    构造方法中使用this的含义
    Android Bundle类
    Android中使用PULL方式解析XML文件
    Android 创建与解析XML(四)—— Pull方式
    File的getPath()和getAbsolutePath()和getCanonicalPath()的区别
    Android-取出SDcard卡下指定后缀名的文件
    page、request、session和application有什么区别?
    prepareStatement的用法和解释
    pageContext对象的用法
    使用JSP连接MySql数据库读取HTML表单数据进行存贮
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12248362.html
Copyright © 2011-2022 走看看