HDU 5136 Yue Fei's Battle
Yue Fei's Battle
Time Limit: 1000ms
Memory Limit: 512000KB
This problem will be judged on HDU. Original ID: 513664-bit integer IO format: %I64d Java class name: Main
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):
![](http://www.bnuoj.com/v3/images/hdu/C558-1010-1.jpg)
Please bring your computer and go back to Yue Fei's time to help him so that you may change the history.
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):
![](http://www.bnuoj.com/v3/images/hdu/C558-1010-1.jpg)
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.
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
Source
解题:
![](https://images2015.cnblogs.com/blog/648385/201509/648385-20150911183016012-1593806717.png)
![](https://images2015.cnblogs.com/blog/648385/201509/648385-20150911183023919-1141491187.png)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const LL mod = 1000000007; 5 const int maxn = 100010; 6 LL dp[maxn],sum[maxn]; 7 LL gcd(LL a,LL b,LL &x,LL &y){ 8 if(!b) { 9 x = 1; 10 y = 0; 11 return a; 12 } 13 LL ret = gcd(b,a%b,y,x); 14 y -= x*(a/b); 15 return ret; 16 } 17 LL Inv(LL b,LL mod){ 18 LL x,y,d = gcd(b,mod,x,y); 19 return d == 1?(x%mod + mod)%mod:-1; 20 } 21 const LL inv2 = Inv(2,mod); 22 const LL inv6 = Inv(6,mod); 23 LL solve(int k){ 24 int i = (k>>1); 25 LL ret = (dp[i]*(dp[i] + 1)%mod*inv2%mod); 26 if((k&1) == 0) return ret; 27 ret = ret*sum[i-1]%mod; 28 ret = (ret + (dp[i]*(dp[i] - 1 + mod)%mod))%mod; 29 ret = (ret + dp[i]*(dp[i] - 1 + mod)%mod*(dp[i] -2 + mod)%mod*inv6%mod)%mod; 30 ret = (dp[i] + ret)%mod; 31 return ret; 32 } 33 int main(){ 34 sum[0] = dp[0] = dp[1] = 1; 35 sum[1] = 2; 36 for(int i = 2; i < maxn; ++i){ 37 dp[i] = (dp[i-1]*(dp[i-1] + 1)%mod*inv2%mod + dp[i-1]*sum[i-2]%mod)%mod; 38 sum[i] = (sum[i-1] + dp[i])%mod; 39 } 40 int k; 41 while(scanf("%d",&k),k) printf("%I64d\n",solve(k)); 42 return 0; 43 }
夜空中最亮的星,照亮我前行