zoukankan      html  css  js  c++  java
  • 动态规划(计数DP):HDU 5136 Yue Fei's Battle

    Yue Fei's Battle

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
    Total Submission(s): 760    Accepted Submission(s): 266


    Problem Description
    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.
     
    Input
    The 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.
     
    Output
    For each test case, print an integer denoting the number of different ways modulo 1000000007.
     
    Sample Input
    3
    4
    0
     
    Sample Output
    2
    3
     
      这道题就是一个简单的DP。

      引用了别人的题解。

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 const int N=100010,Mod=1000000007;
     6 long long dp[N],sum[N],ans;int T,n;
     7 long long Inv(long long x){
     8     return x==1?1:(Mod-Mod/x)*Inv(Mod%x)%Mod;
     9 }
    10 long long i2,i6;
    11 int main(){
    12     sum[0]=1;dp[0]=1;
    13     sum[1]=2;dp[1]=1;
    14     i2=Inv(2);i6=Inv(6);
    15     for(int i=2;i<N;i++){
    16         dp[i]=dp[i-1]*(dp[i-1]+1)%Mod*i2%Mod;
    17         (dp[i]+=dp[i-1]*sum[i-2]%Mod)%=Mod;
    18         sum[i]=(sum[i-1]+dp[i])%Mod;
    19     }
    20     
    21     while(scanf("%d",&n)!=EOF&&n){
    22         if(n==1){printf("1
    ");continue;}
    23         else if(n%2==1){
    24             ans=sum[n/2-1]*(dp[n/2]*(dp[n/2]+1)%Mod*i2%Mod)%Mod;
    25             ans+=dp[n/2]*(dp[n/2]-1+Mod)%Mod*(dp[n/2]-2+Mod)%Mod*i6%Mod;
    26             ans=((ans+dp[n/2]*(dp[n/2]-1+Mod)%Mod)%Mod+dp[n/2])%Mod;
    27         }
    28         else ans=dp[n/2]*(dp[n/2]+1)%Mod*i2%Mod;
    29         printf("%lld
    ",ans);
    30     }
    31     return 0;
    32 }
  • 相关阅读:
    node js 修改js代码自动发布到服务器
    NodeJS创建 HTTP 服务器
    node.js安装环境的检查和配置
    JQuery中extend使用
    DataTable 转换为List
    sql 修改表结构 schema
    iOS自动处理键盘事件的第三方库:IQKeyboardManager
    IOS8下的远程推送(转载)
    OC中运行出错( Unknown class <XXX> in InterfaceBuilder file.) 解决办法
    UISegmentedControl
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5994366.html
Copyright © 2011-2022 走看看