牛客多校第二场 A run (dp)

链接:https://www.nowcoder.com/acm/contest/140/A
来源:牛客网
 

题目描述

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(i,0/1)表示到了i,最后一步是走还是跳的方案数

状态转移一下即可

#include <bits/stdc++.h>
#define fir first
#define se second
#define pb push_back
#define ll long long
#define mp make_pair
using namespace std;
const int maxn=1e5+10;
const int maxm=1e6+10;
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
const double eps=1e-7;
int q,k;
pair<int,int> p[maxn];
ll dp[maxn][2];
//dp(i,j)表示走了i米,最后一步用跑/走的方案数
int Max=0;
ll sum[maxn][2];
int main(){
    cin>>q>>k;
    for (int i=1;i<=q;i++){
        cin>>p[i].fir>>p[i].se;
        Max=max(Max,p[i].se);
    }
    for (int i=0;i<k;i++){
        dp[i][0]=1;
    }
    for (int i=k;i<=Max;i++){
        dp[i][0]=(dp[i-1][0]+dp[i-1][1])%mod;
        dp[i][1]=(dp[i-k][0])%mod;
    }
    sum[0][0]=dp[0][0];
    sum[0][1]=dp[0][1];
    for (int i=1;i<=Max;i++){
        sum[i][0]=(sum[i-1][0]+dp[i][0])%mod;
        sum[i][1]=(sum[i-1][1]+dp[i][1])%mod;
    }
    for (int i=1;i<=q;i++){
        ll ans=(sum[p[i].se][0]-sum[p[i].fir-1][0])%mod;
        ans=(ans+mod)%mod;
        ans=(ans+(sum[p[i].se][1]-sum[p[i].fir-1][1])%mod)%mod;
        ans=(ans+mod)%mod;
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wyj_alone_smile/article/details/81149572