惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
J
Java Code Geeks
C
Check Point Blog
Last Week in AI
Last Week in AI
Microsoft Azure Blog
Microsoft Azure Blog
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
L
LangChain Blog
有赞技术团队
有赞技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 司徒正美
IT之家
IT之家
Martin Fowler
Martin Fowler
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
U
Unit 42
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
POJ 3744.Scout YYF|OhYee 博客
2017-08-02 · via OhYee 博客

题目

{% fold 点击显/隐题目 %}

YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's base. After overcoming a series difficulties, YYF is now at the start of enemy's famous "mine road". This is a very long road, on which there are numbers of mines. At first, YYF is at step one. For each step after that, YYF will walk one step with a probability of p, or jump two step with a probality of 1- p. Here is the task, given the place of each mine, please calculate the probality that YYF can go through the "mine road" safely.

The input contains many test cases ended with EOF. Each test case contains two lines. The First line of each test case is N (1 ≤ N ≤ 10) and p (0.25 ≤ p ≤ 0.75) seperated by a single blank, standing for the number of mines and the probability to walk one step. The Second line of each test case is N integer standing for the place of N mines. Each integer is in the range of [1, 100000000].

For each test case, output the probabilty in a single line with the precision to 7 digits after the decimal point.

1 0.5 2 2 0.5 2 4

0.5000000 0.2500000

{% endfold %}

题解

不考虑地雷的情况下,可以很容易得到递推式:
dp(i) = p*dp(i-1)+(1-p)*dp(i-2)

然而由于距离可以达到100000000,直接去计算需要非常多的内存

根据概率的知识,可以知道,能够通过每一个雷区的概率等于1-踩雷的概率
而通过整个区域的概率就是通过每个雷区的概率的乘积

根据雷区的位置,可以把整个区域分成多段,每一段有且仅有一个地雷
计算每一段的概率即可

不过每一段的长度也有可能是非常长的,要解决它有两种思路:
高中数学优化高等数学优化

对于 {% raw %}an=p×an−1+(1−p)×an−2a_n = p \times a_{n-1}+(1-p) \times a_{n-2}{% endraw %} ,求其通项公式
化简步骤如下:

{% raw %}
\begin{align*} a_n &= p \times a_{n-1} + (1-p) \times a_{n-2} \\ a_n &= (1-(1-p)) \times a_{n-1} + (1-p) \times a_{n-2} \\ a_n &= a_{n-1} - (1-p) \times a_{n-1} + (1-p) \times a_{n-2} \\ a_n - a_{n-1} &= -(1-p) (a_{n-1} - a_{n-2}) \\ \end{align*}
{% endraw %}

这时,已经可以看出等号两边的结构相等
设一个辅助数列
{% raw %}
$$
\begin{align*}
令 b_n &= a_{n+1}-a_{n} \
\
有 b_n &= (p-1) \times b_{n-1} \
可得 b_n &= b_1 \times (p-1)^n-1 \
b_{n-1} &= b_1 \times (p-1)^{n-2} \
b_{n-1} &= a_{n} - a_{n-1}\
\
a_{n} - a_{n-1} &= (a_2 - a_1) \times (p-1)^{n-2} \
a_{n-1} - a_{n-2} &= (a_2 - a_1) \times (p-1)^{n-3} \
& ...\
a_{2} - a_{1} &= (a_2 - a_1) \times (p-1)^{0} \
\
a_{n} - a_{1} &= \sum_{i=0}^{n-2} ((a_2 - a_1) \times (p-1)^i) \
a_{n} - a_{1} &= (a_2 - a_1) \times \sum_{i=0}^{n-2} (p-1)^i) \
a_{n} - a_{1} &= (a_2 - a_1) \times \frac {1 \times (1-(p-1)^{n-1})} {1-(p-1)} \
a_{n} &= a_{1} + (a_2 - a_1) \times \frac {1-(p-1)^{n-1}} {2-p} \

\
其中 a_1 &= 1 , a_2 = p\
a_{n} &= 1 + (p-1) \times \frac {1-(p-1)^{n-1}} {2-p} \

\end{align*}
$$
{% endraw %}

也即,我们有了dp的通项公式
dp[i] = 1 + (p-1)*(1-(p-1)^(n-1)/(2-p))

平方部分使用快速幂计算即可

当然,也可以使用高等数学部分的矩阵乘法

{% raw %}$
\begin{pmatrix}
p & 1-p\
1 & 0
\end{pmatrix}
\times
\begin{pmatrix}
dp[i] \
dp[i-1]
\end{pmatrix}

\begin{pmatrix}
p \times dp[i] + (1-p) \times dp[i-1] \
dp[i]
\end{pmatrix}

\begin{pmatrix}
dp[i+1] \
dp[i]
\end{pmatrix}
${% endraw %}

