White Cloud is exercising in the playground.
White Cloud can walk 1 meters or run k meters per second.
Since White Cloud is tired,it can’t run for two or more continuous seconds.
White Cloud will move L to R meters. It wants to know how many different ways there are to achieve its goal.
Two ways are different if and only if they move different meters or spend different seconds or in one second, one of them walks and the other runs.
Input
The first line of input contains 2 integers Q and k.Q is the number of queries.(Q≤100000,2≤k≤100000)
For the next Q lines,each line contains two integers L and R.(1≤L≤R≤100000)
Output
For each query,print a line which contains an integer,denoting the answer of the query modulo 1000000007.
Example
Input
3 3
3 3
1 4
1 5
Output
2
7
11
题意理解困难:will move L to R meters 意思是求走L,L+1,…,R米的种类数,而不是从L走到R米
先用dp求出不同米的种类数:表示走i米,使用:0走路,1跑步 的种类数
dp[i][0]=(dp[i-1][0]+dp[i-1][1])%mod;
dp[i][1]=dp[i-k][0]%mod;
同时求个前缀和ans[x],方便直接输出答案ans[r]-ans[l-1]
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod=1e9+7;
ll dp[100100][5],ans[100100];
int main()
{
int k,q,l,r;
scanf("%d%d",&q,&k);
dp[0][0]=1;
for(int i=1;i<=100000;i++)
{
dp[i][0]=(dp[i-1][0]+dp[i-1][1])%mod;
if(i>=k) dp[i][1]=dp[i-k][0]%mod;
ans[i]=(dp[i][0]+dp[i][1])%mod;
ans[i]=(ans[i]+ans[i-1])%mod;
}
while(q--)
{
cin>>l>>r;
printf("%lld\n",(((ans[r]-ans[l-1])%mod) + mod)%mod);
}
return 0;
}