Scout YYF I
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5565   Accepted: 1553

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

 
此题为概率dp
1.求概率:
有N个地雷,分段处理,求每段踩中地雷的概率P,则未踩中的概率为1 - P,再分别相乘。
每段踩中地雷的概率递推式为:dn = p * dn-1 + (1 - p) * dn - 2;
2.对递推式的处理:
由于数据太大,直接递推求解容易tle,所以将递推式转化为矩阵形式(方法http://www.cnblogs.com/sunus/p/4404273.html),再用矩阵快速幂处理。
注意:最终得[dn; dn-1] = [p, 1-p; 1, 0]^(n - 1) * [d1; d0];
d1 = p;
d0 = 1;
因此,[dn; dn-1] = [p, 1-p; 1, 0]^(n - 1) * [p; 1];
因此,dn =  ([p, 1-p; 1, 0]^n)[0][0];
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 55
#define repu(i, a, b) for(int i = a; i < b; i++)
using namespace std;
#define MAXN 2
#define ll double
ll base[MAXN][MAXN] = {{1.0, 0.0}, {0.0, 1.0}};
int ma[N]; struct Matrix
{
ll m[MAXN][MAXN]; //二维数组存放矩阵
Matrix(ll num[MAXN][MAXN])
{
for(int i = ; i < MAXN ; i++)
for(int j = ; j < MAXN ; j++)
m[i][j] = num[i][j];
} //对数组的初始化
Matrix() {}
}; Matrix operator * (Matrix m1, Matrix m2)
{
int i, j, k;
Matrix temp;
for (i = ; i < MAXN; i++)
{
for (j = ; j < MAXN; j++)
{
temp.m[i][j] = ;
for(k = ; k < MAXN ; k++)
temp.m[i][j] += (m1.m[i][k] * m2.m[k][j]);// % mod;
//temp.m[i][j] %= mod; //注意每一步都进行取模
}
}
return temp;
} Matrix quickpow(Matrix M, int n)
{
Matrix tempans(base); //初始化为单位矩阵
while(n)
{
if(n & )
tempans = tempans * M; //已经重载了*
n = n >> ;
M = M * M;
} //快速幂(类似整数)
return tempans;
} int main()
{
int n;
ll p;
while(~scanf("%d", &n))
{
scanf("%lf", &p);
Matrix M, C;
M.m[][] = p;
M.m[][] = 1.0 - p;
M.m[][] = 1.0;
M.m[][] = 0.0;
int last, rear;
double P = 1.0;
ma[] = ;
repu(i, , n + )
scanf("%d", &ma[i]);
sort(ma, ma + n + );
repu(i, , n + )
{
C = quickpow(M, ma[i] - ma[i - ] - );
P *= (1.0 - C.m[][]);
}
if(ma[] == ) P = 0.0;
printf("%.7lf\n", P);
}
return ;
}

Scout YYF I(POJ 3744)的更多相关文章

  1. [Poj3744]Scout YYF I (概率dp + 矩阵乘法)

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

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

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

  3. 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)

    Charm Bracelet    POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...

  4. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

  5. Games:取石子游戏(POJ 1067)

    取石子游戏 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37662   Accepted: 12594 Descripti ...

  6. BFS 或 同余模定理(poj 1426)

    题目:Find The Multiple 题意:求给出的数的倍数,该倍数是只由 1与 0构成的10进制数. 思路:nonzero multiple  非零倍数  啊. 英语弱到爆炸,理解不了题意... ...

  7. 并查集+关系的传递(poj 1182)

    题目:食物链 题意:给定一些关系.判断关系的正确性,后给出的关系服从之前的关系: 思路:难点不在并查集,在于关系的判断,尤其是子节点与根节点的关系的判断: 这个关系看似没给出,但是给出子节点与父节点的 ...

  8. 昂贵的聘礼(poj 1062)

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...

  9. Collecting Bugs(POJ 2096)

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 3064   Accepted: 1505 ...

随机推荐

  1. firefox渗透师必备的利器

    工欲善必先利其器,firefox一直是各位渗透师必备的利器,小编这里推荐34款firefox渗透测试辅助插件,其中包含渗透测试.信息收集.代理.加密解密等功能. 1:Firebug Firefox的 ...

  2. eclupse启动报 Failed to load JavaHL Library.错

    解决办法: window --> preferences --> Team --> SVN --> Client选项选择: SVNKit x.x.x.xxx

  3. SQL GUID和自增列做主键的优缺点

    我们公司的数据库全部是使用GUID做主键的,很多人习惯使用int做主键.所以呢,这里总结一下,将两种数据类型做主键进行一个比较. 使用INT做主键的优点: 1.需要很小的数据存储空间,仅仅需要4 by ...

  4. Ubuntu下手动安装VMware Tools步骤

    To mount the CD image and extract the contents: Power on the virtual machine. Log in to the virtual ...

  5. So easy Webservice 8.spring整合CXF 发布WS

    1.添加jar包(cxf的jar包中包含了spring的jar包),添加spring配置文件 2.web.xml中配置CXFServlet,过滤WS服务的地址 <!-- 配置CXFServlet ...

  6. 泛型IComparer<T>排序

    class Program { static void Main(string[] args) { GetListTest(); } private static void GetListTest() ...

  7. 大学生学习编程(PHP)

    在v2ex上看到一大三的求职实习,然后有人给出了建议,个人觉得也挺好,做个记录./  原帖地址 @ARjson问: 大三的学生党,求实习岗位,现居北京.后端PHP一年半开发经验,熟悉speedphp, ...

  8. linux登录mysql

    mysql  -u 用户名 -p密码 mysql -u root -psqj888

  9. LTIB常用命令1

    下面再写一点ltib的常用命令参数吧,虽然觉得对其编译内核和文件系统流程有了一定了解,但是对其命令参数用过的还不是很多,可以说是不甚了解,下面介绍一些,希望有用: 首先一个比较有用的命令参数就是hel ...

  10. intel simd 资料

    http://www.cnblogs.com/zyl910/archive/2012/04/26/md00.html https://software.intel.com/sites/landingp ...