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

题意:  有个人喜欢吃花, 有白花,红花两种花,  但是吃白色只能一次性吃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的更多相关文章

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

  2. Codeforces Round #271 (Div. 2) D. Flowers (递推)

    题目链接:http://codeforces.com/problemset/problem/474/D 用RW组成字符串,要求w的个数要k个连续出现,R任意,问字符串长度为[a, b]时,字符串的种类 ...

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

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

  5. Codeforces Round #271 (Div. 2)题解【ABCDEF】

    Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...

  6. Codeforces Round #271 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/474 A题:Keyboard 模拟水题. 代码例如以下: #include <iostream> #include ...

  7. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

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

  9. Codeforces Round #131 (Div. 1) B. Numbers dp

    题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...

随机推荐

  1. DIV水平 垂直居中CSS

    /*实现一.原理:要让div等块级元素水平和垂直居中,必需知道该div等块级元素的宽度和高度,然后设置位置为绝对位置,距离页面窗口左边框和上边框的距离设置为50%,这个50%就是指页面窗口的宽度和高度 ...

  2. centos右上角wired图标消失有效解决方案

    最近在学习Linux配置nginx时,左上角的wired图标突然没了,很神奇.然后在网上按着很多博客说的去改,都没用,最后终于根据下面参考博客内的方案解决了问题,嘿嘿. mv /var/lib/Net ...

  3. 梦想CAD控件图层COM接口知识点

    梦想CAD控件图层COM接口知识点 一.新建图层 主要用到函数说明: _DMxDrawX::AddLayer 增加新的图层.详细说明如下: 参数 说明 BSTR pszName 图层名 c#中实现代码 ...

  4. 03C++基本数据类型

    基本数据类型 2.2.1整型数据 短整型(short int) 有符号短整型(signed short int) 无符号短整型(unsigned short int) 一般整型(int) 有符号一般整 ...

  5. 诊断:ORA-01919: role ‘PLUSTRACE’ does not exist

    如下错误 SQL> grant plustrace to scott; grant plustrace to scott * ERROR at line 1: ORA-01919: role ' ...

  6. fread快读+fwrite快速输出

    定义数组 char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf; 读入 #define getchar() (p1==p2&a ...

  7. Linux修改系统时间与时区

    GMT  (Greewich Mean Time) 格林威治标准时间:GMT是老的时间计量标准,根据地球的自转和公转来计算时间,也就是太阳每天经过位于英国伦敦郊区的皇家格林尼治天文台的标准时间就是中午 ...

  8. Linux命令学习(2): scp和rsync基本用法与断点续传

    版权声明:本文为博主原创文章,未经允许不得转载. 引子 在平常的工作中,我经常需要在远程服务器和本地之间传输文件. 以前我都使用scp命令,直到今天因为网络中断,scp出现了stalled. 因为上传 ...

  9. Literature Review on Tidal Turbine

    source: scopus , SCIE keywords in tilte: tidal turbine Software: Bibliometrix

  10. SQL学习笔记:一些高级语句

    现在以MySQL为模板.学习的方法和别的数据库写法上会有不同,但是思路基本一致. 用到的数据库表的格式: +----+--------------+-------------------------- ...