1. 程式人生 > >[遞推dp] zoj 3747 Attack on Titans

[遞推dp] zoj 3747 Attack on Titans

題目連結:

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.


題目意思:

給n個士兵排隊,每個士兵三種G、R、P可選,求至少有m個連續G士兵,最多有k個連續R士兵的排列的種數。

解題思路:

dp遞推。

先把問題都轉化成至多連續的情況:至多k個連續R,至多n個連續G情況 【減去】至多k個連續R,至多(m-1)個連續G情況。

至多的情況比較好考慮,至少的情況比較複雜,比賽的時候一直落在至少的圈子裡,沒想到用遞推。

//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];

當第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的情況,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個為R時

如果i<=v 時 無論怎麼放都不會超過u個連續的G這個限制條件 所以dp[i][1]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];

如果i=v+1時,要排除前v個都放了G的情況,dp[i][1]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2]-1;

如果i>v+1時,要排除從i-1到i-v位置都放了G的情況,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<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 1100000

LL dp[Maxn][5]; //dp[i][0]表示第i個為G,至多有u個連續G,至多有v個連續R的個數
                //dp[i][1]表示第i個為R,....
                //dp[i][2]表示第i個為P,....
LL n,m,k,u,v;

LL Cal()
{
    dp[0][0]=1; //初始狀態
    dp[0][1]=0;
    dp[0][2]=0;

    for(int i=1;i<=n;i++)
    {
       LL sum=(dp[i-1][0]+dp[i-1][1]+dp[i-1][2])%M;
       dp[i][2]=sum;

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

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

       //printf("u:%lld v:%lld i:%d %lld %lld %lld\n",u,v,i,dp[i][0],dp[i][1],dp[i][2]);
       //system("pause");

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


int main()
{
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   while(~scanf("%lld%lld%lld",&n,&m,&k))
   {
       LL ans;
       u=n,v=k;
       ans=Cal();

       //printf(":%lld\n",ans);
       //system("pause");

       u=m-1,v=k;
       //printf(":%lld\n",Cal());
       //system("pause");
       ans=((ans-Cal())%M+M)%M;
       printf("%lld\n",ans);


   }
   return 0;
}