ZOJ-3725 Painting Storages 动态规划
题意:给定一个数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 动态规划的更多相关文章
- [ACM] ZOJ 3725 Painting Storages (DP计数+组合)
Painting Storages Time Limit: 2 Seconds Memory Limit: 65536 KB There is a straight highway with ...
- ZOJ - 3725 Painting Storages
Description There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob ask ...
- zoj 3725 - Painting Storages(动归)
题目要求找到至少存在m个连续被染成红色的情况,相对应的,我们求至多有m-1个连续的被染成红色的情况数目,然后用总的数目将其减去是更容易的做法. 用dp来找满足条件的情况数目,, 状态:dp[i][0] ...
- ZOJ 3725 Painting Storages(DP+排列组合)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5048 Sample Input 4 3 Sample Output ...
- Painting Storages(ZOJ)
There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob asks you to pai ...
- zoj 3725
题意: n个格子排成一条直线,可以选择涂成红色或蓝色,问最少 m 个连续为红色的方案数. 解题思路: 应该是这次 ZOJ 月赛最水的一题,可惜还是没想到... dp[i] 表示前 i 个最少 m 个连 ...
- ZOJ-3725 Painting Storages DP
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3725 n个点排列,给每个点着色,求其中至少有m个红色的点连续的数 ...
- [ZOJ 3662] Math Magic (动态规划+状态压缩)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3662 之前写过这道题,结果被康神吐槽说代码写的挫. 的确,那时候 ...
- ZOJ 1234 Chopsticks(动态规划)
Chopsticks 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=234 题目大意:给定n个筷子的长度,取k+8套筷 ...
随机推荐
- Linux下通过crontab及expect实现自动化处理
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 目标 为实现每天定时从其他服务器上复制文件到本地,需要使用crontab建立定时任务,并通过scp进行Linux之间的文件复制. ...
- oralce创建用户
oralce创建用户: sqlplus /nolog回车 -->conn esun/esun@esuntech; -->create user xlh identified by pass ...
- Hive报错之java.sql.SQLException: Field 'IS_STOREDASSUBDIRECTORIES' doesn't have a default value
在创建表的时候报出如下错误: hive> create table if not exists testfile_table( > site string, > url string ...
- ecshop添加商品选择品牌时如何按拼音排序
ECSHOP后台添加新商品时,有一个选择品牌的下拉框,如果品牌太多,在下拉框里查找起来很不方便. 我想给“下拉框里的品牌列表”按品牌名的拼音排序,比如有“中国水利出版社” “中国人民出版社” 这两个品 ...
- 主线程中创建不同的handler实例,接收消息会不会冲突
http://www.cnblogs.com/transmuse/archive/2011/05/16/2048073.html这篇博文讲的比较透彻,可参考. 当然结论是不会冲突.因为每个messag ...
- Spring+Quartz实现定时执行任务的配置
1.要想使用Quartz 必须要引入相关的包:以下是我在项目中gradle中的配置: compile 'org.quartz-scheduler:quartz:2.1.1' 2.Scheduler的配 ...
- 【转】MySQL5安装的图解(mysql-5.0.27-win32.zip)
转载地址:http://blog.csdn.net/xssh913913/article/details/1713182 MySQL5安装的图解(最新版) http://hi.baidu.com/yu ...
- Proud Merchants
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) To ...
- 花40分钟写一个-CBIR引擎-代码公开
浏览网页的时候发现一篇不错的文章"用Python和OpenCV创建一个图片搜索引擎的完整指南"http://python.jobbole.com/80860/.作者在浏 ...
- CyclicBarrier原理
转载:http://www.cnblogs.com/skywang12345/p/3533995.html CyclicBarrier是一个同步辅助类,允许一组线程互相等待,直到到达某个公共屏障点 ( ...