题意: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的更多相关文章

  1. hdu4576 概率dp n^2的矩阵

    这个题目看网上好多题解都是直接O(n*m)卡过.我是这么做的. 对于m次操作,统计每个w的次数.然后对每个w做矩阵乘法. 这样直接做矩阵乘法是会TLE的. 又由于这里的矩阵很特殊,一次乘法可以降维成O ...

  2. 「概率,期望DP」总结

    期望=Σ概率*权值 1. Codeforces 148-D 考虑用$f[i][j]$表示princess进行操作时[还剩有i只w,j只b]这一状态的存在概率.这一概率要存在,之前draw out的一定 ...

  3. BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]

    1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 4142  Solved: 1964[Submit][Statu ...

  4. 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 ...

  5. AEAI DP V3.7.0 发布,开源综合应用开发平台

    1  升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...

  6. AEAI DP V3.6.0 升级说明,开源综合应用开发平台

    AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...

  7. BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4026  Solved: 1473[Submit] ...

  8. [斜率优化DP]【学习笔记】【更新中】

    参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...

  9. BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9812  Solved: 3978[Submit][St ...

随机推荐

  1. cucumber学习索引

    Cucumber(1) —— 环境配置 Cucumber(2)——目录结构以及基本语法 Cucumber(3)——命令以及日志 Cucumber(4)——jenkins的集成

  2. 吊打面试官系列:Redis 性能优化的 13 条军规大全

    1.缩短键值对的存储长度 键值对的长度是和性能成反比的,比如我们来做一组写入数据的性能测试,执行结果如下: 从以上数据可以看出,在 key 不变的情况下,value 值越大操作效率越慢,因为 Redi ...

  3. 详解 字符串—— String、StringBuffer 与 StringBuilder

    本来这篇博文的内容,本人打算在之后的代码中一点一点通过实例讲解的,但是,本人发现,其实这里的知识点还是蛮重要的. 并且,字符串类型,在任何的程序语言中都是被认真对待的,所以,今天专门写一篇博文来介绍一 ...

  4. (转载)基于BIGINT溢出错误的SQL注入

    我对于通过MySQL错误提取数据的新技术非常感兴趣,而本文中要介绍的就是这样一种技术.当我考察MySQL的整数处理方式的时候,突然对如何使其发生溢出产生了浓厚的兴趣.下面,我们来看看MySQL是如何存 ...

  5. 二进制部署kubernetes集群_kube-apiserver提示"watch chan error: etcdserver: mvcc: required revision has been compacted'

    查看kube-apiserver状态 [root@yxz-cluster01 ~]# systemctl status kube-apiserver -l ● kube-apiserver.servi ...

  6. wincache 与 zend guard 的冲突

    ZendLoader.dll 与wincache.dll  同时开启 问题分析:zend与wincache冲突 解决方法: 关掉wincache: 在php.ini中的 extension=php_w ...

  7. 快速部署一个Kubernetes集群

    官方提供的三种部署方式 minikube Minikube是一个工具,可以在本地快速运行一个单点的Kubernetes,仅用于尝试Kubernetes或日常开发的用户使用. 部署地址:https:// ...

  8. MySQL 入门(1):查询和更新的内部实现

    摘要 在MySQL中,简单的CURD是很容易上手的. 但是,理解CURD的背后发生了什么,却是一件特别困难的事情. 在这一篇的内容中,我将简单介绍一下MySQL的架构是什么样的,分别有什么样的功能.然 ...

  9. poj_1323 Game Prediction 贪心

    Game Prediction Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11814   Accepted: 5701 ...

  10. 【JAVA基础】02 Java基础语法

    一.内容 注释 关键字 标识符 常量.进制和进制转换 变量 数据类型和类型转换 运算符 语句 二.注释 注释概述 用于解释说明程序的文字 Java中注释分类格式 单行注释 格式://注释文字 多行注释 ...