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

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 andk (1?≤?t,?k?≤?105),
wheret represents the number of test cases.

The next t lines contain two integers
ai andbi (1?≤?ai?≤?bi?≤?105),
describing the i-th test.

Output

Print t lines to the standard output. Thei-th line should contain the number of ways in which Marmot can eat betweenai
andbi flowers at dinner modulo1000000007 (109?+?7).

Sample test(s)

Input

3 2
1 3
2 3
4 4

Output

6
5
5

Note

  • For K = 2 and length1 Marmot can eat (R).
  • For K = 2 and length2 Marmot can eat (RR) and (WW).
  • For K = 2 and length3 Marmot can eat (RRR), (RWW) and (WWR).
  • For K = 2 and length4 Marmot can eat, for example, (WWWW) or (RWWR), but for
    example he can‘t eat (WWWR).

考虑第n个。假如n是小于k的,那么只能都是是R,也就是只有一种情况。假如大于等于k,如果第n个是W,那么从n-k+1到n全部为W,如果第n个是R,那么数量就是前n-1个的数量。

dp[n] = 1; (0<= n < k)

dp[n] = dp[n-1] + dp[n-k]; (n >= k)

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cmath>
#define mem(f) memset(f,0,sizeof(f))
#define M 100005
#define mod 1000000007
#define MAX 0X7FFFFFFF
#define maxn 100005
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using namespace std;
typedef long long LL;

int n = maxn, k, t, a, b, dp[maxn], sum[maxn];

int main()
{
    scanf("%d%d", &t, &k);
    for(int i = 0; i < k; i++) dp[i] = 1;
    for(int i = k; i < n; i++) dp[i] = (dp[i-1] + dp[i-k])%mod;
    for(int i = 1; i < n; i++) sum[i] = (sum[i-1] + dp[i])%mod;
    while(t--) {
        scanf("%d%d", &a, &b);
        printf("%d\n", ((sum[b]-sum[a-1])%mod+mod)%mod );
    }
    return 0;
}



时间: 2024-10-10 21:50:03

Codeforces Round #271 (Div. 2) D. Flowers (递推 预处理)的相关文章

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] + dp[i - k] . 1 #include <bits/stdc++.h> 2 using name

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 flowers. Therefore a dinner can be represented as a sequence of several

Codeforces Round #271 (Div. 2)D(递推,前缀和)

很简单的递推题.d[n]=d[n-1]+d[n-k] 注意每次输入a和b时,如果每次都累加,就做了很多重复性工作,会超时. 所以用预处理前缀和来解决重复累加问题. 最后一个细节坑了我多次: printf("%I64d\n",(s[b]-s[a-1]+mod)%mod); 这句话中加mod万万不能少,因为理论上s[b]-s[a-1]肯定大于0,但是由于两个都是模1000000007以后的结果,那么就不一定了,当s[b]-s[a-1]<0时结果是负的,不是题意中应该输出的结果,所以一

(补题解)Codeforces Round #271 (Div. 2)

前言:最近被线段树+简单递推DP虐的体无完肤!真是弱! A:简单题,照着模拟就可以,题目还特意说不用处理边界 B:二分查找即可,用lower_lound()函数很好用 1 #include<string.h> 2 #include<math.h> 3 #include<algorithm> 4 #include<stdio.h> 5 #include<math.h> 6 #include<string> 7 #include<i

Codeforces Round #271 (Div. 2) F.Ant colony(线段树 + 统计区间内某数的个数)

F. Ant colony Mole is hungry again. He found one ant colony, consisting of n ants, ordered in a row. Each ant i (1 ≤ i ≤ n) has a strength si. In order to make his dinner more interesting, Mole organizes a version of «Hunger Games» for the ants. He c

Codeforces Round #271 (Div. 2)

It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are label

Codeforces Round #271 (Div. 2) A. Keyboard

Our good friend Mole is trying to code a big message. He is typing on an unusual keyboard with characters arranged in following way: qwertyuiop asdfghjkl; zxcvbnm,./ Unfortunately Mole is blind, so sometimes it is problem for him to put his hands acc

Codeforces Round #261 (Div. 2) 459B. Pashmak and Flowers(数学题,组合)

题目链接:http://codeforces.com/problemset/problem/459/B B. Pashmak and Flowers time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Pashmak decided to give Parmida a pair of flowers from the garden.

递推 Codeforces Round #186 (Div. 2) B. Ilya and Queries

题目传送门 1 /* 2 递推:用cnt记录前缀值,查询区间时,两个区间相减 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cmath> 7 #include <cstring> 8 using namespace std; 9 10 const int MAXN = 1e5 + 10; 11 const int INF = 0x3f3f3f3f; 12 char s[MAXN]; 1