链接:https://www.nowcoder.com/acm/contest/140/A
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
题目描述
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
题意:每次可以走1米或k米,问走到[l,r]内任一点的方案数。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1000000007;
ll dp[100005][2];
ll sum[100005];
int main()
{
int q,k,i,j,l,r;
scanf("%d%d",&q,&k);
for(i=1;i<k;i++)
{
dp[i][0]=1;//走着从i-1米到达i米处的方案数
dp[i][1]=0;//跑着从i-k到达i米处的方案数
}
dp[k][1]=1;dp[k][0]=1;
sum[0]=0;
for(i=k+1;i<=100005;i++)
{
dp[i][0]=dp[i-1][0]+dp[i-1][1];//走到i米处的方案数
dp[i][1]=dp[i-k][0];//跳到i米出的方案数
}
sum[0]=0;
for(i=1;i<=100005;i++)
sum[i]=(sum[i-1]+dp[i][0]+dp[i][1])%mod;
while(q--)
{
scanf("%d%d",&l,&r);
printf("%lld\n",(sum[r]-sum[l-1]+mod)%mod);
}
}