ZOJ - 3725 Painting Storages

Description

There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob asks you to paint all storages with two colors: red and blue. Each storage will be painted with exactly one color.

Bob has a requirement: there are at least M continuous storages (e.g. "2,3,4" are 3 continuous storages) to be painted with red. How many ways can you paint all storages under Bob‘s requirement?

Input

There are multiple test cases.

Each test case consists a single line with two integers: N and M (0&ltN, M<=100,000).

Process to the end of input.

Output

One line for each case. Output the number of ways module 1000000007.

Sample Input

4 3

Sample Output

3

题意:n个格子排成一条直线,可以选择涂成红色或蓝色,问最少 m 个连续为红色的方案数。

思路:DP,分两种情况,一种是对于第i个,如果前i-1个已经有了,那么第i个就无所谓了。另一种是加上第i个才能构成m个连续的话,那么第i-m个就是蓝色的,然后让前i-1-m个不包含连续m个的红色。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
const int maxn = 100005;
const int mod = 1000000007;

ll f[maxn], dp[maxn];
int n, m;

int main() {
	f[0] = 1;
	for (int i = 1; i < maxn; i++)
		f[i] = f[i-1] * 2 % mod;

	while (scanf("%d%d", &n, &m) != EOF) {
		if (m > n) {
			printf("0\n");
			continue;
		}

		memset(dp, 0, sizeof(dp));
		dp[m] = 1;
		for (int i = m+1; i <= n; i++)
			dp[i] = ((dp[i-1] * 2 + f[i-1-m] - dp[i-m-1]) % mod + mod) % mod;

		printf("%lld\n", dp[n]);
	}
	return 0;
}
时间: 2024-10-12 15:15:02

ZOJ - 3725 Painting Storages的相关文章

[ACM] ZOJ 3725 Painting Storages (DP计数+组合)

Painting Storages Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob asks you to paint all storages with two colors: red and blue. Each storage will be painted with e

(DP) zoj 3725

Painting Storages Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob asks you to paint all storages with two colors: red and blue. Each storage will be painted with e

Painting Storages

题目链接 题意: 给n个球,每个球可以涂成红色和蓝色任意一种,求相连的红色球的个数不少于m的涂色方案有多少种,结果对MOD取模 分析: 两种方法解决,第一种直接求,也可以求,就是状态转移略麻烦:另一种求对立问题,即相连的红色球的个数小于于m的涂色方案有多少种,这个相比第一个简单些,但是第一种也可以解决.采用第二种: dp[i]表示i位置放蓝球,满足题意的共几种方案.这个题目如果采用当前状态更新之后状态就比较麻烦,相等于每次区间更新:如果采用由之前的状态求出当前的未知状态就比较简单,区间和,用前缀

[kuangbin带你飞]专题七 线段树

A - 敌兵布阵 HDU - 1166 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视. 中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少

ZOJ - 2575 Full of Painting

Description Tingting wants to draw and stucco N squares with N different colors full of the base line of a wall. Give you the number of squares, the length of the wall, the minimum size and the maximal size of each square and the price of stuccoing o

zoj 1610 Count the Colors 【区间覆盖 求染色段】

Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different colors you can s

ZOJ 3780 Paint the Grid Again(隐式图拓扑排序)

Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or white). Leo has a magical brush which can paint any row with black color, or an

Count the Colors (zoj 1610 线段树 区间颜色覆盖)

Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different colors you can s

F - Count the Colors ZOJ 1610 (线段树+结点为长度为一的区间+树的遍历)

F - Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Status Practice ZOJ 1610 Description Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.