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

题意:

类似斐波那契数列,就是问走L米或者R米,一共几种方式,走1米或者K米,不能连续走K米

代码:

#include<bits/stdc++.h>
using namespace std;
long long  a[100015],sum[100015];
long long dp[100015][2];
#define mod 1000000007
int main()
{
    int k,q,i,j,aa,bb;
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    dp[0][1]=0;//1代表跑
    scanf("%d%d",&q,&k);
    sum[0]=0;
    for(i=1;i<k;i++)
    {
        dp[i][0]=1;
        dp[i][1]=0;
        sum[i]=sum[i-1]+dp[i][0];
    }
    for(;i<=100005;i++)
    {
        dp[i][0]=dp[i-1][0]+dp[i-1][1];
        dp[i][0]=dp[i][0]%mod;
        dp[i][1]=dp[i-k][0];
        dp[i][1]=dp[i][1]%mod;
        sum[i]=sum[i-1]+dp[i][0]+dp[i][1];
        sum[i]=sum[i]%mod;
    }
 
    while(q--)
    {
        scanf("%d%d",&aa,&bb);
        printf("%lld\n",(sum[bb]-sum[aa-1]+mod)%mod);
    }
}
扫描二维码关注公众号,回复: 2305736 查看本文章

猜你喜欢

转载自blog.csdn.net/lml11111/article/details/81148868
run