Codeforces 474D Flowers 动态规划法
话说好久没写算法代码了,工作了有点忙的了。只是算法始终是我的挚爱,故此还是尽量抽时间和挚爱来个约会。
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 动态规划法的更多相关文章
- Codeforces 474D Flowers (线性dp 找规律)
D. Flowers time limit per test:1.5 seconds memory limit per test:256 megabytes We saw the little gam ...
- codeforces 474D.Flowers 解题报告
题目链接:http://codeforces.com/problemset/problem/474/D 题目意思:Marmot 吃两种类型的花(实在难以置信呀--):red 或者 white,如果要吃 ...
- Codeforces 474D Flowers
http://codeforces.com/problemset/problem/474/D 思路:F[i]=F[i-1]+(i>=K)F[i-k] #include<cstdio> ...
- Codeforces - 474D - Flowers - 构造 - 简单dp
https://codeforces.com/problemset/problem/474/D 这道题挺好的,思路是这样. 我们要找一个01串,其中0的段要被划分为若干个连续k的0. 我们设想一个长度 ...
- Codeforces 474D Flowers(DP)
题目链接 非常简单的一道dp题,通过O(n)的预处理来使查询变为O(1). 主要的坑在于取模后的dp数组的前缀和再相减可能得到负数,导致无法得到某一区间和的取模. 解决方法:(a-b)%mo==(a% ...
- Codeforces 474D Flowers dp(水
题目链接:点击打开链接 思路: 给定T k表示T组測试数据 每组case [l,r] 有2种物品a b.b物品必须k个连续出现 问摆成一排后物品长度在[l,r]之间的方法数 思路: dp[i] = d ...
- 【Codeforces 474D】Flowers
[链接] 我是链接,点我呀:) [题意] 让你吃东西 B食物一次必须要吃连续k个 但是对A食物没有要求 问你有多少种吃n个食物的方法(吃的序列) [题解] 设f[i]表示长度为i的吃的序列且符合要求的 ...
- CodeForces - 474D (dp)
题目:https://vjudge.net/contest/326867#problem/B 题意:有很多个蛋糕,现在你有两种吃蛋糕的吃法,一次吃一个,定为A,一次吃k个定为B,然后问你吃m个蛋糕有多 ...
- codeforces474D
Flowers CodeForces - 474D 话说某个幸运的小伙伴X拿到了kevin女神送的蛋糕,然而他的吃法非常奇特,他独创了两种吃蛋糕的办法:一.一次吃一整个蛋糕:二.一次吃k个蛋糕. 那么 ...
随机推荐
- codevs 1349 板猪的火车票
1349 板猪的火车票 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 奸商zn(请勿对号入座)开办了一家火车公司,弱弱的板猪 ...
- 几本推荐的Java书
一.<深入理解Java虚拟机:JVM高级特性与最佳实践> 如果你不满足于做一个只会写if…else…的Java程序员,而是希望更进一步,我随便举几个例子吧: 1.了解Java代码的底层运行 ...
- SQL Server 2008数据库备份和还原(还原是必须有完整备份)
转自lwccc, SQLserver2008数据库备份和还原问题(还原是必须有完整备份) 首先,我要说明的是你必须拥有完整的数据库备份,下面的还原教程,才算有用. 这个连接是某高手的异常恢复方法, 实 ...
- jstack_查看当前进程及所属线程执行情况
C:\Program Files\Java\jdk1.7.0_45\bin\jstack.exe 1.任务管理器:进程选择PID 2.命令行执行 cd C:\Program Fi ...
- iOS:quartz2D绘图(显示绘制在PDF上的图片)
quart2D既可以用来绘制图像到pdf上,也可以从pdf上读取图像并显示出来.在使用这种方式之前,还有一种方式可以用来读取显示pdf上的图像,即使用UIWebView网页视图控件- (void)lo ...
- Linux内核开发者峰会照的全家福
刚才看到一张Linux内核开发者峰会照的全家福,有历史价值,给大家分享一下.上面有Torvalds(大致在中间).Andrew Morton(目前的内核主要维护者,第二排右数第二个).Alan Cox ...
- lumisoft邮件头不规范造成内容无法读取
解决邮件内容为multipart,并且Param_Boundary等于null的不规范邮件内容无法读取问题,这样的邮件内容头部往往带两个或多个ContentType. 红色为自己加的,绿色为注释掉原来 ...
- 深度增强学习--DDPG
DDPG DDPG介绍2 ddpg输出的不是行为的概率, 而是具体的行为, 用于连续动作 (continuous action) 的预测 公式推导 推导 代码实现的gym的pendulum游戏,这个游 ...
- http 使用curl发起https请求报错的解决办法
使用curl发起https请求的时候报错:“SSL certificate problem, verify that the CA cert is OK. Details: error:1409008 ...
- git 超前一个版本 落后一个版本的解决方案
在使用SourceTree的时候经常会遇见超前一个版本,落后N个版本的情况,遇见这种情况应该怎么办呢? 首先打开终端,最好是从SourceTree里面打开,菜单栏有个终端按钮. 然后输入: $ git ...