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






  • 相关阅读:
    一个简单粗暴的爬虫
    Linux 目录结构
    python 部署 Restful web
    JVM 运行时数据区总结 栈 堆 堆大小配置总结
    成都法律援助申请流程
    JavaEE error整理(不断更新)
    ehcache.xml 属性大全
    SpringMVC 构建Restful风格 及问题处理
    Http Content-Type
    Redis 教程 Java工程师学习知识点
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6741900.html
Copyright © 2020-2023  润新知