题意:

有n朵花排成一排,小明要么吃掉连续的k朵白花,或者可以吃单个的红花。

给出一个n的区间[a, b],输出总吃花的方法数模 109+7 的值。

分析:

设d(i)表示吃i朵花的方案数。

则有如下递推关系:

d[i] = d[i-1] + d[i-k], (i ≥ k, d[0] = 1)

我们在计数i+1的情况时,可以分为如下两种情况:

  • 最后一朵是红花,方案数为d[i]
  • 最后k朵是白花,方案数为d[i-k]

然后预处理一下前缀和。

代码中注意取模。

 #include <cstdio>
const int maxn = + ;
const int MOD = + ;
int d[maxn]; int main()
{
int t, k, i;
scanf("%d%d", &t, &k); d[] = ;
for(i = ; i < k; ++i) d[i] = ;
for(; i < maxn; ++i) d[i] = (d[i-] + d[i - k]) % MOD; for(i = ; i < maxn; ++i) d[i] = (d[i] + d[i-]) % MOD;
for(i = ; i < t; ++i)
{
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", (d[b] - d[a-] + MOD) % MOD);
} return ;
}

代码君

CodeForces 474.D Flowers的更多相关文章

  1. Codeforces 474 F. Ant colony

    线段树求某一段的GCD..... F. Ant colony time limit per test 1 second memory limit per test 256 megabytes inpu ...

  2. Codeforces 474 E. Pillars

    水太...... E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. CodeForces 617C Watering Flowers

    无脑暴力题,算出所有点到圆心p1的距离的平方,从小到大排序. 然后暴力枚举p1的半径的平方,计算剩余点中到p2的最大距离的平方,枚举过程中记录答案 #include<cstdio> #in ...

  4. 【Codeforces 474D】Flowers

    [链接] 我是链接,点我呀:) [题意] 让你吃东西 B食物一次必须要吃连续k个 但是对A食物没有要求 问你有多少种吃n个食物的方法(吃的序列) [题解] 设f[i]表示长度为i的吃的序列且符合要求的 ...

  5. Codeforces 474 C. Captain Marmot

    4*4*4*4暴力+点的旋转+推断正方型 C. Captain Marmot time limit per test 1 second memory limit per test 256 megaby ...

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

    D. Flowers time limit per test:1.5 seconds memory limit per test:256 megabytes We saw the little gam ...

  7. codeforces 474D.Flowers 解题报告

    题目链接:http://codeforces.com/problemset/problem/474/D 题目意思:Marmot 吃两种类型的花(实在难以置信呀--):red 或者 white,如果要吃 ...

  8. Codeforces Round #340 (Div. 2) C. Watering Flowers 暴力

    C. Watering Flowers 题目连接: http://www.codeforces.com/contest/617/problem/C Descriptionww.co A flowerb ...

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

    题目链接:http://codeforces.com/problemset/problem/474/D 用RW组成字符串,要求w的个数要k个连续出现,R任意,问字符串长度为[a, b]时,字符串的种类 ...

随机推荐

  1. sqlserver 动态表名 动态字段名 执行 动态sql

    动态语句基本语法: 1 :普通SQL语句可以用exec执行 Select * from tableName exec('select * from tableName') exec sp_execut ...

  2. 2693: jzptab - BZOJ

    Description Input 一个正整数T表示数据组数接下来T行 每行两个正整数 表示N.MOutput T行 每行一个整数 表示第i组数据的结果Sample Input 1 4 5 Sampl ...

  3. c++ 函数返回指针 及用法

    #include<string> #include<iostream> using namespace std; string fun1(int a) { string str ...

  4. Linux安装python 2.7.9

    1.下载python wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz 2.解压.编译安装 tar -zxvf Python- ...

  5. Openstack Quantum project 更名为 Neuron

    因为与磁带备份厂商Quantum商标冲突: The OpenStack Foundation has changed the name of its networking project from Q ...

  6. Ubuntu 下使用Remmina Remote Desktop client 连接windows server输入法的问题

    Ubuntu 自带的Remmina Remote  Desktop 用来连接windows,vnc,ssh等非常方便好用,   但我在连接windows 2008 r2 server时遇到一个问题: ...

  7. poj 3625 Building Roads(最小生成树,二维坐标,基础)

    题目 //最小生成树,只是变成二维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> #include<stdio.h> ...

  8. Code::Blocks生成的EXE文件执行错误解决:The program can't start because libgcc_s_dw2-1.dll is missing

    想用C++弄个简单东东,看有没有可行性, 开发软件,微软的太大太肿,就选用了Code::Blocks. 测试HELLO时,在工程环境中没问题的,但生成的EXE执行有问题, 报什么 libgcc_s_d ...

  9. JAVASCRIPT中的作用域和原型链,应该算是难点了,要好好多学学,练练

    今天初六,要上班啦... JAVASCRIPT,看来是丢不了了.. http://www.dengdeng90.com/wordpress/?p=241 http://www.cnblogs.com/ ...

  10. hdu 1796 How many integers can you find

    容斥原理!! 这题首先要去掉=0和>=n的值,然后再使用容斥原理解决 我用的是数组做的…… #include<iostream> #include<stdio.h> #i ...