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

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. React事件系统入门

    React基于虚拟DOM实现了一个合成事件层,我们所定义的事件处理器会接受到一个合成事件层对象的实例,它完全符合W3C标准,不会存在任何IE标准的兼容性问题.并且和原生的浏览器事件一样拥有同样的接口, ...

  2. (Mark=转)ehcache memcache redis

    Ehcache 在java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS中取出来的高花费.高延迟采取的一种缓存方案.正因为Ehcache具有健壮性(基于java开发).被认证(具有apa ...

  3. devfs、sysfs、udev介绍

    转:http://www.360doc.com/content/11/1203/09/7378000_169310928.shtml 一.devfs linux下有专门的文件系统用来对设备进行管理,d ...

  4. [转] Moran's I

    李旭, Matlab: Moran's I原文地址 Introduction In statistics, Moran's I is a measure of spatial autocorrelat ...

  5. wince5.0 plat form builder下载

    学习嵌入式开发的朋友都知道,嵌入式开发主要用到2个软件一个是Platform Builder for Windows CE 5.0一个是VS.NET2005,其中VS.NET2005网上很容易下载,现 ...

  6. 从Cell的视图推出一个新的界面

    先写一个方法, 强制增加一个navigation的属性. 这样self就可以调出来navigation了 - (UINavigationController*)naviController { for ...

  7. jquery next()方法

    1.html代码 <!DOCTYPE html> <html> <head> <script type="text/javascript" ...

  8. [Android Pro] Android学习——在线查看android源代码的3种方式

    原文:http://blog.csdn.net/chuekup/article/details/8067075 1. https://github.com/android 2. http://grep ...

  9. 关于Java设计模式的一些概况

    设计模式(Design pattern)在软件行业一直都扮演着很重要的角色.最近感觉自己对设计模式的知识有些遗忘了,虽然以前也看了很多,但是坦白说,其实并没有怎么理解.基本还是为了应付面试.然后,在工 ...

  10. attribute用法

    attribute 用法 摘要: 在学习linux内核代码及一些开源软件的源码(如:DirectFB),经常可以看到有关__attribute__的相关使用.本文结合自己的学习经历,较为详细的介绍了_ ...