话说好久没写算法代码了,工作了有点忙的了。只是算法始终是我的挚爱,故此还是尽量抽时间和挚爱来个约会。

Codeforces的题目是最适合练手的了,以下是一道不算难的动态规划法题目。先上题:

D. Flowers

time limit per test1.5 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard 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).

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

算法思路:

1 逐步添加计算的,那么就是逐步填表;

2 设tbl[]为动态规划的表格。 那么每新增第i个表格的时候。新格子仅仅能填写R或者W。填写R的时候就相当于是有tbl[i-1]种填法,由于最后一个格子定了,仅仅能填写R,假设填写W的时候呢?那就是有tbl[i-k]中写法了,由于依据题意。填写了W之后,i个格子之前的全部k个格子都仅仅能填写W,由于W要么不出现,要么就仅仅能k个W连着出现。

故此终于得到的状态转换公式:

tbl[i] = tbl[i-1] + tbl[i-k];

3 可是本题目略微进了一步,由于须要求区间的和值,哇。一看好像要使用线段树的节奏,事实上不须要使用线段树,仅仅须要使用个小算法就能够。高手嘛。小算法自然顺手粘来,仅仅要添加一个sum[]数组,然后计算sum[i] = tbl[0 – i]的和就能够使用sum[i] - sum[j-1]计算[j, i]之间的和值啦。当然计算sum[i] = tbl[0 – i]也是有点技巧的,看代码就知道了。

4 最后还添加点难度,就是会溢出,只是题目要求了求1e9 +7的Mod值

上代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h> int main()
{
const int MOD = (int)1e9 + 7;
const int MAX_M = (int)1e5 + 1;
int t, k;
scanf("%d %d", &t, &k); int tbl[MAX_M] = {};
for (int i = 0; i < k; i++)
{
tbl[i] = 1;
} for (int i = k; i < MAX_M; i++)
{
tbl[i] = tbl[i - 1] + tbl[i - k];
tbl[i] %= MOD;
} int sum[MAX_M] = {};
sum[0] = 0;
for (int i = 1; i < MAX_M; i++)
{
sum[i] = sum[i - 1] + tbl[i];
sum[i] %= MOD;
} int a, b;
for (int i = 0; i < t; i++)
{
scanf("%d %d", &a, &b); printf("%d\n", (sum[b] - sum[a - 1] + MOD) % MOD);
} return 0;
}

Codeforces 474D Flowers 动态规划法的更多相关文章

  1. Codeforces 474D Flowers (线性dp 找规律)

    D. Flowers time limit per test:1.5 seconds memory limit per test:256 megabytes We saw the little gam ...

  2. codeforces 474D.Flowers 解题报告

    题目链接:http://codeforces.com/problemset/problem/474/D 题目意思:Marmot 吃两种类型的花(实在难以置信呀--):red 或者 white,如果要吃 ...

  3. Codeforces 474D Flowers

    http://codeforces.com/problemset/problem/474/D 思路:F[i]=F[i-1]+(i>=K)F[i-k] #include<cstdio> ...

  4. Codeforces - 474D - Flowers - 构造 - 简单dp

    https://codeforces.com/problemset/problem/474/D 这道题挺好的,思路是这样. 我们要找一个01串,其中0的段要被划分为若干个连续k的0. 我们设想一个长度 ...

  5. Codeforces 474D Flowers(DP)

    题目链接 非常简单的一道dp题,通过O(n)的预处理来使查询变为O(1). 主要的坑在于取模后的dp数组的前缀和再相减可能得到负数,导致无法得到某一区间和的取模. 解决方法:(a-b)%mo==(a% ...

  6. Codeforces 474D Flowers dp(水

    题目链接:点击打开链接 思路: 给定T k表示T组測试数据 每组case [l,r] 有2种物品a b.b物品必须k个连续出现 问摆成一排后物品长度在[l,r]之间的方法数 思路: dp[i] = d ...

  7. 【Codeforces 474D】Flowers

    [链接] 我是链接,点我呀:) [题意] 让你吃东西 B食物一次必须要吃连续k个 但是对A食物没有要求 问你有多少种吃n个食物的方法(吃的序列) [题解] 设f[i]表示长度为i的吃的序列且符合要求的 ...

  8. CodeForces - 474D (dp)

    题目:https://vjudge.net/contest/326867#problem/B 题意:有很多个蛋糕,现在你有两种吃蛋糕的吃法,一次吃一个,定为A,一次吃k个定为B,然后问你吃m个蛋糕有多 ...

  9. codeforces474D

    Flowers CodeForces - 474D 话说某个幸运的小伙伴X拿到了kevin女神送的蛋糕,然而他的吃法非常奇特,他独创了两种吃蛋糕的办法:一.一次吃一整个蛋糕:二.一次吃k个蛋糕. 那么 ...

随机推荐

  1. redis源码解析之dict数据结构

    dict 是redis中最重要的数据结构,存放结构体redisDb中. typedef struct dict { dictType *type; void *privdata; dictht ht[ ...

  2. [转]spring property标签中的 ref属性和ref 标签有什么不同

    spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" /> sp ...

  3. 使用织梦开源的分词算法库编写的YII获取分词扩展

    在编辑文章中,很多时候都需要自动根据文章内容获取关键字的功能,因此,本文主要是说明如何在yii中使用织梦开源的分词算法编写一个独立的扩展,可以在不同的模块中使用,步骤如下: 1 到这里下载其他朋友整理 ...

  4. iOS 在系统设置中展示Version, Build, Git等信息

     在设置中,展示自定义内容,如下图INFO区域内容:         步骤: 1.在项目中添加Settings.bundle文件 Root.plist和Root.plist的Source code如下 ...

  5. 在Linux系统环境下修改MySQL的root密码

    root用户登录系统 /usr/local/mysql/bin/mysqladmin -u root -p password 新密码 enter password 旧密码 第二种方法: root用户登 ...

  6. bash中的通配符使用

    通配符的使用 bash命令中的字符由:普通字符.通配符.元字符.转义符构成. 通配符 由shell处理的(不是由所涉及到命令语句处理的,其实我们在shell各个命令中也没有发现有这些通配符介绍), 它 ...

  7. 转载:ArcEngine二次开发界面基本设置

    转自:https://blog.csdn.net/weixin_42032107/article/details/80644991 1.   在form窗体中添加菜单栏和状态栏控件 2.   添加li ...

  8. UINavigationController 详解

    // 导航控制器 // 1. 比较常用的视图控制器管理类 // 2. 以栈的形式管理视图控制器, 先进后出 // 3. 创建navigation后, 视图控制器上会多出一个导航栏 // 4. 导航栏高 ...

  9. PTC问答

    1.什么是PTC点击网站? 答:PTC点击网站是一类网赚网站,通过点击广告来获取收益.当然,单纯通过点击广告获取的收益很少,甚至可以说可以忽略不计.如果单干不推广的话主要通过投资租赁下线来获得收益. ...

  10. asp.net <%%> <%#%><%=%><%@%><%$%>用法区别

    asp.net <%%>&<%#%>&<%=%>&<%@%>&<%$%>用法区别 1.<% %> ...