牛客网暑期ACM多校训练营(第二场) A run

题目:
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.

Input:
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)

Output:
For each query,print a line which contains an integer,denoting the answer of the query modulo 1000000007.

Sample Input:
3 3 
3 3 
1 4 
1 5

Sample Output:


11

题意:一个人可以跑,可以走。

           走一次1米

           跑一次k米。

           不能连续跑2次

           问移动L,L+1,L+2,,,R米共有多少方法。

简单dp

dp[i][0]表示这一次走。dp[i][0]=dp[i-1][0]+dp[i-1][1];

dp[i][1]表示这一次跑。dp[i][1]=dp[i-k][0];

代码:
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
const int maxn = 1e5+5;
ll dp[maxn][2];
ll ans[maxn];
int main()
{
    int q, k;
    scanf("%d%d",&q,&k);
    memset(dp, 0, sizeof dp);
    dp[0][0] = 1;

    memset(ans, 0, sizeof ans);
    for (int i = 1; i < maxn; ++i)
    {
        dp[i][0]=(dp[i-1][0]+dp[i-1][1])%mod;
        if(i-k>= 0)
        {
            dp[i][1]=dp[i-k][0]%mod;
        }
        ans[i]=ans[i-1] + dp[i][0] + dp[i][1];
    }
    for (int i = 1, l, r; i <= q; ++i)
    {
        scanf("%d%d",&l,&r);
        printf("%lld\n", (ans[r] - ans[l - 1]) % mod);
    }
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/snayf/article/details/81161596
今日推荐