Codeforces Round #271 (Div. 2) D. Flowers (递推 预处理)
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 sizek.
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 modulo1000000007 (109 + 7).
Input contains several test cases.
The first line contains two integers t andk (1 ≤ t, k ≤ 105),
wheret represents the number of test cases.
The next t lines contain two integers
ai andbi (1 ≤ ai ≤ bi ≤ 105),
describing the i-th test.
Print t lines to the standard output. Thei-th line should contain the number of ways in which Marmot can eat betweenai
andbi flowers at dinner modulo1000000007 (109 + 7).
3 2
1 3
2 3
4 4
6
5
5
- For K = 2 and length1 Marmot can eat (R).
- For K = 2 and length2 Marmot can eat (RR) and (WW).
- For K = 2 and length3 Marmot can eat (RRR), (RWW) and (WWR).
- For K = 2 and length4 Marmot can eat, for example, (WWWW) or (RWWR), but for
example he can't eat (WWWR).
考虑第n个。假如n是小于k的,那么仅仅能都是是R,也就是仅仅有一种情况。
假如大于等于k。假设第n个是W,那么从n-k+1到n所有为W,假设第n个是R,那么数量就是前n-1个的数量。
dp[n] = 1; (0<= n < k)
dp[n] = dp[n-1] + dp[n-k]; (n >= k)
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cmath>
#define mem(f) memset(f,0,sizeof(f))
#define M 100005
#define mod 1000000007
#define MAX 0X7FFFFFFF
#define maxn 100005
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using namespace std;
typedef long long LL; int n = maxn, k, t, a, b, dp[maxn], sum[maxn]; int main()
{
scanf("%d%d", &t, &k);
for(int i = 0; i < k; i++) dp[i] = 1;
for(int i = k; i < n; i++) dp[i] = (dp[i-1] + dp[i-k])%mod;
for(int i = 1; i < n; i++) sum[i] = (sum[i-1] + dp[i])%mod;
while(t--) {
scanf("%d%d", &a, &b);
printf("%d\n", ((sum[b]-sum[a-1])%mod+mod)%mod );
}
return 0;
}
Codeforces Round #271 (Div. 2) D. Flowers (递推 预处理)的更多相关文章
- Codeforces Round #271 (Div. 2) D. Flowers (递推)
题目链接:http://codeforces.com/problemset/problem/474/D 用RW组成字符串,要求w的个数要k个连续出现,R任意,问字符串长度为[a, b]时,字符串的种类 ...
- Codeforces Round #671 (Div. 2) B. Stairs (递推)
题意:一个台阶由一些单元格组成,如果一个高度为\(n\)的台阶中有\(n\)个不相邻的正方形(如图中的样例),就称这个台阶是"好台阶",现给你\(x\)个单元格,问最多能组成多少个 ...
- Codeforces Round #271 (Div. 2) D Flowers【计数dp】
D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #271 (Div. 2) D.Flowers DP
D. Flowers We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, ...
- Codeforces Round #271 (Div. 2)题解【ABCDEF】
Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...
- Codeforces Round #271 (Div. 2)D(递推,前缀和)
很简单的递推题.d[n]=d[n-1]+d[n-k] 注意每次输入a和b时,如果每次都累加,就做了很多重复性工作,会超时. 所以用预处理前缀和来解决重复累加问题. 最后一个细节坑了我多次: print ...
- Codeforces Round #271 (Div. 2) F ,E, D, C, B, A
前言:最近被线段树+简单递推DP虐的体无完肤!真是弱! A:简单题,照着模拟就可以,题目还特意说不用处理边界 B:二分查找即可,用lower_lound()函数很好用 #include<stri ...
- Codeforces Round #271 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/474 A题:Keyboard 模拟水题. 代码例如以下: #include <iostream> #include ...
- Codeforces Round #271 (Div. 2)
A. Keyboard 题意:一个人打字,可能会左偏一位,可能会右偏一位,给出一串字符,求它本来的串 和紫书的破损的键盘一样 #include<iostream> #include< ...
随机推荐
- bat批处理如何删除本地策略里的用户权限分配中的拒绝从网络访问本机项的guest用户?
echo [Version]>mm.inf echo signature="$CHICAGO$">>mm.inf echo Revision=1>>m ...
- python 读取文件生成嵌套列表
def read_data(file_name): if not re.findall(".txt", file_name): file_name += ".txt&qu ...
- vue -vantUI tab切换时 list组件不触发load事件解决办法
最近由于公司项目需要,用vue写了几个简单的页面.用到了vantUI List 列表 瀑布流滚动加载,用于控制长列表的展示 当列表即将滚动到底部时,会触发事件并加载更多列表项. (页面加载完成后默认会 ...
- [Luogu] P4910 帕秋莉的手环
题目背景 帕秋莉是蕾米莉亚很早结识的朋友,现在住在红魔馆地下的大图书馆里.不仅擅长许多魔法,还每天都会开发出新的魔法.只是身体比较弱,因为哮喘,会在咏唱符卡时遇到麻烦. 她所用的属性魔法,主要是生命和 ...
- Kafka Broker配置
Kafka发行包里自带的配置样本可以用来安装单机服务,但并不能满足大多数安装场景的要求.kafka有很多配置选项,Kafka有很多配置选项,涉及安装和调优的方方面面.不过大多数调优选项可以使用默认配置 ...
- Linux环境搭建SVN服务
一.准备工作 首先要保证自己的linux电脑能上外网,我这电脑因为是新的,没有配置网络,所以再这块花费了点时间. 1.检查ip信息 >ifconfig 2.检查DNS服务器 >cat / ...
- 零基础入门学习Python(26)--字典:当索引不好用时2
知识点 删除字典元素 能删单一的元素也能清空字典,清空只需一项操作. 显示删除一个字典用del命令,如下: >>> dict1 = {'a':1,'b':2,'c':3} >& ...
- Ajax实现跨域访问最新方式
在实际项目当中,我们经常会遇到同一个域名下不同项目之间通过Ajax相互调用数据,这样问题就来了,如何通过Ajax实现跨域呢? 解决方案 1.Jsonp Jsonp解决跨域相对简单,服务器无需任何配置. ...
- Python之元祖
Python之元祖 tuple ( ) 元组和列表是一样的.但是,也有区别. 元组只能有读操作.没有修改删除操作. 列表是用中括号 [ ] 表示. 元组是用小括号 ( ) 表示. dir() 把传入的 ...
- UVa 712 S-Trees(二进制转换 二叉树叶子)
题意: 给定一颗n层的二叉树的叶子, 然后给定每层走的方向, 0代表左边, 1代表右边, 求到达的是那一个叶子. 每层有一个编号, 然后n层的编号是打乱的, 但是给的顺序是从1到n. 分析: 先用一个 ...