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了 这样 ...
随机推荐
- sklearn 词袋 CountVectorizer
from sklearn.feature_extraction.text import CountVectorizer texts=["dog cat fish","do ...
- Deleting elements
There are several ways to delete elements from a list. If you know the index of the element you want ...
- OpenGL的前世和今生
这并不是一个恰当的题目,因为我主要想说的是OpenGL的今生,基于OpenGL3.x一种更现代化的方式.但是把前世和今生放在一起在语言上更加连贯,而且适当的了解过去,会帮助理解现在的OpenGL,以一 ...
- PostgreSQL Replication之第六章 监控您的设置(2)
6.2 检查pg_stat_replication 检查归档以及 archive_command主要用于即时恢复( PITR,Point-In-Time- Recovery).如果您想监控一个基于流的 ...
- SQL Server在用户自定义函数(UDF)中使用临时表
SQL Server在用户自定义函数中UDF使用临时表,这是不允许的. 有时是为了某些特殊的场景, 我们可以这样的实现: CREATE TABLE #temp (id INT) GO INSERT I ...
- Windows Server 2012 r2 显示计算机图标
在 Windows Server 2012 R2 系统中,微软取消了服务器桌面个性化选项,如何重新调出配置界面,在桌面上显示计算机图标,本文为大家介绍一下! Win2012我的电脑怎么显示到桌面? 一 ...
- 并查集 (Union Find ) P - The Suspects
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...
- [JSOI2007]文本生成器(AC自动机+DP)
题意 给你n个串.问有多少长度为m的串使得这n个串至少在其中出现过一次.输出答案膜10007意义下的结果. (n<=100,每个串的长度<=100) 题解 在AC自动机上跑DP. 用到一个 ...
- 修复linux的grub2引导(单独/boot,lvm-root)
root@ubuntu:/home/ubuntu# pwd /home/ubuntu root@ubuntu:/home/ubuntu# lsblk NAME MAJ ...
- js cookie 页面倒计时
疯了啦 写了一篇没有保存需求:页面倒计时 只从第一次加购开始公共方法cookie的设置 获取function getCookie(c_name){ if (document.cookie.length ...