[hdu4576]dp
题意:1-n围成1圈,从1出发,第i次走a[i]步,问走m次后出现在[L,R]的概率L<=R。
思路:明显的DP,把编号变成0~n-1,令dp[i][j]表示走完i步之前停在了j上,则有dp[i][j] * 0.5 -> dp[i+1][(j+a[i])%n] 和 dp[i+1][(j-a[i]+n*a[i])%n]。由于取模运算的大量存在,直接算会TLE,需要预处理取模的结果。时间复杂度O(nm)。
代码1:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <vector>using namespace std;typedef long long LL;#define mem0(a) memset(a, 0, sizeof(a))double dp[2][234];int mod1[234][234], mod2[234][234];int a[1234567];int main() {#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin);#endif // ONLINE_JUDGE int n, m, l, r; while (cin >> n >> m >> l >> r, n) { for (int i = 0; i < m; i ++) scanf("%d", a + i); mem0(dp); dp[0][0] = 1; for (int i = 0; i < 201; i ++) { for (int j = 0; j < 201; j ++) { mod1[i][j] = (i + j) % n; mod2[i][j] = (i - j + n * j) % n; } } int cur = 1; for (int i = 0; i < m; i ++) { for (int j = 0; j < n; j ++) { dp[cur][mod1[j][a[i]]] += dp[cur ^ 1][j] * 0.5; dp[cur][mod2[j][a[i]]] += dp[cur ^ 1][j] * 0.5; } cur ^= 1; mem0(dp[cur]); } double ans = 0; for (int i = l - 1; i < r; i ++) { ans += dp[cur ^ 1][i]; } printf("%.4f\n", ans); } return 0;} |
另一个思路(没A,应该是精度问题):m次走的顺序是不会影响最终的结果的,所以考虑把相同的步数和并,由于步数范围在1-100,所以把m次走的过程分为了最多100个阶段,如果我们预处理每个阶段从0到任意点的概率(最多n个) ,那么就可以在O(1)的时间完成点到点的转移。时间复杂度变成O(m + k * n * n).
代码2:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <vector>using namespace std;typedef long long LL;#define mem0(a) memset(a, 0, sizeof(a))int cnt[123];double p[123][234], dp[123][234];int main() {#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin);#endif // ONLINE_JUDGE int n, m, l, r; while (cin >> n >> m >> l >> r, n) { mem0(cnt); mem0(dp); mem0(p); for (int i = 0; i < m; i ++) { int x; scanf("%d", &x); cnt[x] ++; } for (int x = 1; x <= 100; x ++) { int tot = cnt[x]; if (tot == 0) continue; double buf = 1.0; for (int i = 0; i < tot; i ++) buf /= 2; for (int i = 0; i <= tot; i ++) { p[x][((LL)x * (tot - 2 * i) + (LL)n * tot * x) % n] += buf; buf *= ((double)tot - i) / (i + 1); } } mem0(dp); dp[0][0] = 1; int cur = 0; for (int x = 1; x <= 100; x ++) { if (cnt[x] == 0) continue; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dp[cur + 1][(i + j) % n] += p[x][j] * dp[cur][i]; } } cur ++; } double ans = 0; for (int i = l - 1; i < r; i ++) ans += dp[cur][i]; printf("%.4f\n", ans); } return 0;} |
[hdu4576]dp的更多相关文章
- hdu4576 概率dp n^2的矩阵
这个题目看网上好多题解都是直接O(n*m)卡过.我是这么做的. 对于m次操作,统计每个w的次数.然后对每个w做矩阵乘法. 这样直接做矩阵乘法是会TLE的. 又由于这里的矩阵很特殊,一次乘法可以降维成O ...
- 「概率,期望DP」总结
期望=Σ概率*权值 1. Codeforces 148-D 考虑用$f[i][j]$表示princess进行操作时[还剩有i只w,j只b]这一状态的存在概率.这一概率要存在,之前draw out的一定 ...
- BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]
1911: [Apio2010]特别行动队 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 4142 Solved: 1964[Submit][Statu ...
- 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...
- AEAI DP V3.7.0 发布,开源综合应用开发平台
1 升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...
- AEAI DP V3.6.0 升级说明,开源综合应用开发平台
AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...
- BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4026 Solved: 1473[Submit] ...
- [斜率优化DP]【学习笔记】【更新中】
参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...
- BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 9812 Solved: 3978[Submit][St ...
随机推荐
- 不同目录有同名proto文件情况下,protoc生成.cc/.h
首先先参考一下别人的博客,看完了,看懂了,再回过头来看我下面说的情况. 链接 https://blog.csdn.net/CAir2/article/details/78201572 但是这个也就是基 ...
- Laravel 5.8 RCE 分析
原帖地址 : https://xz.aliyun.com/t/6059 Laravel 代码审计 环境搭建 composer create-project --prefer-dist laravel/ ...
- 新手想掌握Python技能需要众多Python项目练习,适合项目有哪些?
适合新手练习的Python项目有哪些?简单易上手的Python项目汇总:Web 项目设计:内容聚合器.正则表达式查询工具.网址缩短.便利贴.功能.测验.GUI 项目设计:MP3 播放器.闹铃提醒工具. ...
- LeetCode 面试题51. 数组中的逆序对
面试题51. 数组中的逆序对 题目来源:https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof/ 题目 在数组中的两个数字,如果 ...
- python学习笔记(三)---字典
字典 在Python中,字典 字典 是一系列键 键-值对 值对 .每个键 键 都与一个值相关联,你可以使用键来访问与之相关联的值.与键相关联的值可以是数字.字符串.列表乃至字典.事实上,可将 任何Py ...
- token认证和理解
认知篇:https://blog.csdn.net/FYGu18/article/details/89345490 token失效篇认知:https://segmentfault.com/q/1010 ...
- 2019-2020-1 20199303 《Linux内核原理与分析》 第十一周作业
缓冲区溢出漏洞实验 安装一些用于编译C程序的32位软件包 sudo apt-get install -y lib32z1 libc6-dev-i386 sudo apt-get install -y ...
- 二, 连接Oracle 二
一,sqlplus操作 文件操作命令 1.start和@ 说明: 运行sql脚本 案例: sql>@ home/a.sql或是sql>start home/a.sql 2.edit 说明: ...
- Codeforces Round #632 (Div. 2) 题解
空山新雨后,天气晚来秋. 明月松间照,清泉石上流. 竹喧归浣女,莲动下渔舟. 随意春芳歇,王孙自可留.--王维 A. Little Artem 网址:https://codeforces.com/co ...
- UVALive 7501 Business Cycle
细心题 #include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<=b;++i) # ...