题目描述:
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.
输入描述:
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)
输出描述:
For each query,print a line which contains an integer,denoting the answer of the query modulo 1000000007.
示例1
输入
3 3
3 3
1 4
1 5
输出
2
7
11
思路:
二维版本
#include<iostream>
using namespace std;
typedef long long ll;
const int N=100010, mod=1000000007;
int l,r,k;
ll f[N][2], pre[N];
int main()
{
int T;
scanf("%d%d",&T,&k);
f[0][0]=1;
for(int i=1;i<=100000;i++)
{
f[i][0]=(f[i-1][0]+f[i-1][1])%mod;
if(i>=k) f[i][1]=f[i-k][0];
pre[i]=(pre[i-1]+(f[i][0]+f[i][1])%mod)%mod;
}
while(T--)
{
scanf("%d%d",&l,&r);
printf("%lld\n",(pre[r]+mod-pre[l-1])%mod);
}
return 0;
}
一维版本
#include<iostream>
using namespace std;
typedef long long ll;
const int N=100010, mod=1000000007;
int l,r,k;
ll f[N], pre[N];
int main()
{
int T;
scanf("%d%d",&T,&k);
for(int i=0;i<k;i++) f[i]=1;
f[k]=2;
for(int i=k+1;i<=100000;i++)
f[i]=f[i-1]+f[i-k-1]; // 防止连跳
for(int i=1;i<=100000;i++)
pre[i]=(pre[i-1]+f[i])%mod;
while(T--)
{
scanf("%d%d",&l,&r);
printf("%lld\n",(pre[r]+mod-pre[l-1])%mod);
}
return 0;
}