dp[i]表示有i朵花时,有多少种吃法
那么dp[i]=dp[i-1]+dp[i-k];dp[i-1]再吃朵红,dp[i-k]再吃k朵白
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<deque>
#include<functional>
#include<iterator>
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#define CPY(A, B) memcpy(A, B, sizeof(A))
typedef long long LL;
typedef unsigned long long uLL;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos (-1.0);
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
const int maxn = 1e5+5;
LL dp[maxn], ans[maxn];
int main() {
int t, k; scanf ("%d%d",&t,&k);
dp[0]=1;//dp[i]有i朵花时,符合的方案数
for (int i=1; i<=maxn; i++) {
if (i>=k) { dp[i]= (dp[i-1]+ dp[i-k]) %MOD; }
//dp[i-1]再吃朵红,dp[i-k]再吃k朵白
else { dp[i]= dp[i-1] %MOD; }
}
for (int i=1; i<=maxn; i++) {
ans[i]= (ans[i-1]+dp[i]) %MOD;
}
for (int i=0; i<t; i++) {
int l,r; scanf ("%d%d",&l,&r);
cout<< (ans[r]-ans[l-1]+MOD/*!*/) %MOD<<endl;
}
return 0;
}