Scout YYF I
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6757   Accepted: 1960

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

Input

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

Output

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

Sample Input

1 0.5
2
2 0.5
2 4

Sample Output

0.5000000
0.2500000

Source

 
题意:在一条不满地雷的路上,你现在的起点在1处。在N个点处布有地雷,1<=N<=10。地雷点的坐标范围:[1,100000000].
每次前进p的概率前进一步,1-p的概率前进1-p步。问顺利通过这条路的概率。就是不要走到有地雷的地方。
 
设dp[i]表示到达i点的概率,则 初始值 dp[1]=1.
很容易想到转移方程: dp[i]=p*dp[i-1]+(1-p)*dp[i-2];
但是由于坐标的范围很大,直接这样求是不行的,而且当中的某些点还存在地雷。
 
N个有地雷的点的坐标为 x[1],x[2],x[3]```````x[N].
我们把道路分成N段:
1~x[1];
x[1]+1~x[2];
x[2]+1~x[3];
`
`
`
x[N-1]+1~x[N].
 
这样每一段只有一个地雷。我们只要求得通过每一段的概率。乘法原理相乘就是答案。
对于每一段,通过该段的概率等于1-踩到该段终点的地雷的概率。
 
就比如第一段 1~x[1].  通过该段其实就相当于是到达x[1]+1点。那么p[x[1]+1]=1-p[x[1]].
但是这个前提是p[1]=1,即起点的概率等于1.对于后面的段我们也是一样的假设,这样就乘起来就是答案了。
 
对于每一段的概率的求法可以通过矩阵乘法快速求出来。
 
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
struct Mat
{
double mat[][];
};
int x[],n;
Mat operator*(Mat a,Mat b)
{
Mat c;
for(int i = ; i < ; i++)
{
for(int j = ; j < ; j++)
c.mat[i][j] = ;
}
for(int i = ; i < ; i++)
{
for(int j = ; j < ; j++)
{
for(int k = ; k < ; k++)
c.mat[i][j] += a.mat[i][k] * b.mat[k][j];
}
}
return c;
}
Mat operator^(Mat a, int k)
{
Mat c;
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
c.mat[i][j] = (i == j);
while(k)
{
if(k % )
c = c * a;
a = a * a;
k = k / ;
}
return c;
}
int main()
{
double p;
while(scanf("%d%lf", &n,&p) != EOF)
{
for(int i = ; i < n; i++)
scanf("%d", &x[i]);
sort(x, x + n);
Mat tt,temp;
temp.mat[][] = tt.mat[][] = p;
temp.mat[][] = tt.mat[][] = - p;
temp.mat[][] = tt.mat[][] = ;
temp.mat[][] = tt.mat[][] = ; temp = tt ^ (x[] - );
double ans = ;
ans *= ( - temp.mat[][]);
for(int i = ; i < n; i++)
{
if(x[i] == x[i - ])
continue;
temp = tt ^ (x[i] - x[i - ] - ); // 因为要从x[i - 1] + 1开始走,x[i - 1] 是雷
ans *= ( - temp.mat[][]);
}
printf("%0.7lf\n", ans);
}
return ;
}
 

POJ3744Scout YYF I(求概率 + 矩阵快速幂)的更多相关文章

  1. 刷题总结—— Scout YYF I(poj3744 矩阵快速幂+概率dp)

    题目: Description YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate int ...

  2. Scout YYF I (概率+矩阵快速幂)

    YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's ba ...

  3. 【XSY2612】Comb Avoiding Trees 生成函数 多项式求逆 矩阵快速幂

    题目大意 本题的满二叉树定义为:不存在只有一个儿子的节点的二叉树. 定义一棵满二叉树\(A\)包含满二叉树\(B\)当且经当\(A\)可以通过下列三种操作变成\(B\): 把一个节点的两个儿子同时删掉 ...

  4. HDU4565-数学推导求递推公式+矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 我们带着这个根号是没法计算的 我们仔细观察一下,(a+sqrt(b))^n用二项式定理展开,我 ...

  5. Just Oj 2017C语言程序设计竞赛高级组A: 求近似值(矩阵快速幂)

    A: 求近似值 时间限制: 1 s      内存限制: 128 MB 提交 我的状态 题目描述 求⌊(5–√+6–√)2n⌋⌊(5+6)2n⌋%9932017. 例如:n=1,(5–√+6–√)2( ...

  6. 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 Coin 概率+矩阵快速幂

    题目链接: https://nanti.jisuanke.com/t/17115 题意: 询问硬币K次,正面朝上次数为偶数. 思路: dp[i][0] = 下* dp[i-1][0] + 上*dp[i ...

  7. HDU 3221 矩阵快速幂+欧拉函数+降幂公式降幂

    装载自:http://www.cnblogs.com/183zyz/archive/2012/05/11/2495401.html 题目让求一个函数调用了多少次.公式比较好推.f[n] = f[n-1 ...

  8. 2019牛客多校第五场 B - generator 1 矩阵快速幂+十倍增+二进制倍增优化

    B - generator 1 题意 给你\(x_{0}.x_{1}.a.b.b.mod\),根据\(x_{i} = a*x_{i-1} + b*x_{i-2}\)求出\(x_{n}\) 思路 一般看 ...

  9. poj4474 Scout YYF I(概率dp+矩阵快速幂)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4100   Accepted: 1051 Descr ...

随机推荐

  1. 六、动态增加方法Category

    1.概念:Category可以动态为已经存在的类增加一个方法,可以不改动原有的类. 2. 如何创建一个Category类创建一个文件,选择Objective-C category,点next取名时,要 ...

  2. 如何将list转为json?

  3. Windows客户端C/C++编程规范“建议”——风格

    本文来自:http://blog.csdn.net/breaksoftware/article/details/37935459 命名风格也非常适用于C# 9 风格 9.1 优先使用匈牙利命名法 等级 ...

  4. 微软职位内部推荐-SW Engineer II for Skype

    微软近期Open的职位: We are the Skype Beijing team. Skype division drives the communications strategy for Mi ...

  5. 开源(免费)三维 GIS(地形,游戏)

    先写想法: 想做个简单的地形漫游,于是考虑在ww直接开发,或找个控件形式的开发组件. 最大的期望有: 1. 支持google的sketchup,快速智能三维建模 2. 设计模式做好点,最好先做成组件形 ...

  6. cannot change version web module 3.0

    eclipse如何修改dynamic web module version 由于从SVN down下来的工程java及tomcat 版本比本地高,导致工程不能编译,报以下错误. 1.Java comp ...

  7. 基于Html5的移动端开发框架的研究

    下面统计信息部分来自网络,不代表个人观点.请大家参考.         基于Html5移动端开发框架调查                                   序号 框架 简介 优点 缺 ...

  8. [CareerCup] 6.2 Dominos on Chess Board 棋盘上的多米诺

    6.2 There is an 8x8 chess board in which two diagonally opposite corners have been cut off. You are ...

  9. [CareerCup] 7.6 The Line Passes the Most Number of Points 经过最多点的直线

    7.6 Given a two-dimensional graph with points on it, find a line which passes the most number of poi ...

  10. PLC梯形图编程练习

    在PLC培训软件中完成如下两个练习,并把对应的梯形图程序发表在博客上. 交通灯控制 在如下图的场景中,打开SW1开关后,交通灯控制器开始工作,关闭SW1则控制器停止工作. SW2为控制模式选择开关: ...