ZOJ 3747 Attack on Titans

Attack on Titans

Time Limit: 2000ms
Memory Limit: 65536KB
This problem will be judged on ZJU. Original ID: 3747
64-bit integer IO format: %lld      Java class name: Main
 

Over centuries ago, mankind faced a new enemy, the Titans. The difference of power between mankind and their newfound enemy was overwhelming. Soon, mankind was driven to the brink of extinction. Luckily, the surviving humans managed to build three walls: Wall Maria, Wall Rose and Wall Sina. Owing to the protection of the walls, they lived in peace for more than one hundred years.

But not for long, a colossal Titan appeared out of nowhere. Instantly, the walls were shattered, along with the illusory peace of everyday life. Wall Maria was abandoned and human activity was pushed back to Wall Rose. Then mankind began to realize, hiding behind the walls equaled to death and they should manage an attack on the Titans.

So, Captain Levi, the strongest ever human being, was ordered to set up a special operation squad of N people, numbered from 1 to N. Each number should be assigned to a soldier. There are three corps that the soldiers come from: the Garrison, the Recon Corp and the Military Police. While members of the Garrison are stationed at the walls and defend the cities, the Recon Corps put their lives on the line and fight the Titans in their own territory. And Military Police serve the King by controlling the crowds and protecting order. In order to make the team more powerful, Levi will take advantage of the differences between the corps and some conditions must be met.

The Garrisons are good at team work, so Levi wants there to be at least M Garrison members assigned with continuous numbers. On the other hand, members of the Recon Corp are all elite forces of mankind. There should be no more than K Recon Corp members assigned with continuous numbers, which is redundant. Assume there is unlimited amount of members in each corp, Levi wants to know how many ways there are to arrange the special operation squad.

Input

There are multiple test cases. For each case, there is a line containing 3 integers N (0 < N < 1000000), M (0 < M < 10000) and K (0 < K < 10000), separated by spaces.

Output

One line for each case, you should output the number of ways mod 1000000007.

Sample Input

3 2 2

Sample Output

5

Hint

Denote the Garrison, the Recon Corp and the Military Police as G, R and P. Reasonable arrangements are: GGG, GGR, GGP, RGG, PGG.

 

Source

Author

ZOU, Xun
 
解题:很好的dp题,看了Accagain 的解题思路才搞懂还可以这么做
 
题目大意就是:给n个士兵排队,每个士兵三种G、R、P可选,求至少有m个连续G士兵,最多有k个连续R士兵的排列的种数。
 
我们可以先求出之多有n个连续G士兵,最多连续有K个R士兵的排列数
 
然后减去最多有m-1个连续G士兵,最多连续有k个R士兵的排列数 
 
差就是我们所求的至少有m个连续G士兵,最多有k个连续R士兵的排列方案数
 
但是现在如何求出至多u个连续G士兵 且至多v个连续R士兵的排列方案数
 
我们可以令dp[i][0]表示至多u个连续G士兵 且第i个就是G
dp[i][1]表示至多v个连续R士兵,且第i个士兵就是R士兵
dp[i][2]就是表示第i个是P士兵
当i <= u 时,很明显,dp[i][0] = dp[i-1][0] + dp[i-1][1] + dp[i-1][2]
不关第i-1位是什么兵种,都不会导致G士兵超过约束数
当i = u + 1时,我们要排出一种情况就是 1 到 u都是G的情况,如果此时在u + 1号位置放置G士兵,G士兵数量将大于u
故此时的转移方程是dp[i][0] = dp[i-1][0] + dp[i-1][1] + dp[i-1][2] - 1;
当 i > u + 1时,我们最担心的是什么?第i - 1 到 i - u都是G士兵,那么此时在i号位置放置G士兵,也将不符合约束
我们可以减去不符合约束的方案数,当 i - 1 到 i - u都是G士兵时 有多少种方案?肯定当i - u -1是R或者P啦 
dp[i][0] = dp[i-1][0] + dp[i-1][1] + dp[i-1][2] - dp[i-u-1][1] - dp[i-u-1][2]
 
类似的可以推出dp[i][1]的转移方程
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int mod = 1000000007;
 5 const int maxn = 2000010;
 6 LL dp[maxn][3];
 7 int n,m,k;
 8 LL solve(int u,int v){
 9     dp[0][0] = 1;
10     dp[0][1] = dp[0][2] = 0;
11     for(int i = 1; i <= n; ++i){
12         LL sum = (dp[i-1][0] + dp[i-1][1] + dp[i-1][2])%mod;
13         dp[i][2] = sum;
14         if(i <= u) dp[i][0] = sum;
15         if(i == u + 1) dp[i][0] = (sum - 1 + mod)%mod;
16         if(i > u + 1) dp[i][0] = (sum - dp[i - u - 1][1] - dp[i - u - 1][2] + mod)%mod;
17         if(i <= v) dp[i][1] = sum;
18         if(i == v + 1) dp[i][1] = (sum - 1 + mod)%mod;
19         if(i > v + 1) dp[i][1] = (sum - dp[i - v - 1][0] - dp[i - v - 1][2] + mod)%mod;
20     }
21     return (dp[n][0] + dp[n][1] + dp[n][2])%mod;
22 }
23 int main(){
24     while(~scanf("%d%d%d",&n,&m,&k))
25         printf("%lld\n",((solve(n,k) - solve(m-1,k))%mod + mod)%mod);
26     return 0;
27 }
View Code

 

posted @ 2015-09-07 14:01  狂徒归来  阅读(911)  评论(0编辑  收藏  举报