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 ...
随机推荐
- LR中日志参数的设置
LR中日志参数的设置 1.Run-Time Setting日志参数的设置 在loadrunner的vuser菜单下的Run-Time Setting的General的LOG选项中可以对在执行脚本时Lo ...
- golang 自定义time.Time json输出格式
工作中使用golang时,遇到了一个问题.声明的struct含time.Time类型.使用json格式化struct时,time.Time被格式化成”2006-01-02T15:04:05.99999 ...
- 并发编程学习笔记(6)----公平锁和ReentrantReadWriteLock使用及原理
(一)公平锁 1.什么是公平锁? 公平锁指的是在某个线程释放锁之后,等待的线程获取锁的策略是以请求获取锁的时间为标准的,即使先请求获取锁的线程先拿到锁. 2.在java中的实现? 在java的并发包中 ...
- 第二节:SQLServer导出-重置sa密码-常用sql语句
1.SQLServer导出: 点击要导出数据库----->右键(任务)----->生成脚本----->下一步----->下一步(高级)要编写脚本的数据类型---选择架构和数据 ...
- A3. JVM 类加载器
[概述] 虚拟机设计团队把类加载阶段中的 “通过一个类的全限定名来获取描述此类的二进制字节流” 这个动作放到 Java 虚拟机外部去实现,以便让应用程序自己决定如何去获取所需要的类.实现这个动作的代码 ...
- 出生年 (15 分) C解法
出生年 以上是新浪微博中一奇葩贴:"我出生于1988年,直到25岁才遇到4个数字都不相同的年份."也就是说,直到2013年才达到"4个数字都不相同"的要求.本题 ...
- Java 中 break和 continue 的使用方法及区别
break break可用于循环和switch...case...语句中. 用于switch...case中: 执行完满足case条件的内容内后结束switch,不执行下面的语句. eg: publi ...
- (二)Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用
#!usr/bin/env python # -*- coding: utf-8 -*- def test(): print('hello, world') if __name__ == " ...
- power coefficient calculation -- post processing
input: unscaled moment of one bladeoutput: power coefficient of a 3-blades wind/tidal turbine matlab ...
- MySQL 乐观锁和悲观锁
前言 1)在数据库的锁机制中介绍过,数据库管理系统(DBMS)中的并发控制的任务是确保在多个事务同时存取数据库中同一数据时不破坏事务的隔离性和一致性以及数据库的一致性. 2)加锁是为了解决更新丢失问题 ...