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

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值

上代码:

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main()
  5. {
  6. const int MOD = (int)1e9 + 7;
  7. const int MAX_M = (int)1e5 + 1;
  8. int t, k;
  9. scanf("%d %d", &t, &k);
  10. int tbl[MAX_M] = {};
  11. for (int i = 0; i < k; i++)
  12. {
  13. tbl[i] = 1;
  14. }
  15. for (int i = k; i < MAX_M; i++)
  16. {
  17. tbl[i] = tbl[i - 1] + tbl[i - k];
  18. tbl[i] %= MOD;
  19. }
  20. int sum[MAX_M] = {};
  21. sum[0] = 0;
  22. for (int i = 1; i < MAX_M; i++)
  23. {
  24. sum[i] = sum[i - 1] + tbl[i];
  25. sum[i] %= MOD;
  26. }
  27. int a, b;
  28. for (int i = 0; i < t; i++)
  29. {
  30. scanf("%d %d", &a, &b);
  31. printf("%d\n", (sum[b] - sum[a - 1] + MOD) % MOD);
  32. }
  33. return 0;
  34. }

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. Spring中与Spring相关的注解

    # 一.Spring的常用组件类注解 ## @Component     被该注解所修饰的类是一个普通的spring bean类,该注解可以替代@Controller.@Service.@Reposi ...

  2. cookie实现记住密码功能

    之前在一次面试过程中,被问到前后端如何实现记住密码功能,所以自己也试着做,这是纯js做的. 一.实现思路 1.前端页面,自己简单写了一个页面,如下图,不喜勿喷 2.主要有三个函数 setCookie( ...

  3. js的执行环境及作用域

    主要的是执行环境和作用域链. 执行环境 执行环境定义了变量或函数有权访问的其他数据,每个函数都有一个执行环境,每个执行环境都有一个与之关联的变量对象,环境中定义的所有变量和函数都保存在这个变量对象中, ...

  4. Linux 的硬链接与软链接

    Linux 的硬链接与软链接    http://www.ibm.com/developerworks/cn/linux/l-cn-hardandsymb-links/    若一个 inode 号对 ...

  5. mybatis批量操作-xml方式

    在实际项目中,我们一般都会用到批量insert.delete.update等操作,由于使用频率还是蛮高的,这里就做个简单的记录,供以后学习和参考. 批量insert 在数据库中,批量插入可以是多条in ...

  6. MyBatis 显示日志

    <!-- 全局配置 --> <settings> <setting name="cacheEnabled" value="false&quo ...

  7. ylbtech-LanguageSamples-OperatorOverLoading(运算符重载)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-OperatorOverLoading(运算符重载) 1.A,示例(Sample) 返回 ...

  8. TensorFlow------TFRecords的分析与存储实例

    TensorFlow------TFRecords的分析与存储实例: import os import tensorflow as tf # 定义cifar的数据等命令行参数 FLAGS = tf.a ...

  9. a<<=n

    a<<=n等价于a=a<<na<<n表示a左移n位(二进制)等价于a乘以2的n次方 a<<=n的含义就是,a等于a乘以2的n次方

  10. mac osx加入全局启动terminal快捷键

    尽管有非常多第三方工具(Alfred.keyboad Maestro)能够设置全局启动terminal快捷键,但怎么感觉都不如native的好,呵呵.本文就使用mac 自带的Automator来创建一 ...