看了题解才知道用dp。。。原来题意都已经理解错了。。。查
题意:
有x朵花,其中有白花、红花不一。让你找出连续k朵花为白花的种数(当然x朵花里可以有多组k朵白花)。
现在让你找出a朵花~b朵花之间究竟有多少符合条件的。
思路:
DP
状态定义:dp[i]:有i朵花时符合条件的种数。
状态转移:dp[i] = dp[i-1]+dp[i-k]; dp[i-1]:i位置放置红花;dp[i-k]:放白花。
AC代码:
/* **********************************************
Created Time: 2014/10/7 15:20:20
File Name : cf.cpp
*********************************************** */
#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <functional>
#include <algorithm>
typedef long long LL;
using namespace std;
const int MAXN = 1e5+5;
const int MOD = 1e9+7;
LL dp[MAXN], ans[MAXN];
int main()
{
int t, k;
cin>>t>>k;
dp[0] = 1;
for(int i = 1;i <= MAXN; i++)
dp[i] = (dp[i-1] + (i >= k? dp[i-k] : 0)) % MOD;
for(int i = 1;i <= MAXN; i++)
ans[i] = (ans[i-1] + dp[i]) % MOD;
int l, r;
for(int i = 0;i < t; i++)
{
cin>>l>>r;
cout<<(ans[r] - ans[l-1] + MOD)%MOD<<endl;
}
return 0;
}