zoj 3747 递推好题

来源:互联网 发布:手机灯牌软件 编辑:程序博客网 时间:2024/05/22 15:56

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 ofN people, numbered from1 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 thanK 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 三个字母进行填充

前提是:

G必须单独在一块,R必须单独在一块

G最少使用 M 次,R最多使用 K 次

问一共有多少的组合方式

题解:

对于同时存在至多至少的问题,我们可以转化为至多问题的差

比如:(G最少使用M次)   ==  ( G最多使用N次  减去  G最多使用M-1次)


dp1   dp2    dp3  这三个数组分别表示在 i  个位置G R P  出现的时候组合的方式有多少种

状态转移方程:

if(i<=num1) dp1[i]=temp;else dp1[i]=((temp-dp2[i-1-num1]-dp3[i-1-num1])%mod+mod)%mod;
意思是:

至多有num1 个 G 出现的时候

当 i <= num1 的时候,G可以任意次数出现

else  在 i 这个位置出现了G,那么包括当前位置的num1个单位都得是存放G

          然后就得将之前已经出现过num1个G 的情况减去,即i-u 到 i-1都是G,那么i-1-U都是其他字母,转化过来减去即可

同理可以求出所需的所有答案然后相减就可以得到答案了



#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int n,m,k;const int mod=1000000007;#define LL long long#define MAXN 1000010LL dp1[MAXN];LL dp2[MAXN];LL dp3[MAXN];LL deal(int num1,int num2){    dp1[0]=dp2[0]=0;    dp3[0]=1;    for(int i=1;i<=n;i++){        LL temp=(dp1[i-1]+dp2[i-1]+dp3[i-1])%mod;        dp3[i]=temp;        if(i<=num1) dp1[i]=temp;        else dp1[i]=((temp-dp2[i-1-num1]-dp3[i-1-num1])%mod+mod)%mod;        if(i<=num2) dp2[i]=temp;        else dp2[i]=((temp-dp1[i-1-num2]-dp3[i-1-num2])%mod+mod)%mod;    }    return (dp1[n]+dp2[n]+dp3[n])%mod;}int main(){    //freopen("in.txt","r",stdin);    while(scanf("%d%d%d",&n,&m,&k)!=EOF)        printf("%lld\n",((deal(n,k)-deal(m-1,k))%mod+mod)%mod);    return 0;}


0 0