Codeforces Round #271 (Div. 2) D Flowers【计数dp】
1.5 seconds
256 megabytes
standard input
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 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).
思路:
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】的更多相关文章
- 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. 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) D.Flowers DP
D. Flowers We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, ...
- 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 ...
- Codeforces Round #271 (Div. 2)题解【ABCDEF】
Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
- Codeforces Round #271 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/474 A题:Keyboard 模拟水题. 代码例如以下: #include <iostream> #include ...
- Codeforces Round #271 (Div. 2)
A. Keyboard 题意:一个人打字,可能会左偏一位,可能会右偏一位,给出一串字符,求它本来的串 和紫书的破损的键盘一样 #include<iostream> #include< ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
随机推荐
- <probing> 元素指定扩展Asp.Net加载程序集位置
下面的示例说明如何指定运行库应在其中搜索程序集的应用程序基子目录. <configuration> <runtime> <assemblyBinding xmln ...
- hibernate课程 初探单表映射2-2 hibernate常用配置
1 hibernate.cfg.xml常用配置: show_sql 控制台打印sql format_sql 控制台将sql排版 hbm2ddl.auto: create 删除表结构,重新建表并插值 u ...
- How to Install Apache Solr 4.5 on CentOS 6.4
By Shay Anderson on October 2013 Knowledge Base / Linux / How to Install Apache Solr 4.5 on Cent ...
- Visual Studio Code的快捷键和相关技巧
编辑相关的键盘快捷键: Shift + Alt + F = 格式化代码(似乎不好用) Ctrl + Shift + Enter = 在上一行新建空行并转到上一行 Ctrl + K,Ctrl + C = ...
- amap -bq 192.168.5.9 80 3306
amap -bq 192.168.5.9 80 3306 查看运行在指定端口上运行的服务
- ssh免密钥登录一例问题
今天遇到一个奇怪的问题,在同一机器上创建的普通用户使用 ssh-copy-id -i .ssh/id_rsa.pub root@192.168.3.254 建立与root用户的免密钥通信,结果死活还是 ...
- LeetCode Excel Sheet Column Number 表列数
题意:天啊!我竟然看不懂题意,还去翻别人的代码才懂!给定一个字符串,求该字符串二十六进制的总值. 思路:'A'~'Z'就是1到26,"AA"=26+1=27,"BA&qu ...
- 避免使用 JS 特性 with(obj){}
1)简要说明 with 语句可以方便地用来引用某个特定对象中已有的属性,但是不能用来给对象添加属性.要给对象创建新的属性,必须明确地引用该对象. 2)语法格式 with(object ...
- [VC]strcpy memcpy memset区别与简介
strcpy 原型:extern char *strcpy(char *dest,char *src); 用法:#include <string.h> 功能:把src所指由NULL结束的字 ...
- 黑箱中的 retain 和 release
https://github.com/Draveness/Analyze/blob/master/contents/objc/黑箱中的%20retain%20和%20release.md 写在前面 在 ...