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

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

    什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角度类审 ...

  2. JavaScript中的with语句

    执行环境的类型有两种:全局执行环境和局部执行环境(函数执行环境). 1.全局执行环境的变量对象是window对象,是JS代码开始运行时的默认环境.全局执行环境的变量对象始终都是作用域链中的最后一个对象 ...

  3. Educational Codeforces Round 8 B. New Skateboard 暴力

    B. New Skateboard 题目连接: http://www.codeforces.com/contest/628/problem/A Description Max wants to buy ...

  4. ApplicationDelegate里的方法

    // 程序第一次加载完毕 - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictio ...

  5. Fatal error: Call to undefined function mb_detect_encoding()

    1.找到php扩展目录(我的php扩展目录的路径是:C:\Program Files\AppServ\php\extensions)     在extensions目录下面找到php_mbstring ...

  6. [标准性文档]WEB应用安全验证标准

    这是根据OWASP ASVS翻译过来的,可作为WEB应用开发的验收标准(未包含具体细节),值得注意的是,验收标准并非越严格越好,应根据企业自身的业务需求判定. WEB应用安全验证标准 安全验证等级的定 ...

  7. HDU 4635 Strongly connected (2013多校4 1004 有向图的强连通分量)

    Strongly connected Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. android源码包下载

    http://rgruet.free.fr/public/ 其他下载地址:http://cid-b50f9d5897331c44.office.live.com/browse.aspx/Android ...

  9. jenkins的slave报错误: org.owasp.dependencycheck.data.nvdcve.DatabaseException: Exception retrieving vulnerability for cpe:/a:mysql:mysql:5.1.26

    目前的临时解决办法是通过任务分配到master节点跑

  10. 创建maven web项目无法创建sec目录

    创建maven web项目无法创建sec目录 解决方法:-DarchetypeCatalog=internal