zoukankan      html  css  js  c++  java
  • 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,1<=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]表示到达走着到达i
    dp[i][1]表示跑着到达i
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 using namespace std;
     5 typedef long long LL;
     6 const int maxn = 1e5 + 10;
     7 const int mod = 1e9 + 7;
     8 int q, k, dp[maxn][2], l, r;
     9 int main() {
    10     while(scanf("%d%d", &q, &k) != EOF) {
    11         dp[0][0] = 0;
    12         for (int i = 1 ; i < maxn ; i++) {
    13             if (i < k) dp[i][0] = dp[i - 1][0] + 1;
    14             else {
    15                 dp[i][0] = (dp[i - 1][0]+dp[i-1][1] + 1) % mod;
    16                 dp[i][1] = (dp[i - k][0] + 1) % mod;
    17             }
    18         }
    19         for (int i = 0 ; i < q ; i++) {
    20             scanf("%d%d", &l, &r);
    21             int ans = ((dp[r][0] + dp[r][1]) - (dp[l - 1][0] + dp[l - 1][1])) % mod;
    22             printf("%d
    ", ans);
    23         }
    24     }
    25     return 0;
    26 }


  • 相关阅读:
    ASP.NET
    JS、JQury
    ASP.NET
    ASP.NET
    ASP.NET
    C#
    C#
    C#
    基础算法-树:预备知识
    开发环境配置--Ubuntu+Qt4+OpenCV(二)
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9346289.html
Copyright © 2011-2022 走看看