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. [SAP ABAP开发技术总结]字符串处理函数、正则表达式

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  2. LINQ 简单用法【1】

    LINQ:Language INtegrated Query,语言集成查询. 以下内容演示如何利用LINQ进行增加,修改,删除和查询操作,针对数据库. 首先创建Linq Class. 添加数据库信息, ...

  3. MySQL多表更新(逻辑外键/事实外键)

    语法结构: UPDATE  table_reference  SET  列名1=value1[,列名2=value2,......] [WHERE  where_condition] 说明: tabl ...

  4. LTE Module User Documentation(翻译2)——配置LTE MAC 调度器

    LTE用户文档 (如有不当的地方,欢迎指正!) 5 配置 LTE MAC 调度器   这里有几种 LTE MAC 调度器用户可以选择.使用下面的代码定义调度器的类型: Ptr<LteHelper ...

  5. NOJ 1063 生活的烦恼

    描述 生活的暑假刚集训开始,他要决心学好字典树,二叉树,线段树和各种树,但生活在OJ上刷题的时候就遇到了一个特别烦恼的问题.那当然就是他最喜欢的二二叉树咯!题目是这样的:给你一颗非空的二叉树,然后再给 ...

  6. ZOJ-2362 Beloved Sons 最大权值匹配

    题意:国王有N个儿子,现在每个儿子结婚都能够获得一定的喜悦值,王子编号为1-N,有N个女孩的编号同样为1-N,每个王子心中都有心仪的女孩,现在问如果安排,能够使得题中给定的式子和最大. 分析:其实题目 ...

  7. iOS - Swift Set 集合

    前言 Set:集合 public struct Set<Element : Hashable> : Hashable, CollectionType, ArrayLiteralConver ...

  8. QQLogin

    import java.awt.*; import javax.swing.*; public class QQLogin extends JFrame{ QQLogin(){ this.setSiz ...

  9. jQuery扩展插件和拓展函数的写法

    <script type="text/JavaScript">            //jQuery插件的写法(需要传入操作对象)        ;(function ...

  10. Jdbc入门

    JDBC入门 l  导jar包:驱动! l  加载驱动类:Class.forName(“类名”); l  给出url.username.password,其中url背下来! l  使用DriverMa ...