POJ3744-Scout YYF I-概率DP

(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦

题意:传送门

 原题目描述在最下面。
 长度为1e8的坐标轴,你在坐标轴点1处,有p的概率向右跳一个,1-p的概率向右跳两格。坐标轴上有n(<10)个地雷,不能踩。问安全通过这个坐标轴的概率。

思路:

dpi d p i = dpi1 d p i − 1 × × p p + dpi2 ×(1p),iϵ[1,1018] × ( 1 − p ) , i ϵ [ 1 , 10 18 ]
 因为数据范围太大不能直接递推。有两种优化方法:找规律,矩阵快速幂。
找规律:发某大神现当一段区间长度超过500(500不一定是下界,不过够用)时,概率收敛于一个定值。这样的话区间长度最多为5000,再来递推就可以了。
矩阵快速幂:

(dpi+1dpi)=(dpidpi1)×(p(1p)10) ( d p i + 1 d p i ) = ( d p i d p i − 1 ) × ( p ( 1 − p ) 1 0 )

 又有 dpari+1=dpari1×(1p) d p a r i + 1 = d p a r i − 1 × ( 1 − p )

本题还可以推通项公式:

dpi d p i = dpi1 d p i − 1 × × p p + dpi2 ×(1p) × ( 1 − p )

dpi d p i = dpi1 d p i − 1 ×(p1) × ( p − 1 )
dpi1 d p i − 1 = dpi2 d p i − 2 ×(p1) × ( p − 1 )

dpi d p i = dpi2 d p i − 2 ×(p1)2 × ( p − 1 ) 2

AC代码:
//找规律
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<assert.h>
#include<bitset>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = 9000;
int n, m;
int ar[N],vis[N];
double p, dp[N];
int main(){
#ifndef ONLINE_JUDGE
    freopen("E://ADpan//in.in", "r", stdin);
    freopen("E://ADpan//out.out", "w", stdout);  
#endif
  while(~scanf("%d%lf", &n, &p)){
    memset(ar,0,sizeof(ar));
    for(int i=1;i<=n;++i){
      scanf("%d",&ar[i]);
    }    
    sort(ar+1,ar+n+1);
    double res = 1;
    for(int i=1;i<=n;++i){
      if(ar[i]-ar[i-1]<=500)continue;
      for(int j=n;j>=i;--j){
        ar[j]-=(ar[i]-ar[i-1]-500);
      }
    }
    memset(vis,0,sizeof(vis));
    memset(dp,0,sizeof(dp));
    dp[1]=1;
    for(int i=1;i<=n;++i)vis[ar[i]]=1;
    for(int i=1;i<=ar[n];++i){
      if(vis[i])continue;
      dp[i+1]+=dp[i]*p;
      dp[i+2]+=dp[i]*(1-p);
    }
    printf("%.7f\n", dp[ar[n]+2]+dp[ar[n]+1]);
  }
  return 0;
}
//矩阵快速幂
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<assert.h>
#include<bitset>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = 9000;
int n, m;
struct lp{
  double ar[3][3];
}aa,bb;
int ar[N];
double p;
lp mul(lp a, lp b){
  lp c;
  memset(c.ar,0,sizeof(c.ar));
  for(int k=0;k<2;++k){
    for(int i=0;i<2;++i){
      if(a.ar[i][k]==0)continue;
      for(int j=0;j<2;++j){
        if(b.ar[k][j]==0)continue;
        c.ar[i][j]+=a.ar[i][k]*b.ar[k][j];
      }
    }
  }
  return c;
}
lp ksm(lp a, int b){
  lp res;
  for(int i=0;i<2;++i){
    for(int j=0;j<2;++j){
      res.ar[i][j]=(i==j);
    }
  }
  while(b){
    if(b&1)res=mul(res,a);
    a=mul(a,a);
    b>>=1;
  }
  return res;
}
/*
dp[1]=1;
p 1-p | dp[i]     -> dp[i+1]
1  0  | dp[i-1]   -> dp[i]
==>
dp[ar[i+1]-1] <- dp[ar[i]+1]  |  * *
dp[ar[i+1]-2] <- dp[ar[i]]=0  |  * *

dp[ar[i]+1]=dp[ar[i]-1]*(1-p)
*/
int main(){
#ifndef ONLINE_JUDGE
    freopen("E://ADpan//in.in", "r", stdin);
    freopen("E://ADpan//out.out", "w", stdout);  
#endif
  while(~scanf("%d%lf", &n, &p)){
    for(int i=1;i<=n;++i){
      scanf("%d",&ar[i]);
    }    
    sort(ar+1,ar+n+1);
    double res = 1;
    aa.ar[0][0]=p;aa.ar[0][1]=1-p;
    aa.ar[1][0]=1;aa.ar[1][1]=0;
    int flag=0;
    for(int i=1;i<=n;++i){
      if(ar[i]==ar[i-1]+1){
        flag=1;break;
      }
      bb=ksm(aa,ar[i]-ar[i-1]-2);
      res*=bb.ar[0][0]*(1-p);//dp[i+1]=dp[i-1]*(1-p)
    }
    if(flag) printf("%.7f\n",0.0);
    else printf("%.7f\n", res);
  }
  return 0;
}
//通项公式
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<assert.h>
#include<bitset>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = 9000;
int n, m;
int ar[N];
double p;
double ksm(double a, int b){
  double res = 1;
  while(b){
    if(b&1)res *= a;
    a *= a;
    b >>=1;
  }
  return res;
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("E://ADpan//in.in", "r", stdin);
    freopen("E://ADpan//out.out", "w", stdout);  
#endif
  while(~scanf("%d%lf", &n, &p)){
    memset(ar,0,sizeof(ar));
    for(int i=1;i<=n;++i){
      scanf("%d",&ar[i]);
    }    
    sort(ar+1,ar+n+1);
    double res = 1;
    for(int i=1;i<=n;++i){
      int k = ar[i]-ar[i-1]-1;
      res = res *(1-ksm(p-1,k))/(2-p);
      res *= (1-p);
    }
    printf("%.7f\n", res);
  }
  return 0;
}


原题目描述:

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

POJ Monthly Contest - 2009.08.23, Simon

参考博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值