转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

现场赛的时候由于有个地方有点小问题,没有成功AC,导致与金牌失之交臂。


由于今天下午有点事情,无法打重现,所以下午只是花了十分钟做了一道J题,抢了个FB,2333333333

Yue Fei's Battle

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

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
 

题意:规定每个结点最多连3个点,有若干个结点构成一棵树,则在树的直径为k时,共有几种非同构的结构。

分析:由于需要考虑非同构的情况比较复杂,那么,我们可以考虑将这棵树拆开,先看偶数,偶数比较简单

1.若直径n为偶数,则选取这棵树的直径的中间那条边,将其断开,从而产生长度均为n/2的两个分支,为了保证计数时不会将同构的重复计入,根据组合数学,则方案数为C(num[n/2]+2-1,2);

注:num[i]表示分支长度为i的非同构的种数。

  dp[i]表示分支长度从0到i的所有非同构的分支方案数。

2.若直径n为奇数,则选取这棵树的直径上的中点,从而拆分成长度为(n-1)/2,长度为(n-1)/2以及另一条长度可为0到(n-1)/2的三个分支。

  a.若剩下一条分支的长度为[0,(n-1)/2-1],根据组合数学,可以得到对应的方案数为dp[(n-1)/2-1]*C(num[(n-1)/2]+2-1,2);

  b.若剩下另一条分支的长度也为(n-1)/2,则此情况下对应的方案数为C(num[(n-1)/2]+3-1,3);

  即奇数情况为dp[(n-1)/2-1]*C(num[(n-1)/2]+2-1,2)+C(num[(n-1)/2]+3-1,3);

接下来考虑num[i]与dp[i]是如何得出的问题:

显然dp[i]只是一个前缀和,求出num[i]之后累加即可。

对于num[i],对于长度为i的分支,其端点上只能连接两个子分支(因为另一个要留着与其它分支相连),那么,对于长度为i+1的分支,则只是在长度为i的分支的端点处,增加了一个结点,然后再在新的端点上添加长度为0到i的分支。

  a.若添加的分支的长度为0到i-1,则方案数为num[i]*dp[i-1];

  b.若添加的分支的长度为i,则方案数为C(num[i]+2-1,2);

  即num[i+1]=num[i]*dp[i-1]+C(num[i]+2-1,2);

 #include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
const int maxn=;
ll dp[maxn];
ll num[maxn];
const int mod=;
ll inv(int x)
{
int n=mod-;
ll temp=x;
ll ret=;
while(n)
{
if(n&)ret=ret*temp%mod;
temp=temp*temp%mod;
n>>=;
}
return ret;
}
int main()
{
ios::sync_with_stdio(false);
ll inv_2,inv_3;
inv_2=inv();
inv_3=inv();
dp[]=num[]=;
dp[]=num[]=;
for(int i=;i<maxn;i++)
{
dp[i]=(num[i-]*dp[i-]%mod+(num[i-]+)*num[i-]%mod*inv_2%mod)%mod;
dp[i-]+=dp[i-];
dp[i-]%=mod;
num[i]=dp[i];
}
int n;
while(cin>>n&&n)
{
ll ans;
if(n==)ans=;
else if(n==)ans=;
else if(n&)
{
ans=num[n/]*(num[n/]+)%mod*inv_2%mod*dp[n/-]%mod;
ans+=num[n/]*(num[n/]+)%mod*(num[n/]+)%mod*inv_2%mod*inv_3%mod;
ans%=mod;
}
else
{
ans=(num[n/]*(num[n/]+))%mod*inv_2%mod;
}
cout<<ans<<endl;
}
return ;
}

代码君

