Codeforces 474D Flowers (线性dp 找规律)
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).
题目连接:http://codeforces.com/problemset/problem/474/D
题目大意:一个东西爱吃花,有两种颜色红R和白W。他吃白花每次都一组一组吃,一组是连续在一起的k个,问在花的个数从ai到bi范围里。他总共同拥有多少种吃法
题目分析:dp[i]表示长度为i是他吃花的方案数。初始时dp[i] = 1 (0 <= i < k) i小于k时显然仅仅有一种
当i>=k时,我们dp[i]能够是dp[i - 1]加上一朵红花,或者dp[i - k]加上k朵白花,dp[i] = dp[i - 1] + dp[i - k]
然后求出dp数组的前缀和,查询时O(1)
#include <cstdio>
#include <cstring>
#define ll long long
int const MAX = 1e5 + 5;
int const MOD = 1e9 + 7;
int a[MAX];
ll dp[MAX], sum[MAX]; int main()
{
int t, k;
scanf("%d %d", &t, &k);
for(int i = 0; i < k; i++)
dp[i] = 1;
for(int i = k; i < MAX; i++)
dp[i] = (dp[i - 1] % MOD + dp[i - k] % MOD) % MOD;
sum[1] = dp[1];
for(int i = 2; i < MAX; i++)
sum[i] = (sum[i - 1] % MOD + dp[i] % MOD) % MOD;
while(t --)
{
int a, b;
scanf("%d %d", &a, &b);
printf("%lld\n", (MOD + sum[b] - sum[a - 1]) % MOD);
}
}
Codeforces 474D Flowers (线性dp 找规律)的更多相关文章
- Codeforces 474D Flowers(DP)
题目链接 非常简单的一道dp题,通过O(n)的预处理来使查询变为O(1). 主要的坑在于取模后的dp数组的前缀和再相减可能得到负数,导致无法得到某一区间和的取模. 解决方法:(a-b)%mo==(a% ...
- [FJOI2007]轮状病毒 题解(dp(找规律)+高精度)
[FJOI2007]轮状病毒 题解(dp(找规律)+高精度) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1335733 没什么好说的,直接把规律找出来,有 ...
- HDU 1028 Ignatius and the Princess III (母函数或者dp,找规律,)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!
http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE, 更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...
- codeforces B. A and B 找规律
Educational Codeforces Round 78 (Rated for Div. 2) 1278B - 6 B. A and B time limit per test 1 secon ...
- Codeforces 870C Maximum splitting (贪心+找规律)
<题目链接> 题目大意: 给定数字n,让你将其分成合数相加的形式,问你最多能够将其分成几个合数相加. 解题分析: 因为要将其分成合数相加的个数最多,所以自然是尽可能地将其分成尽可能小的合数 ...
- Codeforces - 474D - Flowers - 构造 - 简单dp
https://codeforces.com/problemset/problem/474/D 这道题挺好的,思路是这样. 我们要找一个01串,其中0的段要被划分为若干个连续k的0. 我们设想一个长度 ...
- Codeforces 474D Flowers dp(水
题目链接:点击打开链接 思路: 给定T k表示T组測试数据 每组case [l,r] 有2种物品a b.b物品必须k个连续出现 问摆成一排后物品长度在[l,r]之间的方法数 思路: dp[i] = d ...
- loj6172 Samjia和大树(树形DP+找规律)
题目: https://loj.ac/problem/6172 分析: 首先容易得出这样的dp式子 然后发现后面那个Σ其实是两段区间,可以用总和减去中间一段区间表示,所以只要维护个前缀和就ok了 这样 ...
随机推荐
- [转]Python UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 的解决办法
UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 的解决办法 python在安装时,默认的编码是ascii,当 ...
- Minikube之Win10单机部署Kubernetes(k8s)自动化容器操作的开源平台
Minikube之Win10单机部署 Kubernetes(k8s)是自动化容器操作的开源平台,基于这个平台,你可以进行容器部署,资源调度和集群扩容等操作.如果你曾经用过Docker部署容器,那么可以 ...
- hdu 3292 No more tricks, Mr Nanguo
No more tricks, Mr Nanguo Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Jav ...
- Navicat for Oracle
1.先解压Navicat for Oracle到任意目录 2.将instantclient-basic-nt-12.1.0.2.0解压到1中目录的instantclient_10_2文件夹下(推荐,可 ...
- python BeautifulSoup 获取页面多个子节点中的各个节点的内容
页面html格式为 <tr bgcolor="#7bb5de"><td style="border-bottom: 1px solid #C9D8AD& ...
- 超链接:a标签
a标签的功能:实现跳转功能 a标签的重要属性:href,target href的值为跳转目标的地址,如果是跳转页面的话,需要这个页面的超链接. target的值有四个:_blank._self._pa ...
- RC Immix
目录 RC Immix 目的 合并型引用计数 伪代码 优点和缺点 合并型引用计数法和Immix的融合 新对象 被动的碎片整理 积极的碎片整理 优点和缺点 优点 缺点 RC Immix Rifat Sh ...
- clear---清除当前屏幕
clear命令用于清除当前屏幕终端上的任何信息.
- 紫书 习题 10-13 UVa 11526(打表找规律+分步枚举)
首先看这道题目,我预感商数肯定是有规律的排列的,于是我打表找一下规律 100 / 1 = 100 100 / 2 = 50 100 / 3 = 33 100 / 4 = 25 100 / 5 = ...
- 紫书 习题 10-12 UVa 557(概率计算)
开始的时候我没有考虑1/2的概率,直接一波组合数,然后WA 后来去看题解发现我们可以反过来想,求最后两个人不一样的情况 这个时候肯定会抛到最后的 所以每一种可能就是(0.5)^(n - 2),然后一共 ...