Description
There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob asks you to paint all storages with two colors: red and blue. Each storage will be painted with exactly one color.
Bob has a requirement: there are at least M continuous storages (e.g. "2,3,4" are 3 continuous storages) to be painted with red. How many ways can you paint all storages under Bob's requirement?
Input
There are multiple test cases.
Each test case consists a single line with two integers: N and M (0<N, M<=100,000).
Process to the end of input.
Output
One line for each case. Output the number of ways module 1000000007.
Sample Input
4 3
Sample Output
3
题意:n个格子排成一条直线,能够选择涂成红色或蓝色,问最少 m 个连续为红色的方案数。
思路:DP,分两种情况,一种是对于第i个,假设前i-1个已经有了,那么第i个就无所谓了。还有一种是加上第i个才干构成m个连续的话,那么第i-m个就是蓝色的,然后让前i-1-m个不包括连续m个的红色。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
const int maxn = 100005;
const int mod = 1000000007;
ll f[maxn], dp[maxn];
int n, m;
int main() {
f[0] = 1;
for (int i = 1; i < maxn; i++)
f[i] = f[i-1] * 2 % mod;
while (scanf("%d%d", &n, &m) != EOF) {
if (m > n) {
printf("0\n");
continue;
}
memset(dp, 0, sizeof(dp));
dp[m] = 1;
for (int i = m+1; i <= n; i++)
dp[i] = ((dp[i-1] * 2 + f[i-1-m] - dp[i-m-1]) % mod + mod) % mod;
printf("%lld\n", dp[n]);
}
return 0;
}