题意: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. Spring Data REST不完全指南(三)

    上一篇我们介绍了使用Spring Data REST时的一些高级特性,以及使用代码演示了如何使用这些高级的特性.本文将继续讲解前面我们列出来的七个高级特性中的后四个.至此,这些特性能满足我们大部分的接 ...

  2. PHP文件包含漏洞(利用phpinfo)复现

    0x01 简介 PHP文件包含漏洞中,如果找不到可以包含的文件,我们可以通过包含临时文件的方法来getshell.因为临时文件名是随机的,如果目标网站上存在phpinfo,则可以通过phpinfo来获 ...

  3. 巧用Grafana和Arthas自动抓取K8S中异常Java进程的线程堆栈

    前言 近期发现业务高峰期时刻会出现CPU繁忙导致的timeout异常,通过监控来看是因为Node上面的一些Pod突发抢占了大量CPU导致的. 问: 没有限制CPU吗?是不是限制的CPU使用值就可以解决 ...

  4. GOLANG 闭包和普通函数的区别

    闭包和匿名函数是一回事 闭包使用完毕之后不会自动释放,值依然存在 普通函数调用完毕后,值会自动释放

  5. opencv-2-VS2017与QT显示图像

    opencv-2-VS2017与QT显示图像 opencvqtVSC++ 目的 使用 VS 构建第一个 opencv 程序 使用 QT 构建 第一个 opencv 程序 VS 导入 QT 程序 开始 ...

  6. 你知道吗?iOS不少程序常传送装置信息给第三方

    2019独角兽企业重金招聘Python工程师标准>>> 华盛顿邮报( The Washington Post)与隐私程序开发商Disconnect共同进行的研究揭露,许多iOS程序其 ...

  7. ajax 技术

    ajax 技术 $.ajax({ url:"", type:'GET', success:function(data){ console.log(data); }, error:f ...

  8. apache、nginx配置openssl自签名证书

    1.生成私钥 生成rsa私钥,des3算法,2048位强度.server.key是秘钥文件名,需要提供一个至少4位的密码. [root@localhost ~]# openssl genrsa -de ...

  9. jQuery学习(三)

    事件 on方法可以将一个事件绑定在jQuery对象上,当你的操作触发了这些事件时,便会调用你所绑定的函数. 例如,给某个超链接绑定点击事件. <head> <meta http-eq ...

  10. C#读写ini

    using System; using System.IO; using System.Runtime.InteropServices; using System.Text;   namespace ...