那么可以推出dp[i]的表达式为
{% raw %}$
\begin{pmatrix}
dp[i]\
dp[i-1]
\end{pmatrix}

\begin{pmatrix}
p & 1-p\
1 & 0
\end{pmatrix} ^ {n-2}
\times
\begin{pmatrix}
p\
1
\end{pmatrix}
${% endraw %}

使用快速矩阵幂计算即可,需要注意两个雷紧挨着的时候的情况

代码

{% fold 点击显/隐递推方法代码 %}```cpp Scout YYF递推 https://github.com/OhYee/sourcecode/tree/master/ACM 代码备份
//
#define debug
#include
//
/
#include
#include
#include
#include
#include
#include
using namespace std;

const int maxn = 15;

double pow(double a, int n) {
if (n == 0)
return 1.0;
if (n == 1)
return a;
double ans = pow(a, n / 2);
ans = ans * ans;
if (n & 1)
ans *= a;
return ans;
}
double f(int n,double p){
return 1 + (p - 1) * (1 - pow(p - 1, n - 1)) / (2 - p);
}

int mines[maxn];

int main() {
#ifdef debug
freopen("in.txt", "r", stdin);
int START = clock();
#endif
cin.tie(0);
cin.sync_with_stdio(false);
int n;
double p;
while (cin >> n >> p) {
mines[0] = 0;
for (int i = 1; i <= n; i++)
cin >> mines[i];

    sort(mines + 1, mines + 1 + n);

    double ans = 1;
    for (int i = 1; i <= n; i++) {
        int dis = mines[i] - mines[i - 1];

        double dp = f(dis,p); 
        //cout <<"\t "<<dis<<" "<< dp<<endl;
        ans *= (1 - dp);
    }

    cout << fixed << setprecision(7) << ans << endl;
}

#ifdef debug
printf("Time:%.3fs.\n", double(clock() - START) / CLOCKS_PER_SEC);
#endif
return 0;
}

{% endfold %}

{% fold 点击显/隐矩阵方法代码 %}```cpp Scout YYF矩阵  https://github.com/OhYee/sourcecode/tree/master/ACM 代码备份
/*/
#define debug
#include <ctime>
//*/
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <set>
using namespace std;

const int maxn = 15;

class Matrix {
  public:
    static const int LINE = 2; //行列数
    double matrix[LINE][LINE];

    Matrix() {
        for (int i = 0; i < LINE; i++)
            for (int j = 0; j < LINE; j++)
                matrix[i][j] = 0;
    }

    Matrix operator*(Matrix rhs) { return mul(*this, rhs); }
    Matrix operator^(int n) { return pow(*this, n); }

    static Matrix mul(Matrix a, Matrix b) {
        Matrix ans;
        for (int i = 0; i < LINE; i++)
            for (int j = 0; j < LINE; j++) {
                ans.matrix[i][j] = 0;
                for (int k = 0; k < LINE; k++)
                    ans.matrix[i][j] += a.matrix[i][k] * b.matrix[k][j];
            }
        return ans;
    }

    static Matrix pow(Matrix a, int n) {
        if (n == 0) {
            Matrix E;
            for (int i = 0; i < LINE; i++)
                E.matrix[i][i] = 1;
            return E;
        }
        if (n == 1)
            return a;
        Matrix ans = pow(a, n / 2);
        ans = ans * ans;
        if (n & 1)
            return ans * a;
        return ans;
    }
    void print() {
        for (int i = 0; i < LINE; i++) {
            printf("|");
            for (int j = 0; j < LINE; j++)
                printf("%f ", matrix[i][j]);
            printf("|\n");
        }
        printf("\n");
    }
};

int mines[maxn];

int main() {
#ifdef debug
    freopen("in.txt", "r", stdin);
    int START = clock();
#endif
    cin.tie(0);
    cin.sync_with_stdio(false);

    int n;
    double p;
    while (cin >> n >> p) {
        mines[0] = 0;
        for (int i = 1; i <= n; i++)
            cin >> mines[i];

        sort(mines + 1, mines + 1 + n);

        double ans = 1;
        for (int i = 1; i <= n; i++) {
            int dis = mines[i] - mines[i - 1];

            if (dis == 1) {
                ans = 0;
                break;
            }

            Matrix dp, pro;
            dp.matrix[0][0] = p;
            dp.matrix[1][0] = 1;

            pro.matrix[0][0] = p;
            pro.matrix[0][1] = 1 - p;
            pro.matrix[1][0] = 1;

            dp = Matrix::pow(pro, dis - 2) * dp;

            dp.print();

            ans *= (1 - dp.matrix[0][0]);
        }

        cout << fixed << setprecision(7) << ans << endl;
    }

#ifdef debug
    printf("Time:%.3fs.\n", double(clock() - START) / CLOCKS_PER_SEC);
#endif
    return 0;
}

{% endfold %}