POJ-3744-概率dp+矩阵幂(分段)


Scout YYF I
Time Limit:1000MS Memory Limit:65536K
Total Submissions:10214 Accepted:2980

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 withEOF.
Each test case contains two lines.
The First line of each test case isN(1 ≤N≤ 10) andp(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

POJ Monthly Contest - 2009.08.23, Simon
    起始点在1,总共有n枚炸弹,位置位于x[i],(1<=x[i]<=1e8),每次有P的概率走一步,(1-P)的概率走两步,问安全走过所有炸弹的概率。
设f[i]表示安全到i点的概率,那么答案就是f[ max{x[i]} +1 ] ,因为每次只能走1/2,要想走过一个炸弹xi,只能在xi-1处走两步来实现(最后一次走)。
有f[i]=f[i-1]*P+f[i-2]*(1-P) ,但是N很大会超时。想到用矩阵优化,但是中间有炸弹的地方就不好处理了。对这个道路进行划分,根据炸弹的位置分为
[1,x[1]] , [x[1]+1,x[2]]......[x[n-1]+1,x[n]] ; 分成了N段,假设成功通过第i段的概率是Pi,那么答案就是∏Pi ,Pi就等价于从x[i-1]+1走到x[i]+1且最后一步走的大小是2
的概率,由于中间过程没有炸弹所以可以矩阵幂来算。
(f(n),f(n-1)) = (f(n-1),f(n-2)) * ((P,1),(1-P,0))
  注意判断特殊情况,两个炸弹相邻或者起点是炸弹那么输出0.0000000即可。
  
 1 #include<iostream>
 2 #include<cstring>
 3 #include<queue>
 4 #include<cstdio>
 5 #include<stack>
 6 #include<set>
 7 #include<map>
 8 #include<cmath>
 9 #include<ctime>
10 #include<time.h> 
11 #include<algorithm>
12 using namespace std;
13 #define mp make_pair
14 #define pb push_back
15 #define debug puts("debug")
16 #define LL long long 
17 #define pii pair<int,int>
18 #define eps 1e-12
19 
20 struct matrix{
21     double a[2][2];
22     matrix& operator*(matrix &tmp){
23         matrix ans;
24         memset(ans.a,0,sizeof(ans.a));
25         for(int i=0;i<2;++i){
26             for(int j=0;j<2;++j){
27                 for(int k=0;k<2;++k){
28                     ans.a[i][j]+=a[i][k]*tmp.a[k][j];
29                 }
30             }
31         }
32         return ans;
33     }
34 }A,E;
35 double qpow(matrix H,int b){
36     matrix ans=E;
37     while(b){
38         if(b&1) ans=ans*H;
39         H=H*H;
40         b>>=1;
41     }
42     return ans.a[0][0];
43 }
44 int main()
45 {
46     int n,m,i,j,k,t;
47     int x[15];
48     double P;
49     E.a[0][0]=E.a[1][1]=1;
50     E.a[0][1]=E.a[1][0]=0;
51     while(scanf("%d%lf",&n,&P)!=EOF){
52     A.a[0][0]=P;
53     A.a[1][0]=1.00-P;
54     A.a[0][1]=1;
55     A.a[1][1]=0;
56         for(i=1;i<=n;++i) scanf("%d",x+i);
57         sort(x+1,x+1+n);
58         bool ok=1;
59         for(i=2;i<=n;++i)
60             if(x[i]-x[i-1]==1) ok=0;
61         if(ok==0||x[1]==1){
62             printf("%.7f\n",0.0);
63             continue;
64         }
65         double ans=1;
66         ans=ans*(1-P)*qpow(A,x[1]-2);
67         for(i=2;i<=n;++i){
68             if(x[i]==x[i-1]) continue;
69             ans=ans*((double)1-P)*qpow(A,x[i]-x[i-1]-2);
70         }
71         printf("%.7f\n",ans);
72     }
73     return 0; 
74 }

长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn