hihocoder1489 Legendary Items

一道期望数学题。

哎可惜,数学期望没学好,被dfs大暴力套牢了。。。

题目链接:

http://hihocoder.com/contest/mstest2017march/problem/1

题解链接:

https://www.cnblogs.com/weedboy/p/6817786.html

http://www.bubuko.com/infodetail-2022790.html

 

我的dfs超时代码:

 

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 1000005
#include <cmath>
double n,p,q;
double ans;
double A[maxn];
void dfs(double yes,int got,double*A,int cnt)//yes:传进来的是“本次”的概率 
{
    if(got>=n)
    {
        double tmp = 1;
        tmp *= cnt;
        for(int i=0;i<cnt;i++)
        {
            tmp *= A[i];
        }
        //cout<<tmp<<endl;
        ans += tmp;
        return ;
    }
    
    double no = 1-yes;
    if(yes>=1)//一定取得 
    {
        got++;
        A[cnt] = 1;
        cnt++;
        int tmp = int(p*100/(pow(2,got)));
        double yes_next = 1.0*tmp/100;
        dfs(yes_next,got,A,cnt);
        //复原
        got--;
        cnt--; 
    }
    else
    {
        //如果成功
        got++;
        A[cnt] = yes;
        cnt++;
        int tmp = int(p*100/(pow(2,got)));
        double yes_next = 1.0*tmp/100;
        dfs(yes_next,got,A,cnt);
        //复原
        got--;
        cnt--;
        //如果失败
        A[cnt] = no;
        cnt++;
        yes_next = yes + q;
        dfs(yes_next,got,A,cnt); 
        //复原
        cnt--; 
    }
    
}
int main()
{
    cin>>p>>q>>n;
    p/=100;
    q/=100;
    ans = 0;
    dfs(p,0,A,0); 
    cout<<ans<<endl;
    
    return 0;
 } 
View Code

 

posted on 2018-12-21 22:00  _isolated  阅读(205)  评论(0)    收藏  举报