进击的巨人 (有限制的DP)
❓ 问题描述
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<stdio.h>
#include<string.h>
#define N 1000001
#define mod 1000000007
typedef long long int ll;
ll n,m,k,sum=0;ll dp[N][4];
//写法2
ll f1(ll u)
{ ll i,ans=1;//i==1初始都为第一位为 任意字母都为1
dp[0][2]=dp[0][1]=1;//处理 i==u+1 和 i==k+1的特俗情况
dp[0][0]=0;
for(i=1;i<=n;i++)
{
dp[i][0]=ans;
if(i<=u)dp[i][1]=ans;
else dp[i][1]=(ans-dp[i-u-1][0]-dp[i-u-1][2])%mod;
if(i<=k)dp[i][2]=ans;
else dp[i][2]=(ans-dp[i-k-1][0]-dp[i-k-1][1])%mod;
ans=(dp[i][0]+dp[i][1]+dp[i][2])%mod;
}
return ans;
}
/*
//写法1
ll f1(ll u)
{ ll i,ans=1;//i==1初始都为第一位为 任意字母都为1
for(i=1;i<=n;i++)
{
dp[i][0]=ans;
if(i<=u)dp[i][1]=ans;
else if(i==u+1)dp[i][1]=ans--;
else dp[i][1]=(ans-dp[i-u-1][0]-dp[i-u-1][2])%mod;
if(i<=k)dp[i][2]=ans;
else if(i==k+1)dp[i][2]=ans--;
else dp[i][2]=(ans-dp[i-k-1][0]-dp[i-k-1][1])%mod;
ans=(dp[i][0]+dp[i][1]+dp[i][2])%mod;
}
return ans;
}
*/
int main()
{
while(scanf("%lld%lld%lld",&n,&m,&k)!=EOF)
{
sum=((f1(n)-f1(m-1))%mod+mod)%mod;//防止 为负数
printf("%lld\n",sum);
}
return 0;
}
- 点赞
- 收藏
- 关注作者
评论(0)