bzoj4008[HNOI2015]亚瑟王

http://www.lydsy.com/JudgeOnline/problem.php?id=4008

我们只需要求每张牌发动的概率$P[i]$,然后乘上每张牌的伤害值$d[i]$即可。

记$f[i][j]$表示,在这$r$轮游戏中,有$j$轮游戏在第$i$张牌或第$i$张牌之前已经结束的概率。

那么还有$r-j$轮游戏在第$i$张牌时还没结束。

考虑第$i+1$张牌

如果这剩下的$r-j$轮游戏中,第$i+1$张牌都没有发动:

概率是$(1-p[i+1])^{r-j}$

转移到$f[i+1][j]$:

$f[i+1][j]+=f[i][j]*(1-p[i+1])^{r-j}$

如果这剩下的$r-j$轮游戏中,第$i+1$张牌在其中一局中发动了:

概率是$1-(1-p[i+1])^{r-j}$

转移到$f[i+1][j+1]$:

$f[i+1][j+1]+=f[i][j]*\{1-(1-p[i+1])^{r-j}\}$

这时候第$i+1$张牌的概率$p[i+1]$加上$f[i][j]*\{1-(1-p[i+1])^{r-j}\}$。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj
 
using namespace std;

typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP;

#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b)  for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define p_b(a) push_back(a)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))

template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}

inline int sgn(DB x){if(abs(x)<1e-9)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

int gint()
  {
        int res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }
LL gll()
  {
      LL res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }

const int maxn=220;
const int maxr=132;

int n,r;
DB p[maxn+10],d[maxn+10];
DB f[maxn+10][maxr+10],ans;

DB power(DB a,int k){DB x=1.0;while(k){if(k&1)x*=a;a*=a;k>>=1;}return x;}

int main()
  {
      freopen("bzoj4008.in","r",stdin);
      freopen("bzoj4008.out","w",stdout);
      int i,j;
      for(int Case=gint();Case;Case--)
        {
            n=gint();r=gint();
            re(i,1,n)scanf("%lf%lf\n",&p[i],&d[i]);
            re(i,0,maxn+10-1)re(j,0,maxr+10-1)f[i][j]=0;
            ans=0;
            f[0][0]=1;
                re(i,0,n-1)
                    re(j,0,min(i,r))
                        {
                          DB tmp=power(1-p[i+1],r-j);
                            f[i+1][j]+=f[i][j]*tmp;
                            if(j+1<=r)
                              {
                                  f[i+1][j+1]+=f[i][j]*(1-tmp);
                                  ans+=f[i][j]*(1-tmp)*d[i+1];
                                }
                        }
                PF("%0.10lf\n",ans);
        }
      return 0;
  }
View Code

 

posted @ 2015-11-11 11:08  maijing  阅读(487)  评论(0编辑  收藏  举报