[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 ...
随机推荐
- 基于scrapy框架输入关键字爬取有关贴吧帖子
基于scrapy框架输入关键字爬取有关贴吧帖子 站点分析 首先进入一个贴吧,要想达到输入关键词爬取爬取指定贴吧,必然需要利用搜索引擎 点进看到有四种搜索方式,分别试一次,观察url变化 我们得知: 搜 ...
- git分支,git commit,git流程
1. git分支命令规范 1. Master 主分支 2. Dev 开发分支 3. Feature 功能分支(例如:feature-x) 4. Release 预发布分支(例如:release-1.2 ...
- 解决sublime打开文档,出现中文乱码问题
sublime text 软件中出现中文乱码,大多是因为编码格式不支持,需要安装一个插件就可以解决中文乱码问题,推荐安装 ConvertToUtf8 安装步骤: 1.按“shift + ctrl + ...
- jquery动态live绑定toggle事件
$(".btn").live("click",function(){ $(this).toggle( function () { //事件 1 console. ...
- Jenkins(3)- 安装Jenkins过程中遇到问题的排查思路
如果想从头学起Jenkins的话,可以看看这一系列的文章哦 https://www.cnblogs.com/poloyy/category/1645399.html 安装Jenkins过程中,可能会遇 ...
- QFileDialog::getOpenFileName() hangs
https://forum.qt.io/topic/49209/qfiledialog-getopenfilename-hangs-in-windows-when-using-the-native-d ...
- GithubAction-Deploy
GithubAction-Deploy githubhexoaction 使用 github action 自动化部署 创建GitHub repository 存放源文件 在repo设置界面里添加Se ...
- Android 5.0系统默认颜色
伴随着 Android5.0 的发布也更新了support-v7-appcompat 到 V21,其中增加了 ToolBar.recyclerview.cardview 等控件. Android5.0 ...
- atom 之 前端必备插件
一. 语法支持 1. Language-label Ø ES2016.ESNext.JXS语法扩展 2. Language-postcss Ø Postcss语法高亮 二. 自动补全 1. Autoc ...
- web前端项目中遇到的一些问题总结(08.23更新)
个人网站 https://iiter.cn 程序员导航站 开业啦,欢迎各位观众姥爷赏脸参观,如有意见或建议希望能够不吝赐教! 写一些最近工作中Vue项目中遇到的问题. 巴啦啦小魔仙,污卡拉,全身变,小 ...