zoukankan      html  css  js  c++  java
  • Codeforces Round #271 (Div. 2) D Flowers【计数dp】

    D. Flowers
    time limit per test
    1.5 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several flowers, some of them white and some of them red.

    But, for a dinner to be tasty, there is a rule: Marmot wants to eat white flowers only in groups of size k.

    Now Marmot wonders in how many ways he can eat between a and b flowers. As the number of ways could be very large, print it modulo 1000000007 (109 + 7).

    Input

    Input contains several test cases.

    The first line contains two integers t and k (1 ≤ t, k ≤ 105), where t represents the number of test cases.

    The next t lines contain two integers ai and bi (1 ≤ ai ≤ bi ≤ 105), describing the i-th test.

    Output

    Print t lines to the standard output. The i-th line should contain the number of ways in which Marmot can eat between ai and bi flowers at dinner modulo 1000000007 (109 + 7).

    Examples
    input
    3 2
    1 3
    2 3
    4 4
    output
    6
    5
    5
    Note
    • For K = 2 and length 1 Marmot can eat (R).
    • For K = 2 and length 2 Marmot can eat (RR) and (WW).
    • For K = 2 and length 3 Marmot can eat (RRR), (RWW) and (WWR).
    • For K = 2 and length 4 Marmot can eat, for example, (WWWW) or (RWWR), but for example he can't eat (WWWR).

    思路:

    1、设定dp【i】表示长度为i的情况有多少合法放置方式。

    dp【i】=dp【i-1】+dp【i-k】;

    长度为i-1的时候,直接在其每个合法的放置方式的右边多加一个红色的花也都是合法的情况。

    长度为i-k的时候,直接在其每个合法的放置方式的右边多加k个连续白色的花也都是合法的情况。

    那么累加即可。

    2、那么答案就是sum【bi】-sum【ai】

    代码:

     1 #include<bits/stdc++.h>
     2 #define db double
     3 #include<vector>
     4 #define ll long long
     5 #define vec vector<ll>
     6 #define Mt  vector<vec>
     7 #define ci(x) scanf("%d",&x)
     8 #define cd(x) scanf("%lf",&x)
     9 #define cl(x) scanf("%lld",&x)
    10 #define pi(x) printf("%d
    ",x)
    11 #define pd(x) printf("%f
    ",x)
    12 #define pl(x) printf("%lld
    ",x)
    13 const int N = 1e5 + 5;
    14 const int mod = 1e9 + 7;
    15 const int MOD = mod-1;
    16 const db  eps = 1e-18;
    17 const db  PI = acos(-1.0);
    18 using namespace std;
    19 ll f[N],sum[N];
    20 int main()
    21 {
    22     int n,k;
    23     ci(n),ci(k);
    24     memset(f,0,sizeof(f));
    25     memset(sum,0,sizeof(sum));
    26     f[0]=1;
    27     for(int i=1;i<=100000;i++){
    28         if(i>=k) f[i]=(f[i-1]+f[i-k])%mod;
    29         else f[i]=f[i-1]%mod;
    30         sum[i]=(sum[i-1]+f[i])%mod;
    31     }
    32     for(int i=0;i<n;i++){
    33         int l,r;
    34         ci(l),ci(r);
    35         ll ans=(sum[r]-sum[l-1]+mod)%mod;
    36         pl(ans);
    37     }
    38     return 0;
    39 }
  • 相关阅读:
    块存储、文件存储、对象存储的区别
    brk 和 sbrk 区别
    【转】海量数据处理:十道面试题与十个海量数据处理方法总结
    C++ STL 里为什么不维护一个 size 成员变量?
    C++对象内存分布详解(包括字节对齐和虚函数表)
    虚函数实现原理
    关于Ubuntu中Could not get lock /var/lib/dpkg/lock解决方案
    Jerasure 1.2A 中的 C 函数 tips
    C++ qsort() 函数调用时实参与形参不兼容的问题解决
    C语言位运算符:与、或、异或、取反、左移与右移详细介绍
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/7772167.html
Copyright © 2011-2022 走看看