1. 程式人生 > >牛客網暑期ACM多校訓練營(第二場).run(遞推)

牛客網暑期ACM多校訓練營(第二場).run(遞推)

時間限制: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

參考程式碼:

#include <iostream>
using namespace std;

int main()
{
    long long b[100001],sum[100001];
    int q,k,x,y;
    cin>>q>>k;
    b[0]=0,sum[0]=0;
    for(int i=1;i<k;i++) b[i]=1;
    b[k]=2,b[k+1]=3;
    for(int i=k+2;i<100001;i++) b[i]=b[i-1]+b[i-k-1];
    for(int i=1;i<100001;i++) sum[i]=sum[i-1]+b[i];
    for(int i=0;i<q;i++){
        cin>>x>>y;
        cout<<(sum[y]-sum[x]+b[x])%1000000007<<endl;
    }
    return 0;
}