D. Flowers
time limit per test

1.5 seconds

memory limit per test

256 megabytes

input

standard input

output

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

Examples
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、设定dp【i】表示长度为i的情况有多少合法放置方式。

dp【i】=dp【i-1】+dp【i-k】;

长度为i-1的时候,直接在其每个合法的放置方式的右边多加一个红色的花也都是合法的情况。

长度为i-k的时候,直接在其每个合法的放置方式的右边多加k个连续白色的花也都是合法的情况。

那么累加即可。

2、那么答案就是sum【bi】-sum【ai】

代码:

 #include<bits/stdc++.h>
#define db double
#include<vector>
#define ll long long
#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
const int N = 1e5 + ;
const int mod = 1e9 + ;
const int MOD = mod-;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
ll f[N],sum[N];
int main()
{
int n,k;
ci(n),ci(k);
memset(f,,sizeof(f));
memset(sum,,sizeof(sum));
f[]=;
for(int i=;i<=;i++){
if(i>=k) f[i]=(f[i-]+f[i-k])%mod;
else f[i]=f[i-]%mod;
sum[i]=(sum[i-]+f[i])%mod;
}
for(int i=;i<n;i++){
int l,r;
ci(l),ci(r);
ll ans=(sum[r]-sum[l-]+mod)%mod;
pl(ans);
}
return ;
}

Codeforces Round #271 (Div. 2) D Flowers【计数dp】的更多相关文章

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

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

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

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

  4. Codeforces Round #518 (Div. 2) D(计数DP)

    #include<bits/stdc++.h>using namespace std;const long long mod=998244353;int n;int a[100007];l ...

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

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

  6. Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)

    题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...

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

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

  8. Codeforces Round #271 (Div. 2)

    A. Keyboard 题意:一个人打字,可能会左偏一位,可能会右偏一位,给出一串字符,求它本来的串 和紫书的破损的键盘一样 #include<iostream> #include< ...

  9. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

随机推荐

  1. ubuntu 无法应用原保存的显示器配置

    打开ubuntu之后的开启页面出现: 所选模式均不匹配可能的模式: 为 CRTC 63 尝试模式 CRTC 63:尝试 800x600@60Hz 模式输出在 1366x768@60Hz (通过 0) ...

  2. 程序员从初级到中级10个秘诀——摘自CSDN

    程序员从初级到中级10个秘诀 1.学习先进的搜索技术.手段和及策略 2.帮助别人 教别人始终是学习一切东西的最好方法之一.相对而言,由于你在开发领域还是个新手,认为自己没什么可教给人家的,这可以理解. ...

  3. Java实例学习——企业进销存管理系统(2)

    Java实例学习--企业进销存管理系统(2) (本实例为书上实例,我所记录的是我的学习过程) 开始时间:2月12日 完成时间:暂未完成 2月15日-系统登录 对于昨天新建的12个Java包不能完全显示 ...

  4. MySQL分库分表的技巧

    分表是分散数据库压力的好方法. 分表,最直白的意思,就是将一个表结构分为多个表,然后,可以再同一个库里,也可以放到不同的库. 当然,首先要知道什么情况下,才需要分表.个人觉得单表记录条数达到百万到千万 ...

  5. Spring cloud Eureka 服务治理(注册服务提供者)

    搭建完成服务注册中心,下一步可以创建服务提供者并向注册中心注册服务. 接下来我们创建Spring Boot 应用将其加入Eureka服务治理体系中去. 直接使用签名章节创建hello服务项目改造: 1 ...

  6. JQuery初识(三 )

    一丶JQuery的文档操作 1.插入操作: 父元素.append(子元素) 解释:追加某元素,在父元素中添加新的子元素.子元素可以为:stirng|element(js对象)|JQuery元素 var ...

  7. es6-Iterator与for...of

    Iterator(遍历器)的概念 JavaScript原有的表示“集合”的数据结构,主要是数组(Array)和对象(Object),ES6又添加了Map和Set.这样就有了四种数据集合,用户还可以组合 ...

  8. CSS单词换行and断词,你真的完全了解吗

    背景 某天老板在群里反馈,英文单词为什么被截断了? 很显然,这是我们前端的锅,自行背锅.这个问题太简单了,css里加两行属性,分分钟搞定. 开心的提交代码,刷新页面.我擦,怎么还是没有断词?不可能啊! ...

  9. 项目移动后报error LNK1123

    VS20101.解决方案窗口 项目|项目属性|配置属性|清单工具|输入和输出|嵌入清单 “是”改为“否”:2.项目|项目属性|配置属性|连接器|清单文件|嵌入清单 “是”改为“否”:3.对于64位的操 ...

  10. hdu-1166 敌兵布阵---树状数组模板

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目大意: 维护动态的区间和,单点更新,就是模板题 #include<iostream& ...