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了 这样 ...
随机推荐
- hdoj--2709--Sumsets(数位dp)
Sumsets Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- PostgreSQL相关总结
源码安装PostgreSQL总结 简明安装步骤(其中prefix指定PostgreSQL的安装目录,该目录与数据目录pgdata和PostgreSQL的源代码包目录均无关) yum -y instal ...
- JavaScript 获取移动设备的型号
https://joyqi.com/javascript/how-to-detect-mobile-devices-model-using-javascript.html?utm_source=too ...
- Python 上下文(Context)学习笔记
前言 最早接触到with语句的时候,是初学python,对文件进行读写的时候,当时文件读写一般都是用open()函数来对文件进行读写,为了防止读写的过程中出现错误,也为了让代码更加的pythonic, ...
- ATL中宏定义offsetofclass的分析
近日学习ATL,通过对宏定义offsetofclass的解惑过程.顺便分析下虚函数表,以及通过虚函数表调用函数的问题. 1 解开ATL中宏定义offsetofclass的疑惑 #define _ATL ...
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
- LVDS原理及设计指南
LVDS是一种低摆幅的差分信号技术,它使得信号能在差分PCB 线对或平衡电缆上以 几百Mbps的速率传输,其低压幅和低电流驱动输出实现了低噪声和低功耗. IEEE 在两个标准中对LVDS ...
- BZOJ2016: [Usaco2010 Feb]Chocolate Eating
[传送门:BZOJ2016] 简要题意: 贝西收到了N 块巧克力,她会在接下来的D 天里吃掉这些巧克力,她想制定一个计划,让她每 天的快乐度都保持在较高的水品上. 在第一天刚开始的时候,贝西的快乐度为 ...
- js插件---图片裁剪cropImgBox(适合练习编写插件之用)
js插件---图片裁剪cropImgBox(适合练习编写插件之用) 一.总结 一句话总结:无论是灰度还是高对比度的图片,都是先处理canvas的像素,使其变成灰度或者高对比度,然后再用canvas.t ...
- 30.angularJS第一个实例
转自:https://www.cnblogs.com/best/tag/Angular/ AngularJS 通过 ng-directives 扩展了 HTML. ng-app 指令定义一个 Angu ...