CodeForces 474.D Flowers
题意:
有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的更多相关文章
- Codeforces 474 F. Ant colony
线段树求某一段的GCD..... F. Ant colony time limit per test 1 second memory limit per test 256 megabytes inpu ...
- Codeforces 474 E. Pillars
水太...... E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CodeForces 617C Watering Flowers
无脑暴力题,算出所有点到圆心p1的距离的平方,从小到大排序. 然后暴力枚举p1的半径的平方,计算剩余点中到p2的最大距离的平方,枚举过程中记录答案 #include<cstdio> #in ...
- 【Codeforces 474D】Flowers
[链接] 我是链接,点我呀:) [题意] 让你吃东西 B食物一次必须要吃连续k个 但是对A食物没有要求 问你有多少种吃n个食物的方法(吃的序列) [题解] 设f[i]表示长度为i的吃的序列且符合要求的 ...
- Codeforces 474 C. Captain Marmot
4*4*4*4暴力+点的旋转+推断正方型 C. Captain Marmot time limit per test 1 second memory limit per test 256 megaby ...
- Codeforces 474D Flowers (线性dp 找规律)
D. Flowers time limit per test:1.5 seconds memory limit per test:256 megabytes We saw the little gam ...
- codeforces 474D.Flowers 解题报告
题目链接:http://codeforces.com/problemset/problem/474/D 题目意思:Marmot 吃两种类型的花(实在难以置信呀--):red 或者 white,如果要吃 ...
- Codeforces Round #340 (Div. 2) C. Watering Flowers 暴力
C. Watering Flowers 题目连接: http://www.codeforces.com/contest/617/problem/C Descriptionww.co A flowerb ...
- Codeforces Round #271 (Div. 2) D. Flowers (递推)
题目链接:http://codeforces.com/problemset/problem/474/D 用RW组成字符串,要求w的个数要k个连续出现,R任意,问字符串长度为[a, b]时,字符串的种类 ...
随机推荐
- 1021 玛丽卡 - Wikioi
题目描述 Description麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知 ...
- Kafka之ReplicaManager(1)
基于Kafka 0.9.0版 ReplicaManager需要做什么 Replicated Logs Kafka的partition可以看成是一个replicated log, 每个replica就是 ...
- hdu 4586 Play the Dice
思路:设期望值为s,前m个是再来一次机会,则有 s=(a[1]+s)/n+(a[2]+s)/n+……+(a[m]+s)/n+a[m+1]/n…… 化简:(n-m)s=sum 当sum=0时,为0: 当 ...
- C# Regex类用法
使用Regex类需要引用命名空间:using System.Text.RegularExpressions; 利用Regex类实现全部匹配输出 string str = "test43232 ...
- Python中Lambda, filter, reduce and map 的区别
Lambda, filter, reduce and map Lambda Operator Some like it, others hate it and many are afraid of t ...
- [wikioi]线段树练习
http://codevs.cn/problem/1080/ #include <vector> #include <iostream> #include <string ...
- TCL语言笔记:TCL练习二
一.练习 1.二进制转十进制 proc b2d {b} { ;set len [string length $b] } {$i<$len} {incr i} { incr sum [expr , ...
- 百度地图API简单使用
百度地图API是由JavaScript语言编写的,在使用之前需要将API引用到页面中: 现在新版本的需要密钥,下面用的是旧版的 <script src="http://api.map ...
- arcengine C# 读写lyr(转)
写lyr { IFeatureLayer LineLayer = axMapControl1.get_Layer(0) as IFeatureLayer; ILayerFile ...
- Java文件解压之TGZ解压
package com.alibaba.intl.batch.dependency; import java.io.File; import java.io.FileInputStream; impo ...