简单dp ——run

链接: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

思路:不想说什么了。要认真读题,题目说了it can't run for two or more continuous seconds.。run是跑的意思啊!!!!!然后就是不能连续跑啊啊啊啊啊啊!烦死了。比赛时这句话直接忽略掉啊。

而且跑一个和走一个是不一样的走法啊!!!比赛时我写的代码居然还当成了同一种情况~orz

就是用一个dp[i][0]表示最后到达i点的那一步是walk过来的

dp[i][1]表示最后到达i点的那一步是run过来的。注意初始化。

#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
const int mod=1000000007;
long long sum[maxn];
long long dp[maxn][3];
int main()
{
    int q;int k;
    scanf("%d%d",&q,&k);
    memset(dp,0,sizeof(dp));
    memset(sum,0,sizeof(sum));
    sum[0]=0;
    dp[0][0]=0;
    dp[0][1]=0;
    for(int i=1;i<=k;i++){
        dp[i][0]=1;///到达i点,最后是走过来的
        if(i==k)
            dp[k][1]=1;///到达k点,最后是tiao过来的

        sum[i]=(dp[i][0]+dp[i][1]+sum[i-1])%mod;
       /// cout<<dp[i][0]<<"  ***  "<<dp[i][1]<<endl;

    }
    for(int i=k+1;i<=100008;i++)
    {
       dp[i][0]=dp[i-1][0]+dp[i-1][1];
       ///dp[i][1]=dp[i-k][1]+dp[i-k][0];
       dp[i][1]=dp[i-k][0];
       
       dp[i][0]=dp[i][0];
       dp[i][1]=dp[i][1];
       sum[i]=(dp[i][0]+dp[i][1]+sum[i-1])%mod;
       ///cout<<"i: "<<i<<"  "<<sum[i]<<endl;
    }
    int l,r;
    for(int i=1;i<=q;i++)
    {
        scanf("%d%d",&l,&r);
        cout<<(sum[r]-sum[l-1]+mod)%mod<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/81149536
run