【dp】递推 zoj3747

Attack on Titans

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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.


题目意思:有G,R,P三种总人数和为N的士兵排成一排,问至少有M个连续的G士兵且至多有K个连续的R士兵的情况有多少种。采用动态规划的递推方法,因为至少和至多是两种情况不一样不好算,所以我们把它转化为都求至多的问题。也就是说先求①至多N个连续G和且至多K个连续R的情况数。②至多M-1个连续G且至多K个连续R的情况数。   ①减②即为符合至少M个G且至多K个R的情况。


//dp[i][0]表示第i个为G,至多有u个连续G,至多有v个连续R的个数  //这里的u和v根据题目已经确定好了

//dp[i][1]表示第i个为R,....

 //d[i][2]表示第i个为P,....

当第i个为P的情况很好考虑不会对连续的R和G产生影响,dp[i][2]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2]; 把这一串命名为sum

当第i个为G时

如果i<=u 时 无论怎么放都不会超过u个连续的G这个限制条件 所以dp[i][0]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];

如果i=u+1时,要排除前u个都放了G的情况,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-1开始往前能数出来u个G: {(i-1)-(i-u)+1=u}  】

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];//此时要减去i前面已经出现连续u个G的情况,即从i-u到i-1这一段都是G,那么i-1-u的位置可以是P或者R

                             

当第i个为R时如果i<=v 时 无论怎么放都不会超过v个连续的R这个限制条件 所以dp[i][1]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];

如果i=v+1时,要排除前v个都放了R的情况,dp[i][1]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2]-1;

如果i>v+1时,要排除从i-1到i-v位置都放了R的情况,dp[i][1]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2]-dp[i-v-1][0]-dp[i-v-1][2];

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std;

#define MEM(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define debug printf("!/m")
#define INF 1100000
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define MOD 1000000007

LL dp[INF][5];
LL n,m,k,u,v;

LL Cal()
{
          dp[0][0] = 1;//三个dp初始化时可以随意一个为1,没有什么影响,因为后面已经把所有情况考虑进去
          dp[0][1] = 0;
          dp[0][2] = 0;
          int i;
          for(i = 1;i<=n;i++)
          {
                    LL sum = (dp[i-1][0] + dp[i-1][1] + dp[i-1][2])%MOD;
                    dp[i][2]=sum;
                    if(i<=u)
                              dp[i][0] = sum;
                    else if(i==u+1)
                              dp[i][0] = (sum-1)%MOD;
                    else
                              dp[i][0] = (sum - dp[i-u-1][1] - dp[i-u-1][2])%MOD;

                    if(i<=v)
                              dp[i][1] = sum;
                    else if(i==v+1)
                              dp[i][1] = (sum - 1)%MOD;
                    else
                              dp[i][1] = (sum - dp[i-v-1][0] - dp[i-v-1][2])%MOD;

          }
          return (dp[n][0]+dp[n][1]+dp[n][2])%MOD;
}


int main()
{
          while(~sf("%lld%lld%lld",&n,&m,&k))
          {
                    u = n;v = k;
                    LL ans = Cal();
                    u = m-1;v = k;
                    ans = ((ans - Cal())%MOD+MOD)%MOD;//这里注意 防止ans-Cal()为负值 要加个MOD
                    pf("%lld\n",ans);
          }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值