POJ 3744 概率 + 分段 + 数学推导 + 快速幂

来源:互联网 发布:黑马程序员 课程表 编辑:程序博客网 时间:2024/05/01 00:42

传送门 :

题解

这里把每两个mine之间分段, 每两个mine之间安全走过到末尾概率pi分别求出(快速幂), 越过的概率是pi*(1 - p)(一次走两步)
推导过程
这里写图片描述

AC code:

#include<iostream>#include<cstring>#include<cmath>#include<iomanip>#include<cstdio>#include<algorithm>using namespace std;int x[30], n;double p, ans;double quick_multi(int b) {    double res = 1.0, tmp = p - 1.0;    while (b) {        //cout << res << endl;        if (b & 1) res *= tmp;        b >>= 1;        tmp *= tmp;    }    return res;}int main() {    //freopen("in.txt", "r", stdin);    while (cin >> n >> p) {        for (int i = 1; i <= n; ++i) cin >> x[i];        x[0] = 0;        ans = 1;        sort(x, x + n + 1);        for (int i = 1; i <= n; ++i) {            int t = x[i] - x[i - 1] - 1;//分段快速幂            ans *= (1.0 - quick_multi(t)) / (2.0 - p);            if (fabs(ans) < 1e-6) break;//概率为0直接退出就可以了            ans *= 1.0 - p;//越过x[i]处的mine        }        cout << fixed << setprecision(7) << ans << endl;    }    return 0;}
0 0