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

来源:互联网 发布:征服者升级软件 编辑:程序博客网 时间:2024/05/16 16:55
Scout YYF I
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4569 Accepted: 1195

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 ofp, 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.522 0.52 4

Sample Output

0.50000000.2500000
 
 
题解详见:http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710586.html
 
AC代码:
#include <iostream>#include <cstring>#include <cstdio>#include <queue>#include <cmath>#include <algorithm>#define ll long longusing namespace std;const int INF = 1e9;const int maxn = 1000005;const int mod = 100000000;int n;double p;int mine[15];struct Mat{    double mat[2][2];    void zero(){        memset(mat, 0, sizeof(mat));    }    void unit(){        zero();        for(int i = 0; i < 2; i++) mat[i][i] = 1;    }}A, T;Mat operator * (const Mat &a, const Mat & b){    Mat tmp;    for(int i = 0; i < 2; i++)    for(int j = 0; j < 2; j++)    {        double sum = 0;        for(int k = 0; k < 2; k++)        sum += a.mat[i][k] * b.mat[k][j];        tmp.mat[i][j] = sum;    }    return tmp;}Mat operator ^ (Mat x, int m){    Mat tmp;    tmp.unit();    while(m)    {        if(m & 1) tmp = tmp * x;        x = x * x;        m >>= 1;    }    return tmp;}void init(){    T.zero();    T.mat[0][0] = p, T.mat[0][1] = 1 - p, T.mat[1][0] = 1;}int main(){    while(~scanf("%d%lf", &n, &p))    {        init();        for(int i = 0; i < n; i++)        scanf("%d", &mine[i]);        sort(mine, mine + n);        double ans = 1;        Mat tmp = T ^ (mine[0] - 1);        ans *= (1 - tmp.mat[0][0]);        for(int i = 1; i < n; i++)        {            if(mine[i] == mine[i - 1]) continue;            tmp = T ^ (mine[i] - mine[i - 1] - 1);            ans *= (1 - tmp.mat[0][0]);        }        printf("%.7f\n", ans);    }    return 0;}

0 0
原创粉丝点击