1. 程式人生 > >補題:牛客網暑期ACM多校訓練營(第二場)-A-run

補題:牛客網暑期ACM多校訓練營(第二場)-A-run

題目大意
白雲在健身,每秒可以走1米或跑k米,並且不能連續兩秒都在跑。
當它的移動距離在[L,R]之間時,可以選擇結束鍛鍊。
問有多少種方案結束。

題目描述
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

學習:

DP f[i][0/1]表示已經跑了i米,最後一步是跑還是走的方案數。
f[i][1]=f[i-k][0],f[i][0]=f[i-1][0]+f[i-1][1] 字首和
sum[i]=(sum[i-1]+dp[i][1]+dp[i][0])%mod;

#include<bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int main()
{
    int q,k;
    int l,r;
    int dp[100005][2];
    int sum[100005];
    while(~scanf("%d%d",&q,&k))
    {
        memset(sum,0,sizeof(sum));
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=1; i<=100005; i++)
        {
            dp[i][0]=(dp[i-1][0]+dp[i-1][1])%mod;
            if(i>=k)
            {
                dp[i][1]=dp[i-k][0];
            }
            sum[i]=(sum[i-1]+dp[i][1]+dp[i][0])%mod;
        }
        while(q--)
        {
            scanf("%d%d",&l,&r);
            cout<<(sum[r]-sum[l-1]+mod)%mod<<endl;
        }
    }
}