题意:给定一个数N,表示有N个位置,要么放置0,要么放置1,问至少存在一个连续的M个1的放置方式有多少?

分析:正面求解可能还要考虑到重复计算带来的影响,该题适应反面求解。设dp[i][j]表示到前 i 为后导 1 个数为 j 的方案数,于是有动态规划方程:

dp[i][0] = sum{ dp[i-1][0... min(i-1, M) ] };
dp[i][j] = dp[i-1][j-1]  其中 j != 1

单单根据这个方程时间度为O(N*M),还是不足以在有限的时间内解出该问题。通过观察我们发现方程可以简化,即后一层的和值是上一层和值的两倍减去上层的最后一个值。于是可以维护一个最后一个值得队列,然后计算出首行的和值即可。注意首行是表示状态数达到M个的行。

代码如下:

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std; typedef long long LL;
const int mod = int(1e9)+;
int N, M;
queue<int>q;
int pp[]; LL _pow(LL a, int b) {
LL ret = ;
while (b) {
if (b & ) ret = (ret * a) % mod;
b >>= ;
a = (a * a) % mod;
}
return ret;
} void gao() {
int ret = ;
while (!q.empty()) q.pop();
pp[] = , pp[] = ;
q.push(pp[]);
q.push(pp[]);
for (int i = ; i < M; ++i) {
pp[i] = (1LL * pp[i-] * ) % mod;
ret = (1LL * ret + pp[i]) % mod;
q.push(pp[i]);
}
for (int i = M; i <= N; ++i) {
q.push(ret);
ret = (1LL * ret * - q.front() + mod) % mod;
q.pop();
}
printf("%d\n", (1LL * _pow(, N) - ret + mod) % mod);
} int main() {
while (scanf("%d %d", &N, &M) != EOF) {
if (N == || M > N) {
puts("");
continue;
}
if (M == ) {
printf("%d\n", _pow(, N));
continue;
}
if (M == ) {
printf("%d\n", (_pow(, N)-+mod)%mod);
continue;
}
gao();
}
return ;
}

ZOJ-3725 Painting Storages 动态规划的更多相关文章

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

    Painting Storages Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a straight highway with ...

  2. ZOJ - 3725 Painting Storages

    Description There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob ask ...

  3. zoj 3725 - Painting Storages(动归)

    题目要求找到至少存在m个连续被染成红色的情况,相对应的,我们求至多有m-1个连续的被染成红色的情况数目,然后用总的数目将其减去是更容易的做法. 用dp来找满足条件的情况数目,, 状态:dp[i][0] ...

  4. ZOJ 3725 Painting Storages(DP+排列组合)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5048 Sample Input 4 3 Sample Output ...

  5. Painting Storages(ZOJ)

    There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob asks you to pai ...

  6. zoj 3725

    题意: n个格子排成一条直线,可以选择涂成红色或蓝色,问最少 m 个连续为红色的方案数. 解题思路: 应该是这次 ZOJ 月赛最水的一题,可惜还是没想到... dp[i] 表示前 i 个最少 m 个连 ...

  7. ZOJ-3725 Painting Storages DP

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3725 n个点排列,给每个点着色,求其中至少有m个红色的点连续的数 ...

  8. [ZOJ 3662] Math Magic (动态规划+状态压缩)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3662 之前写过这道题,结果被康神吐槽说代码写的挫. 的确,那时候 ...

  9. ZOJ 1234 Chopsticks(动态规划)

    Chopsticks 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=234 题目大意:给定n个筷子的长度,取k+8套筷 ...

随机推荐

  1. NEON在Android中的使用举例【转】

    转自:http://blog.csdn.net/fengbingchun/article/details/37766607 版权声明:本文为博主原创文章,未经博主允许不得转载. 1.  打开Eclip ...

  2. Java遍历Map的3种方式

    package test; import java.util.Collection; import java.util.HashMap; import java.util.Map; import ja ...

  3. Centos7 安装配置NFS

    一.安装 NFS 服务器所需的软件包 # yum install -y nfs-utils 二.编辑exports文件 # vim /etc/exports /data/disk1/video *(a ...

  4. HDU 3078:Network(LCA之tarjan)

    http://acm.hdu.edu.cn/showproblem.php?pid=3078 题意:给出n个点n-1条边m个询问,每个点有个权值,询问中有k,u,v,当k = 0的情况是将u的权值修改 ...

  5. HDU 5818:Joint Stacks(stack + deque)

    http://acm.hdu.edu.cn/showproblem.php?pid=5818 Joint Stacks Problem Description   A stack is a data ...

  6. hdwiki 在IIS 下的伪静态

    HDwiki有SEO设置的功能,此功能可以将HDwiki的页面进行URL静态化转换,从而使HDwiki内容更容易被搜索引擎挖掘,提高被收录的机率.注意事项        1.本功能对服务器环境有特殊要 ...

  7. java 编程时候的性能调优

    一.避免在循环条件中使用复杂表达式 在不做编译优化的情况下,在循环中,循环条件会被反复计算,如果不使用复杂表达式,而使循环条件值不变的话,程序将会运行的更快. 例子: import java.util ...

  8. python 购物车和三级菜单

    程序:购物车程序 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时退出,退出时,打印已购买商品和余额 ...

  9. A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏

    A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35564 Accepted: 12119 ...

  10. 山东理工大学第七届ACM校赛-字符的变化 分类: 比赛 2015-06-26 10:32 46人阅读 评论(0) 收藏

    字符的变化 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 一个长度为n(1<=n<=1000)的字符串(只包含小写字 ...