[hdu5136]Yue Fei's Battle 2014 亚洲区域赛广州赛区J题(dp)的更多相关文章

  1. 2014年亚洲区域赛北京赛区现场赛A,D,H,I,K题解(hdu5112,5115,5119,5220,5122)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 下午在HDU上打了一下今年北京区域赛的重现,过了5题,看来单挑只能拿拿铜牌,呜呜. ...

  2. ZOJ3827 ACM-ICPC 2014 亚洲区域赛的比赛现场牡丹江I称号 Information Entropy 水的问题

    Information Entropy Time Limit: 2 Seconds      Memory Limit: 131072 KB      Special Judge Informatio ...

  3. ZOJ3819 ACM-ICPC 2014 亚洲区域赛的比赛现场牡丹江司A称号 Average Score 注册标题

    Average Score Time Limit: 2 Seconds      Memory Limit: 131072 KB Bob is a freshman in Marjar Univers ...

  4. 第39届ACM亚洲区域赛牡丹江赛区赛后总结

    2014年10月10日,周五,早晨匆匆忙忙的出了寝室,直奔复印社去打了两份模板,然后直接就去上课了.第三节课下课,直接跟老师讲了一声,就去实验室跟学长们汇合了.12点半,踏上了开往牡丹江的列车,我们那 ...

  5. 2013 ACM-ICPC亚洲区域赛南京站C题 题解 轮廓线DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4804 题目大意 给你一个 \(n \times m\) 的矩形区域.你需要用 \(1 \times 1 ...

  6. 2018ACM-ICPC亚洲区域赛南京站I题Magic Potion(网络流)

    http://codeforces.com/gym/101981/attachments 题意:有n个英雄,m个敌人,k瓶药剂,给出每个英雄可以消灭的敌人的编号.每个英雄只能消灭一个敌人,但每个英雄只 ...

  7. 动态规划(计数DP):HDU 5136 Yue Fei's Battle

    Yue Fei's Battle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Other ...

  8. 2015 ACM / ICPC 亚洲区域赛总结(长春站&北京站)

    队名:Unlimited Code Works(无尽编码)  队员:Wu.Wang.Zhou 先说一下队伍:Wu是大三学长:Wang高中noip省一:我最渣,去年来大学开始学的a+b,参加今年区域赛之 ...

  9. 2019~2020icpc亚洲区域赛徐州站H. Yuuki and a problem

    2019~2020icpc亚洲区域赛徐州站H. Yuuki and a problem 题意: 给定一个长度为\(n\)的序列,有两种操作: 1:单点修改. 2:查询区间\([L,R]\)范围内所有子 ...

随机推荐

  1. 范围for语句 && 列表初始值&& 标准库函数begin和end

    范围for语句: 引入的意义:简化传统for的编写,主要用于遍历给定序列中的每个元素并对序列中的每个值执行某种操作,其语法形式是: for( 声明: 给定序列) { 执行的操作. } 其中,“给定序列 ...

  2. android图像模糊技术

    今天我们来更深入了解一下Android开发上的模糊技术.我读过几篇有关的文章,也在StackOverFlow上看过一些相关教程的帖子,所以我想在这里总结一下学到的东西. 为什么学习这个模糊技术? 现在 ...

  3. var_export函数的使用方法

    var_export() 函数返回关于传递给该函数的变量的结构信息,它和 var_dump() 类似,不同的是其返回的表示是合法的 PHP 代码.var_export必须返回合法的php代码, 也就是 ...

  4. sudo 无法解析主机的解决办法

    错误存在于更改主机名字后,解决办法如下: sudo gedit /etc/hosts找到如下行:127.0.1.1       XXX将其修改为:127.0.1.1       (你现在的主机名) 保 ...

  5. bootstrap 模态框动态加载数据

    .页面中添加modal <!-- 模态框(Modal) --> <div class="modal fade" id="showModal" ...

  6. 163k地方门户网站系统团购定时结束限量控制

    #coding=utf8 #!/usr/bin/env python # 网站自动审核系统 import pymssql import re import sys import datetime im ...

  7. 通过IIS发布站点和VS2012自带发布网站

    vs2012通过IIS发布站点 http://jingyan.baidu.com/article/0964eca2d7beeb8285f536bd.html 用VS2012自带发布网站 http:// ...

  8. windows下wchar_t* 转char*

    这个在windows下很常见,常用,留个档. 一般用这个函数: size_t wcstombs( char *mbstr, const wchar_t *wcstr, size_t count ); ...

  9. C++基本要点复习--------coursera程序设计实习(PKU)的lecture notes

    因为一些特性复杂,很多时候也用不到一些特性,所以忘记了,算是随笔,也当作一个临时查找的手册.没有什么顺序,很杂. 1.构造函数通过函数重载的机制可以有多个(不同的构造函数,参数个数,或者参数类型不同. ...

  10. centos 6.5 hadoop 2.3 初配置

    为了安装hadoop废了好大的劲才把esxi5.5给装好. 同时装了centos6.5,由于hadoop里面有个免密码登陆所以这里讲的就是免密码登陆. 看了大家的博客文章发现转发的一部分,写ubunt ...