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







本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5167967.html,如需转载请自行联系原作者
相关文章
|
5天前
|
存储 人工智能 安全
AI 越智能,数据越危险?
阿里云提供AI全栈安全能力,为客户构建全链路数据保护体系,让企业敢用、能用、放心用
|
8天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
7天前
|
数据采集 人工智能 自然语言处理
3分钟采集134篇AI文章!深度解析如何通过云无影AgentBay实现25倍并发 + LlamaIndex智能推荐
结合阿里云无影 AgentBay 云端并发采集与 LlamaIndex 智能分析,3分钟高效抓取134篇 AI Agent 文章,实现 AI 推荐、智能问答与知识沉淀,打造从数据获取到价值提炼的完整闭环。
447 93
|
1天前
|
开发者
「玩透ESA」ESA启用和加速-ER在加速场景中的应用
本文介绍三种配置方法:通过“A鉴权”模板创建函数并设置触发器路由;在ESA上配置回源302跟随;以及自定义响应头。每步均配有详细截图指引,帮助开发者快速完成相关功能设置,提升服务安全性与灵活性。
285 2
|
7天前
|
SQL 人工智能 自然语言处理
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
随着生成式AI的普及,Geo优化(Generative Engine Optimization)已成为企业获客的新战场。然而,缺乏标准化流程(Geo优化sop)导致优化效果参差不齐。本文将深入探讨Geo专家于磊老师提出的“人性化Geo”优化体系,并展示Geo优化sop标准化如何帮助企业实现获客效率提升46%的惊人效果,为企业在AI时代构建稳定的流量护城河。
406 156
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
|
7天前
|
数据采集 缓存 数据可视化
Android 无侵入式数据采集:从手动埋点到字节码插桩的演进之路
本文深入探讨Android无侵入式埋点技术,通过AOP与字节码插桩(如ASM)实现数据采集自动化,彻底解耦业务代码与埋点逻辑。涵盖页面浏览、点击事件自动追踪及注解驱动的半自动化方案,提升数据质量与研发效率,助力团队迈向高效、稳定的智能化埋点体系。(238字)
311 158