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之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
随机推荐
- qemu 出现Could not access KVM kernel module: No such file or directory failed to initialize KVM: No such file or directory
使用qemu命令 qemu-system-x86_64 -hda image/ubuntu-test.img -cdrom ubuntu-16.04.2-server-amd64.iso -m 102 ...
- 扩展(spread)/收集(rest)运算符
一.扩展运算符(spread) 场景:使用在数组之前. 作用:将一个数组转为用逗号分隔的参数序列 举例1:数组之前 function foo(x, y, z){ console.log(x, y ...
- [转]Linux中如何读写硬盘上指定物理扇区
读指定物理扇区: dd if=<源设备> of=<输出设备或文件> skip=<指定扇区值> bs=512 count=1 写指定物理扇区: dd i ...
- 海海DRM视频保护解密流程分析
环境及工具 手机 :小米手机 MI 2A 系统版本: Android 4.1.1 工具 : IDA pro 6.6 .C32Asm .VS2005 一:第一次打开加密视频会出现如下验证: ...
- UGUI的事件系统分析
UGUI的源码还是非常清晰的,打开源码可以发现, 从UGUI的源码可知:在EventSystem中调用每一帧函数来实现: private void TickModules() { for (var i ...
- git剖析
GIT(分布式版本控制系统) Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. 1.特点 分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者通过克隆(g ...
- linux打开文件数测试
/proc/sys/kernel/threads-max 系统最大线程数量 /proc/sys/vm/max_map_count 限制一个进程可以拥有的VMA(虚拟内存区域)的数量 /proc/sys ...
- 允许被ping设置方法
参考下图设置:
- 传入指定字段名称就可以排序的EF写法
private static IQueryable<T> SetQueryableOrder<T>(this IQueryable<T> query, string ...
- empty、isset、is
直接上代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <?php $a=0; $b='0'; $c=0.0; ...