zoukankan      html  css  js  c++  java
  • ZOJ

    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
    ");
    			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
    ", dp[n]);
    	}
    	return 0;
    }






  • 相关阅读:
    关于http协议详解
    python记录_day15 面向对象初识
    python记录_day14 内置函数二 迭代 二分法
    python记录_day13 内置函数
    python记录_day12 生成器
    python记录_day11 闭包 迭代器
    函数默认参数的初始化问题
    python记录_day10 动态传参 命名空间 作用域
    python记录_day09 初识函数
    python记录_day08
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6741900.html
Copyright © 2011-2022 走看看