Codeforces Round #271 (Div. 2) D.Flowers DP
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 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.
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).
3 2
1 3
2 3
4 4
6
5
5
- 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).
题意: 有个人喜欢吃花, 有白花,红花两种花, 但是吃白色只能一次性吃K 朵或者不吃白花,现在问你吃n朵花的方案数是多少.
题解: dp[i]表示吃i朵的方案数,那么 dp[i+k]+=dp[i]用来更新 吃一次白花,dp[i+1]+=dp[i]用来更新吃一次红花
///
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define INF 0x7fffffff
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//****************************************
#define maxn 100000+5
#define mod 1000000007
int dp[maxn];
int main(){
int n=read(),k=read();
mem(dp);
dp[]=;
for(int i=;i<=;i++){
if(i+<=)
dp[i+]=(dp[i+]+dp[i])%mod;
if(i+k<=)
dp[i+k]=(dp[i+k]+dp[i])%mod;
}
ll sum[maxn];
mem(sum);
sum[]=;
for(int i=;i<=;i++){
sum[i]+=sum[i-]+dp[i];
}
int a,b;
for(int i=;i<=n;i++){
scanf("%d%d",&a,&b);
cout<<(sum[b]-sum[a-])%mod<<endl;
}
return ;
}
代码
Codeforces Round #271 (Div. 2) D.Flowers DP的更多相关文章
- 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 (递推)
题目链接:http://codeforces.com/problemset/problem/474/D 用RW组成字符串,要求w的个数要k个连续出现,R任意,问字符串长度为[a, b]时,字符串的种类 ...
- Codeforces Round #271 (Div. 2) D 简单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 (递推 预处理)
We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all kn ...
- Codeforces Round #271 (Div. 2)题解【ABCDEF】
Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...
- Codeforces Round #271 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/474 A题:Keyboard 模拟水题. 代码例如以下: #include <iostream> #include ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #271 (Div. 2) E. Pillars 线段树优化dp
E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #131 (Div. 1) B. Numbers dp
题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...
随机推荐
- HDU_1011_Starship Troopers_树型dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 Starship Troopers Time Limit: 10000/5000 MS (Jav ...
- codeforces_300C_组合数_快速幂
C. Beautiful Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- tee命令用法
用途说明 在执行Linux命令时,我们可以把输出重定向到文件中,比如 ls >a.txt,这时我们就不能看到输出了,如果我们既想把输出保存到文件中,又想在屏幕上看到输出内容,就可以使用tee命令 ...
- CAD使用DeleteXData删除数据(网页版)
主要用到函数说明: MxDrawEntity::DeleteXData 删除扩展数据,详细说明如下: 参数 说明 pzsAppName 删除的扩展数据名称,如果为空,删除所有扩展数据 js代码实现如下 ...
- Vue.js 观察者(watch)
Vue.js 观察者(watch) watch 属性用于监视 vue 实例上的数据变动,并相应的改变其他变量的值. 用法 实例 1 <!DOCTYPE html> <html> ...
- [USACO06JAN] 牛的舞会 The Cow Prom
题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their ...
- docker 部署spring.boot项目【一】(引用外部配置文件)
上一篇随笔,nginx是启动运行在容器内,spring.boot的web项目是运行在宿主内,这一篇的目的,是把web项目也制作成镜像,然后在容器里启动. 文件目录结构如下: 主要文件结构说明:(1)b ...
- 在vmware中 centos7安装gooderp
环境为windows 10系统,vmware 12,centos 7.4.centos安装了gnome桌面,用里面的终端来安装,自带的firefox浏览器. 增加用户 首先要新建一个用户来管理good ...
- linux-vmstat-显示虚拟内存状态
推荐:更多linux 性能监测与优化 关注:linux命令大全 vmstat命令的含义为显示虚拟内存状态(“Viryual Memor Statics”),但是它可以报告关于进程.内存.I/O等系统整 ...
- Linux mpstat-显示各个可用CPU的状态
更多linux 性能监测与优化 关注:linux命令大全 mpstat命令指令主要用于多CPU环境下,它显示各个可用CPU的状态系你想.这些信息存放在/proc/stat文件中.在多CPUs系统里,其 ...