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. MySQL子查询(SubQuery)

    由比较运算符引发的子查询,若括号内的子查询结果为多个,要在括号前加上ANY/SOME/ALL 由[NOT]IN引发的子查询, =ANY与IN等效       !=ALL / <>ALL与N ...

  2. bzoj 1467: Pku3243 clever Y 扩展BSGS

    1467: Pku3243 clever Y Time Limit: 4 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description 小 ...

  3. 几个移动App测试工具

    介绍几款移动App测试的工具: 腾讯测试:http://bugly.qq.com/优测:http://utest.qq.com/fir.im测试:http://bughd.com/ 大致介绍如下: b ...

  4. asp.net MVC开发过程中,使用到的方法(内置方法及使用说明)

    ® 视图的返回使用案例: [HttpGet] [SupportFilter] public ActionResult UserTopic(string type, string TopPicId, s ...

  5. Python类、模块、包的区别

    类 类的概念在许多语言中出现,很容易理解.它将数据和操作进行封装,以便将来的复用. 模块 模块,在Python可理解为对应于一个文件.在创建了一个脚本文件后,定义了某些函数和变量.你在其他需要这些功能 ...

  6. 如何定位摄像机,使物体在屏幕上始终具有相同的像素宽度和高度?(threes)

    from How to position the camera so that the object always has the same pixel width and height on the ...

  7. hibernate的离线关联(多级)查询

    如果实体对象中没有关联对象的情况使用DetachedCriteria进行查询是一件很简单的事情. 假设要通过stuName查询一个学生Student记录,可以如下: Java代码 DetachedCr ...

  8. zookeeper错误

    ZooKeeper JMX enabled by defaultUsing config: /data4/hadoop/zookeeper-3.4.8/bin/../conf/zoo.cfgError ...

  9. php正则:匹配(),{},[]小括号,大括号,中括号里面的内容

    php正则:匹配(),{},[]小括号,大括号,中括号里面的内容 比如有一段字符: $s='60c8 {"code":"200","message&q ...

  10. graph-tool文档(一)- 快速开始使用Graph-tool - 3.图的过滤

    目录: 图的过滤 图视图 -- 组合图视图 名词解释: filter:过滤 mask:屏蔽 inverted parameter:倒参数 overhead:开销 minimum spanning tr ...