Codeforces 474D Flowers (线性dp 找规律)

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);
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-04 20:23:47

Codeforces 474D Flowers (线性dp 找规律)的相关文章

POJ 1157 LITTLE SHOP OF FLOWERS (线性dp)

OJ题目:click here~~ 题目分析:f个束花,编号为1-- f.v个花瓶,编号为1 -- v.编号小的花束,所选花瓶的编号也必须比编号大的花束所选花瓶的编号小,即花i 选k, 花j选t ,如果i < j ,则定有k < t . 如果 i > j , 则定有 k > t . 每束花放在每个花瓶里有一个值.求f束花,能得到的最大值. 设dp[ i ][ j ] 为第 i 束花选择了第 j 个花瓶 , 则转移方程为 dp[ i ][ j ] =  max(dp[ i  - 1]

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

[FJOI2007]轮状病毒 题解(dp(找规律)+高精度) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1335733 没什么好说的,直接把规律找出来,有两种规律(据说还有多种dp),再套个高精度 \(First\) \(f[1]=1,f[2]=5,f[i]=3×f[i-1]-f[i-2]+2\) 就直接写个高精+低精和高精×低精和高精-高精就行了 \(Second\) \(f[1]=1,f[2]=3,f[i]=f[i-1]+f[i-2]\) \(i

Codeforces 474D Flowers dp(水

题目链接:点击打开链接 思路: 给定T k表示T组测试数据 每组case [l,r] 有2种物品a b,b物品必须k个连续出现 问摆成一排后物品长度在[l,r]之间的方法数 思路: dp[i] = dp[i-1]+dp[i-k]; #include <iostream> #include <cmath> #include <algorithm> #include <cstdio> #include <cstring> #include <v

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

http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE,  更重要的是找出规律后,O(n)递推也过不了,TLE,一定要矩阵快速幂.然后立马GG. 用2代表m,1代表f.设dp[i][j][k]表示,在第i位,上一位站了的人是j,这一位站的人是k,的合法情况. 递推过去就是,如果j是1,k是2,那么这一位就只能放一个2,这个时猴dp[i][k][2] += dp[i - 1][j][k]; 其他情况分类下就好,然后

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

题目: https://loj.ac/problem/6172 分析: 首先容易得出这样的dp式子 然后发现后面那个Σ其实是两段区间,可以用总和减去中间一段区间表示,所以只要维护个前缀和就ok了 这样复杂度就是O(nm)的 但是题目中的m异常巨大,有1e9,好像不能用dp做 但我们可以找下规律,发现对于一个点,其所有dp值是前后对称的,而且中间有很长的一段都是相同的数字! 设某个点x深度为d,那么它受到最“偏”的影响是来自叶子节点的,状态值之间会差d*k 对于一颗树而言,深度最大值为n-1,所以

ZOJ-3929 Deque and Balls (DP+找规律)

题目大意:n个数,每个数的大小都在1~n之间.操作n次,第 i 次将第 i 个数放到一个双端队列里面,放到队列两端的概率是相等的.问操作n次之后双端队列中元素满足xi>xi+1的对数的期望,输出的数据为:(期望*2^n)%mod. 题目分析:定义状态dp(i)表示操作 i 次之后的相应期望值.则状态转移方程为: dp(i)=1/2*(dp(i-1)+k1)+1/2*(dp(i-1)+k2)  (两种情况,放在队首和队尾) 其中,k1表示比a(i)小的元素可能与a(i)相邻的总次数,k2表示比a(

Codeforces 474D Flowers

http://codeforces.com/problemset/problem/474/D 思路:F[i]=F[i-1]+(i>=K)F[i-k] 1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<cstring> 5 #include<iostream> 6 const int Mod=1000000007; 7 int K,sum[200005]

Codeforces Gym 100637B B. Lunch 找规律

B. Lunch Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/B Description The swamp looks like a narrow lane with length n covered by floating leaves sized 1, numbered from 1 to n with a fly sitting on the top of ea

hdu 5000 Clone (dp + 找规律)

Clone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 820    Accepted Submission(s): 403 Problem Description After eating food from Chernobyl, DRD got a super power: he could clone himself righ