zoukankan      html  css  js  c++  java
  • Codeforces Round #271 (Div. 2)

    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 modulo1000000007 (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).

    Sample test(s)
    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).

    我理解题目的能力实在太差了,一个小时都没看懂题目.....真不知道我四级是怎么过的....

    题意:

    一个由‘R’和‘W’构成的字符串中,w仅仅能以连续k个存在。比如k=2。长度为4合法的字符串仅仅有 RRWW   WWRR   WWWW RWWR RRRR,给出两个长度值a,b,问长度为[a,b]的串中合法的字符串有多少个。dp[i]第i位有多少种字符串,当第i位选R时则dp[i]=dp[i-1],当第i位选W时则dp[i]=dp[i-k],所以转移方程为dp[i]=dp[i-1]+dp[i-k]

    感悟:

    尽管这个dp不难,可是还是从不断WA,不断TLE中学到了非常多,预处理使得o(n^2)算法降为o(n),主要是查询的时候变为o(1)!

    先附上TLE代码:

            int t,k;
            cin>>t>>k;
            dp[0] = 1;
            for(int i =1; i <= 100000+5; i++)
            {
                if(i < k)  {
                    dp[i] = 1;
                    sum[i] = sum[i-1] +1;
                }
                else if(i == k)
                    dp[k] = 2;
                else dp[i] = (dp[i-1] + dp[i - k])%mod;
            }
    
            while(t--)
            {
                 int a,b;
                 //cin>>a>>b;
                 scanf("%d%d",&a,&b);
                 LL sum = 0;
                 for(int i = a; i <= b; i++)//这里最多会取t*(a-b)次模,相当于n^2,10^10,居然没注意这是个o(n^2)的算法,我也是醉了QAQ
                 {
                     sum += dp[i];
                     sum %= mod;
                 }
                cout<<sum<<endl;
    
            }
    AC代码

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include <algorithm>
    #define LL  long long
    const int mod = 1e9+7;
    const int  maxn = 2000000+10;
    using namespace std;
    LL dp[maxn];
    LL sum[maxn];
    int main()
    {
    
            int t,k;
            cin>>t>>k;
            dp[0] = 1;
            sum[0] = 0;
            for(int i = 1; i < k; i++)
            {
                dp[i] = 1;
                sum[i] = sum[i-1] + dp[i];
            }
            for(int i =k; i <= 100000+5; i++)
            {
                        dp[i] = (dp[i-1] + dp[i - k])%mod;
                        sum[i] = sum[i-1] + dp[i];
                        sum[i] %= mod;
            }
            while(t--)
            {
                 int a,b;
               cin>>a>>b;
              cout<<(sum[b] - sum[a-1]+mod)%mod<<endl;//注意这里我也WA了好几发,sum[b]-sum[a-1]有可能是负数,由于一開始取模了有肯能取摸 后sum[b]<sum[a-1],比方b = 10006,a = 1005,b%10006 < a%10006,所以要加个mod
    
            }
        return 0;
    }
    

    从这到简单的dp涨了不少姿势,233333



  • 相关阅读:
    Content delivery network
    散列算法的基础原理 确保资料传递无误
    科学计算 NumPy 与C语言对比 N-dimensional array ndarray 元素元素操作 计算正太分布分位数 ndarray中的所有元素的类型都是相同的,而Python列表中的元素类型是任意的,所以ndarray在存储元素时内存可以连续,而python原生list就只能通过寻址方式找到下一个元素
    t
    百度 url 当在baidu搜索结果展示页,去点击标头时
    指定文件夹 指定文件后缀名 删除整个文件夹 git 冲突解决 create a new repository on the command line push an existing repository from the command line rebase
    修改MojoWeixin 只保留用户name 取消群昵称
    AnyEvent::HTTP 介绍
    AnyEvent::HTTP 介绍
    异步和同步http请求超时机制
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5167967.html
Copyright © 2011-2022 走看看