D. Flowers
time limit per test:1.5 seconds
memory limit per test:256 megabytes

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

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.

Output

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).

Sample test(s)
Input
3 2
1 3
2 3
4 4
Output
6
5
5
Note
  • 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 找规律)的更多相关文章

  1. Codeforces 474D Flowers(DP)

    题目链接 非常简单的一道dp题,通过O(n)的预处理来使查询变为O(1). 主要的坑在于取模后的dp数组的前缀和再相减可能得到负数,导致无法得到某一区间和的取模. 解决方法:(a-b)%mo==(a% ...

  2. [FJOI2007]轮状病毒 题解(dp(找规律)+高精度)

    [FJOI2007]轮状病毒 题解(dp(找规律)+高精度) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1335733 没什么好说的,直接把规律找出来,有 ...

  3. 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 ...

  4. hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!

    http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE,  更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...

  5. 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 ...

  6. Codeforces 870C Maximum splitting (贪心+找规律)

    <题目链接> 题目大意: 给定数字n,让你将其分成合数相加的形式,问你最多能够将其分成几个合数相加. 解题分析: 因为要将其分成合数相加的个数最多,所以自然是尽可能地将其分成尽可能小的合数 ...

  7. Codeforces - 474D - Flowers - 构造 - 简单dp

    https://codeforces.com/problemset/problem/474/D 这道题挺好的,思路是这样. 我们要找一个01串,其中0的段要被划分为若干个连续k的0. 我们设想一个长度 ...

  8. Codeforces 474D Flowers dp(水

    题目链接:点击打开链接 思路: 给定T k表示T组測试数据 每组case [l,r] 有2种物品a b.b物品必须k个连续出现 问摆成一排后物品长度在[l,r]之间的方法数 思路: dp[i] = d ...

  9. loj6172 Samjia和大树(树形DP+找规律)

    题目: https://loj.ac/problem/6172 分析: 首先容易得出这样的dp式子 然后发现后面那个Σ其实是两段区间,可以用总和减去中间一段区间表示,所以只要维护个前缀和就ok了 这样 ...

随机推荐

  1. 可变对象 vs 不可变对象(Python)

    Python 在 heap 中分配的对象分成两类:可变对象和不可变对象.所谓可变对象是指,对象的内容是可变的,例如 list.而不可变的对象则相反,表示其内容不可变. 不可变对象:int,string ...

  2. BZOJ 2115 DFS+高斯消元

    思路: 先搞出来所有的环的抑或值 随便求一条1~n的路径异或和 gauss消元找异或和最大 贪心取max即可 //By SiriusRen #include <cstdio> #inclu ...

  3. HBase的体系结构

  4. watchpoint set variable

    watchpoint set variable string_weak_assign Watchpoint created: Watchpoint 3: addr = 0x10fcaa468 size ...

  5. Java ——代理模式[转发]

    1.  简介 代理模式(Proxy Pattern)是GoF 23种Java常用设计模式之一.代理模式的定义:Provide a surrogate or placeholder for anothe ...

  6. NodeJS学习笔记 (13)数据加密-crypto(OK)

    写在前面 本章节写得差不多了,不过还需要再整理一下(TODO). hash例子 hash.digest([encoding]):计算摘要.encoding可以是hex.latin1或者base64.如 ...

  7. Java 异常的捕获与处理详解 (一)

    一,异常的产生(Exception) 异常是程序之中导致程序中断的一种指令流,异常一旦出现并且没有进行合理处理的话,那么程序就会中断执行. An exception is a flow of inst ...

  8. 【Henu ACM Round#19 B】 Luxurious Houses

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 从右往左维护最大值. 看到比最大值小(或等于)的话.就递增到比最大值大1就好. [代码] #include <bits/std ...

  9. apache kafka监控系列-KafkaOffsetMonitor

    apache kafka中国社区QQ群:162272557 概览 近期kafka server消息服务上线了,基于jmx指标參数也写到zabbix中了.但总认为缺少点什么东西.可视化可操作的界面. z ...

  10. 28. Brackets安装angularjs插件

    Brackets是Adobe公司研发的一款开源WEB前端开发框架,界面清爽简约,代码提示功能比较强大,而且支持第三方插件,其提供的插件库中有大量的对Brackets感兴趣的开发人员所开发的插件,使用者 ...