Codeforces Round #271 (Div. 2) D. Flowers (递推 预处理)
来源:互联网 发布:数据安全防护体系 编辑:程序博客网 时间:2024/05/16 05:02
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 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.
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).
3 21 32 34 4
655
- 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 <stdio.h>#include <string.h>#include <math.h>#include <iostream>#include <queue>#include <algorithm>#include <cmath>#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;}
- Codeforces Round #271 (Div. 2) D. Flowers (递推 预处理)
- Codeforces Round #271 (Div. 2) D. Flowers(DP)
- Codeforces Round #271 (Div. 2) D 递推
- Codeforces Round #240 (Div. 2)(D:dp递推)
- Codeforces Round #249 (Div. 2) D. Special Grid (递推乱搞)(好题)
- Codeforces Round #239 (Div. 2) D Long Path(递推)
- Codeforces Round #332 (Div. 2)-D Spongebob and Squares(枚举+递推)
- Codeforces Round #447 (Div. 2) D 预处理+归并+ 二分
- Codeforces Round #340 (Div. 2)C. Watering Flowers(暴力)
- D. Flowers Codeforces Round #271(div2)
- 【Codeforces Round 271 (Div 2)D】【DP】Flowers 黑色必须连续摆放k,长度为n的摆放方案数
- Codeforces Round #411(Div. 2)D. Minimum number of steps【思维递推+快速幂】
- Codeforces Round #271 (Div. 2) D
- codeforces Round # 187(Div.2) D
- Codeforces Round #277 (Div. 2)(D)
- Codeforces Round #291 (Div. 2)(D)
- Codeforces Round #418 (Div. 2) C (尺取+预处理)
- Codeforces Round #346 (Div. 2) G 递推
- 诺贝尔生理学或医学奖揭晓 三名科学家共同获得
- ios swift学习日记1-Swift 初见
- PHP获得文件的md5并检验是否被修改
- Java输入输出(2)--流
- 优酷真实地址解析
- Codeforces Round #271 (Div. 2) D. Flowers (递推 预处理)
- 八皇后问题(用排列来做)
- c语言结构体指针初始化
- Ubuntu 安装mysql和简单操作
- hibernate抓取策略fetch=select /join/subselect
- 职场分享:什么情况下我们不能要求加薪
- codeforces Round #271(div2) C解题报告
- 操作系统 - 线程
- Notification framework层的处理流程分析