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 个数中,至少m个可以等价为,在 n 个数中至多 n 个数 - 在 n 个数中至多(n-m)个。
在这道题中,可以令dp[i][0] 表示至多n个G,且第 i 个为G,在 i <= n 的时候,可以随意放置,因为不论前面的是什么,都不会使G 超出n;当 i == n+1 的时候,我们要排除前 n 个都为G 的情况;当 i > n+1的时候,我们要排除(i-u-1)到 (i-1)之前全为G 的情况。同理,R,P也使用相同的方法计算。
#include <bits/stdc++.h>
using namespace std;
#define Maxn 1100000
#define LL long long
#define M 1000000007
LL dp[Maxn][5]; //dp[i][0]表示第i个为G,至多有n个连续G,至多有m个连续R的个数
//dp[i][1]表示第i个为R,....
//dp[i][2]表示第i个为P,....
LL n,m,k;
LL Cal(int m,int k)
{
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 <= m)
dp[i][0]=sum;
else if(i == m+1)
dp[i][0]=(sum-1)%M;
else
dp[i][0]=(sum-dp[i-m-1][1]-dp[i-m-1][2])%M;
if(i <= k)
dp[i][1]=sum;
else if(i == k+1)
dp[i][1]=(sum-1)%M;
else
dp[i][1]=(sum-dp[i-k-1][0]-dp[i-k-1][2])%M;
}
return (dp[n][0]+dp[n][1]+dp[n][2])%M;
}
int main()
{
while(cin >> n >> m >> k){
LL ans;
ans=Cal(n,k);
ans=((ans-Cal(m-1,k))%M+M)%M;
cout << ans << endl;
}
return 0;
}