POJ 3744 Scout YYF I 简单递推

题意就是

一个人, 站在坐标为1的点处,然后每次走路有p的概率一下走出去坐标1,1-p的概率一下走出去坐标2

路上某些点(n < 10)有雷,问这个人最终迈过这些雷不被炸的概率是多大

想一下就知道这些雷之间实际上是独立不相关的

可以分段考虑

然后互相之间乘一下就行

假设有个雷在x点

现在人在坐标1

然后不踩雷就得从1点到x-1点

并且从x-1点迈出坐标2到x+1

从x-1迈出坐标2到x+1的概率是1-p

之前1到x-1这段是没有任何雷的。就可以进行普通的递推了

a_n = a_(n-1) * p + a_(n-2) * (1-p)

这个我们可以用矩阵乘法

或者求出通项

a_n - a_(n-1) * p -a_(n-2) * (1-p) = 0

特征方程是n^2 - p *n - (1-p) = 0

求出两个解,1, p - 1

a_n = A + B * (p - 1) ^ n

带入a_0 = 1 , a_1 = p

得a_n = 1/(2-p) + (1-p) / (2-p) * (p - 1 ) ^n

即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <map>
#include <ctime>
#define MAXN 222222
#define MAXM 222222
#define INF 1000000001
using namespace std;
double p;
int n;
int a[15];
double ans;
double getan(int x) {
    return 1.0 / (2.0 - p) + (1.0 - p) / (2.0 - p) * pow(p - 1.0, x);
}
int main() {
    while(scanf("%d%lf", &n, &p) != EOF) {
        for(int i = 0; i < n; i++) {
            scanf("%d", &a[i]);
        }
        sort(a, a + n);
        int now = 1;
        ans = 1;
        for(int i = 0; i < n; i++) {
            int nxt = a[i];
            if(a[i] >= now + 1) {
                int dis = a[i] - now - 1;
                ans = ans * getan(dis) * (1.0 - p);
                now = a[i] + 1;
            } else {
                ans = 0;
                break;
            }
        }
        printf("%.7f\n", ans);
    }
    return 0;
}
时间: 2024-11-11 01:02:35

POJ 3744 Scout YYF I 简单递推的相关文章

POJ 3744 Scout YYF I 矩阵快速幂

Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4452   Accepted: 1159 Description 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,

poj 3744 Scout YYF I(概率dp,矩阵优化)

Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5020   Accepted: 1355 Description 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,

POJ 3744 Scout YYF I(矩阵快速幂 概率dp)

题目链接:http://poj.org/problem?id=3744 Description 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"

POJ 3744 Scout YYF I:概率dp

题目链接:http://poj.org/problem?id=3744 题意: 有n个地雷,位置为pos[i]. 在每个位置,你向前走一步的概率为p,向前走两步的概率为1-p. 你的初始位置为1. 问你通过雷区的概率. 题解: 表示状态: dp[i] = probability moving to i 表示走到i的概率 找出答案: ans = dp[last_mine+1] last_mine:最右边一颗雷的位置 如何转移: dp[i] = dp[i-1] * p + dp[i-2] * (1-

poj 3744 Scout YYF I(矩阵优化概率DP)

Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5153   Accepted: 1404 Description 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,

POJ 3744 Scout YYF I 矩阵快速幂优化--概率dp

点击打开链接 Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5416   Accepted: 1491 Description 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 diffic

poj 3744 Scout YYF I 概率dp+矩阵乘法

分析: dis(k,v1,v2)函数求到当前位置概率为v1,到当前位置之前一步的概率为v2,前进k步到达位置的概率,然后矩阵加速. 代码: //poj 3744 //sep9 #include <iostream> #include <algorithm> using namespace std; int pos[12]; double p,mat[4][4]; double ans[4][4]; void mul1() { double c[4][4]; c[0][1]=c[1]

poj 3744 Scout YYF I(矩阵优化概率)

http://poj.org/problem?id=3744 有n个雷,某人的起始位置在1,每次走一步的概率为p,走两步的概率是1-p,给出n个雷的位置,问最后成功走出雷区的概率. 放在高中应该是很简单的分步乘法求概率.即把每一个雷都没踩到的概率求出来,最后n个相乘就是顺利通过的概率.对于输入的n个位置进行分段1~num[1],num[1]+1~num[2]......每一段都只有一个雷num[i],每一段内踩不到雷的概率就是1-踩到num[i]雷的概率. 设dp[i]表示踩到第i个雷的概率,那

poj 3744 Scout YYF I (矩阵)

Description 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