博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #271 (Div. 2) D. Flowers (递推 预处理)
阅读量:6294 次
发布时间:2019-06-22

本文共 2159 字,大约阅读时间需要 7 分钟。

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

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.

Output

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

Sample test(s)
Input
3 21 32 34 4
Output
655
Note
  • 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 
#include
#include
#include
#include
#include
#include
#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, rusing 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;}



转载地址:http://flpta.baihongyu.com/

你可能感兴趣的文章
再学 GDI+[43]: 文本输出 - 获取已安装的字体列表
查看>>
nginx反向代理
查看>>
操作系统真实的虚拟内存是什么样的(一)
查看>>
hadoop、hbase、zookeeper集群搭建
查看>>
python中一切皆对象------类的基础(五)
查看>>
modprobe
查看>>
android中用ExpandableListView实现三级扩展列表
查看>>
%Error opening tftp://255.255.255.255/cisconet.cfg
查看>>
java读取excel、txt 文件内容,传到、显示到另一个页面的文本框里面。
查看>>
《从零开始学Swift》学习笔记(Day 51)——扩展构造函数
查看>>
python多线程队列安全
查看>>
[汇编语言学习笔记][第四章第一个程序的编写]
查看>>
android 打开各种文件(setDataAndType)转:
查看>>
补交:最最原始的第一次作业(当时没有选上课,所以不知道)
查看>>
Vue实例初始化的选项配置对象详解
查看>>
PLM产品技术的发展趋势 来源:e-works 作者:清软英泰 党伟升 罗先海 耿坤瑛
查看>>
vue part3.3 小案例ajax (axios) 及页面异步显示
查看>>
浅谈MVC3自定义分页
查看>>
.net中ashx文件有什么用?功能有那些,一般用在什么情况下?
查看>>
select、poll、epoll之间的区别总结[整理]【转】
查看>>