D. Flowers Codeforces Round #271(div2)

Codeforces 474D Flowers 动态规划法 话说好久没写算法代码了,工作了有点忙的了,不过算法始终是我的挚爱,故此还是尽量抽时间和挚爱来个约会。 Codeforces的题目是最适合练手的了,下面是一道不算难的动态规划法题目,先上题:D. Flowerstime limit per test1.5 seconds memory limit per test256 megabytes inputstandard input outputs 阅读详情
D. Flowers
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
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 modulo1000000007 (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).


状态转移方程 dp[i]=dp[i-1]+dp[i-k]。取模要特别注意。

代码:
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
const long long mod = 1000000007;
const int maxn=100000;
long long dp[maxn+100];
long long sum[maxn+100];
using namespace std;
int main(){
    int n,k;
    memset(sum, 0, sizeof(sum));
    scanf("%d %d",&n,&k);
    sum[0]=0;
    dp[0]=1;
    for(int i=1;i<k;++i){
        dp[i]=1;
        sum[i]=sum[i-1]+dp[i];
    }
    for(int i=k;i<=maxn;++i){
        dp[i]=dp[i-1]+dp[i-k];
        dp[i]%=mod;
        sum[i]=sum[i-1]+dp[i];
        sum[i]%=mod;
    }
    int a,b;
    for(int i=0;i<n;++i){
        scanf("%d %d",&a,&b);
        printf("%I64d\n",(sum[b]-sum[a-1]+mod)%mod);
    }
    return 0;
}



codeforces 474D Flowers 动态规划 题目链接 http://codeforces.com/contest/474/problem/D 把红花和白花摆成一排,并且要求若出现白花,它们连续的数量必须是k的倍数。 给我们 n,接下来的n次询问。和k   (n,k&lt;100000) 每行 一个 a[i],b[i]。 设f(k)为长度为k的满足条件的一排花 的 可能的数量 最后求f(a[i])+f(a[i+1])+...+f(b... 阅读详情

相关推荐

题解3-C

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

weixin_45664948的博客 238

Flowers

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

weixin_45988242的博客 310

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

https://codeforces.com/problemset/problem/474/D 这道题挺好的,思路是这样。 我们要找一个01串,其中0的段要被划分为若干个连续k的0。 我们设想一个长度为n的合法串是怎么被构造出来的,要么是上一个合法串后面直接连接1,要么是上一个合法串后面连接k个连续的0,那么每个0一一对应于一段连续的0。 所以dp[i]=dp[i-1]+dp[i-k]。 ...

weixin_30835649的博客 184

Codeforces Round #271 (Div. 2), problem: (D) Flowers 【DP+前缀和+取模运算】

Codeforces Round #271 (Div. 2), problem: (D) Flowers 题目大意 土拨鼠???反正大概意思是拿红花或者k个白花,那么对于dp [ i ] =dp [ i - 1 ] + dp [ i - k ] 对于第 i 个长度 可以是之前最后一个红花或者k个白花 题解 题目给你的a 和 b 是一个区间范围的方法值 所以我们对我们求出的dp数组还要...

超逸の学习技术博客 734

Codeforces Round #271 (Div. 2) D. Flowers(DP)

题目地址:http://codeforces.com/problemset/problem/474/D 思路:f[i]表示长度为i时的方案数,当0=k时,f[i]可以是f[i-1]加上一朵红花,也可以是f[i-k]加上k朵白花。 即f[i]=f[i-1]+f[i-k],求前缀和,则区间【a,b】内方案数为sum[b]-sum[a-1]。 #include #include #incl

哆啦AC梦的博客 534

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 know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can

__ElemenT 795

Codeforces Round #271 (Div. 2) D. Flowers (递推)

题目链接:http://codeforces.com/problemset/problem/474/D 用RW组成字符串,要求w的个数要k个连续出现,R任意,问字符串长度为[a, b]时,字符串的种类有多少。 递推,dp[i]表示长度为i的种类有多少。当i < k的时候 dp[i] = 1 , 当i == k的时候 dp[i] = 2 , 否则 dp[i] = dp[i - 1] + ...

weixin_30739595的博客 101

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, as we all know, Marmot eats flowers. At every dinner he eats some red and white...

weixin_30687051的博客 195

Codeforces Round #271 (Div. 2) D Flowers【计数dp】

D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input output standard output We saw the little game Marmot made for Mole's ...

weixin_34059951的博客 113

Codeforces Round #271 (Div. 2)

D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input output standard output We saw the little game Marmot made for Mole'...

weixin_34217773的博客 145

「日常训练」Watering FlowersCodeforces Round #340 Div.2 C)

题意与分析 (CodeForces 617C) 题意是这样的:一个花圃中有若干花和两个喷泉,你可以调节水的压力使得两个喷泉各自分别以\(r_1\)和\(r_2\)为最远距离向外喷水。你需要调整\(r_1,r_2\)的值使得所有的花都能被水所灌溉——换句话说,每一朵花要么到第一个喷泉的距离不超过\(r_1\),要么到第二个喷泉的距离不超过\(r_2\)。当然如果两个条件都满足也是可以的。你需要用尽可...

weixin_30877755的博客 201

Codeforces Round #340 (Div. 2)题解

A. ElephantAn elephant decided to visit his friend. It turned out that the elephant’s house is located at point 0 and his friend’s house is located at point x(x > 0) of the coordinate line. In one step

long-long-ago 1453

D. Flowers Codeforces Round 271 (Div. 2)

题目大致意思:放白花和红花,鼹鼠要至少有一个连续长度为k的W,问有多少方式。

halation_Z的博客 444
上一篇: hdu 5057 Argestes and Sequence(BestCoder Round #11)
下一篇: B. Worms Codeforces Round #271 (div2)
AC_way
博客等级 码龄12年 55粉丝 248原创
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值