ZOJ 3747 Attack on Titans

原创 2015年07月08日 22:36:40

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

题意:有三个兵团 GRP,现在需要组成N人团队,至少调动M个连续G士兵,至多调动K个连续R士兵,问有多少种组合情况

思路:由于又是至多又是至少没办法处理,所以统一转换成至多,对于事件A={至多N个G士兵连续},事件B={至多m-1个G士兵连续},那么A-B={连续人数在m到n中取值,即至少m个连续士兵}。统一之后,设定一个二维数组,dp[i][3],dp[i][0]表示对于第i个位置,放G士兵,同理,dp[i][1],dp[i][2]分别表示第i个位置放R,和P。

         最大连续u个G,连续v个R;

        对于当前第i个位置,sum=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];

        由于放P对整个没有影响,所以,dp[i][2]=sum;

        对于A事件  

                       当i <= u 时,dp[i][0]=sum;                                             //至多连续u个,那么小于等于u,就可以直接放G

                       当i==u+1时,dp[i][0]=sum-1;                                      // 对于现在这个位置,之前可能出现连续u个的情况,当前要放G,就要减去那一种情况

                       当i>u+1时,dp[i][0]=sum-dp[i-u-1][1]-dp[i-u-1][2];      //此时要减去i前面已经出现连续u个G的情况,即从i-u到i-1这一段都是G,那么i-1-u的位置可以是P或者R,                                                                                                          //于是减去dp[i-u-1][1],dp[i-1-u][2]合起来总共具有的情况数。

         对于B事件                                                                                 //对于B事件同理

                         当i<=v时,dp[i][1]=sum;

                         当i==v+1时,dp[i][1]=sum-1;

                         当i>v+1时,dp[i][2]=sum-dp[i-v-1][0]-dp[i-v-1][2];


        

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 1000009
#define mod 1000000007
typedef long long ll;

using namespace std;

ll n,m,k;
ll dp[N][5];

ll fun(ll u,ll v)
{
    //memset(dp,0,sizeof dp);
    dp[0][2]=1;                 //初始化时,对于第0个位置,整体为1即可,也可理解为对第0个位置放P,并没有什么影响。
    dp[0][0]=dp[0][1]=0;

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

        dp[i][2]=sum;

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

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

       // cout<<dp[i][0]<<" "<<dp[i][1]<<" "<<dp[i][2]<<endl;
    }
    return (dp[n][0]+dp[n][1]+dp[n][2])%mod;
}

int main()
{
    while(~scanf("%lld%lld%lld",&n,&m,&k))
    {
        ll ans;
        ans=fun(n,k);

       printf("%lld\n",((ans-fun(m-1,k))%mod+mod)%mod);

    }
    return 0;
}








zoj3747(递推)

链接:点击打开链接 题意:n个士兵排队,每个士兵可选择G,R,P三种字母,求至少有m个连续士兵选G,最多有k个连续士兵选R的排列的种数 代码: #include #include #include...
  • stay_accept
  • stay_accept
  • 2016年04月28日 21:26
  • 613

zoj 3747

Attack on Titans Time Limit: 2 Seconds      Memory Limit: 65536 KB Over centuries ago, mankind...
  • my_acm
  • my_acm
  • 2014年09月02日 21:55
  • 665

ZOJ 3747 Attack on Titans(DP)

Over centuries ago, mankind faced a new enemy, the Titans. The difference of power between mankind a...
  • nare123
  • nare123
  • 2016年03月09日 17:33
  • 216

zoj 3747 Attack on Titans 递推 计数dp

题意:给定三种士兵G,R,P,有无限个,要求取出一个排列为n人,要求满足至少m个连续个G士兵,至多k个连续的R士兵,P士兵不作要求。求排列总数,对1000000007取模。 思路:http://bl...
  • chen_minghui
  • chen_minghui
  • 2017年06月15日 19:37
  • 153

zoj 3747 递推好题

Attack on Titans Time Limit: 2 Seconds      Memory Limit: 65536 KB Over centuries ago, mankind ...
  • Summer__show_
  • Summer__show_
  • 2017年04月19日 17:55
  • 284

xtu 1233 Coins && zoj 3747 Attack on Titans

xtu 题目意思: 有n个硬币排成一排,求至少连续m个硬币正面朝上的方案数。 给定一个序列,正面朝上硬币连续值范围【0--n】 一般至少转换成至多,总方案数(2^n)- 至多(m-1)个硬币正面朝上...
  • chen_lin111
  • chen_lin111
  • 2016年01月09日 22:31
  • 345

ZOJ 3747 Attack on Titans

原题地址 题意 存在三种士兵G,R,P站成一排,总共N人,要求至少有M个G类士兵连续地站着,至多有K个R类士兵连续地站着,问合法的排列数。 题解 至少有M个G类士兵连续地站着这...
  • zwsign
  • zwsign
  • 2016年04月28日 17:41
  • 187

ZOJ 3747 Attack on Titans【带限制条件的递推dp+计数技巧】

题意:进击的巨人的题目背景(简直自带画面感和音效orz...),说要组建Levi班,现在从三个兵团中欲选出共n个人,对他们进行编号,编号有限制条件:驻扎兵团中至少要有m个分配到连续的编号,调查兵团中至...
  • MartaYang
  • MartaYang
  • 2017年08月15日 21:20
  • 118

ZOJ 题目3747 Attack on Titans(DP)

Attack on Titans Time Limit: 2 Seconds      Memory Limit: 65536 KB Over centuries ago, mank...
  • yu_ch_sh
  • yu_ch_sh
  • 2015年09月15日 16:48
  • 344

ZOJ 3747 Attack on Titans

Attack on Titans Time Limit: 2 Seconds      Memory Limit: 65536 KB Over centuries ago, mankind fa...
  • wust_ZJX
  • wust_ZJX
  • 2015年07月08日 22:36
  • 939
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ZOJ 3747 Attack on Titans
举报原因:
原因补充:

(最多只允许输入30个字)