hdu4586——Play the Dice

Play the Dice

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 3072    Accepted Submission(s): 985
Special Judge


Problem Description
There is a dice with n sides, which are numbered from 1,2,...,n and have the equal possibility to show up when one rolls a dice. Each side has an integer ai on it. Now here is a game that you can roll this dice once, if the i-th side is up, you will get ai yuan. What's more, some sids of this dice are colored with a special different color. If you turn this side up, you will get once more chance to roll the dice. When you roll the dice for the second time, you still have the opportunity to win money and rolling chance. Now you need to calculate the expectations of money that we get after playing the game once.
 

Input
Input consists of multiple cases. Each case includes two lines.
The first line is an integer n (2<=n<=200), following with n integers a i(0<=a i<200)
The second line is an integer m (0<=m<=n), following with m integers b i(1<=b i<=n), which are the numbers of the special sides to get another more chance.
 

Output
Just a real number which is the expectations of the money one can get, rounded to exact two digits. If you can get unlimited money, print inf.
 

Sample Input
  
6 1 2 3 4 5 6 0 4 0 0 0 0 1 3
 

Sample Output
  
3.50 0.00
 

Source


概率问题启发性博客 点击打开链接
转载的题解
题意:一个骰子有 n 面,每一面有不同的数字a[i],再给一个m,给m个面,这些面如果被骰到,可以继续多骰一次,问你骰一次得到数字的期望是多少?

    解题思路:我们要算骰骰子得出的概率,我开始想的就是sum1是(n-m)非特殊面的和,sum2是特殊面的和,那么骰一次的概率就是
sum1*[(n-m)/n],骰两次的概率是(sum2*(m/n)+sum1*(m/n)*[(n-m)/n],第三次是sum2*(m/n)*(m/n)+sum1*(m/n)*(m/n)*[(n-m)/n] 如此这样递推可以将最终的解求出。最终可以得到的解是(sum1+sum2)+(sum1+sum2)*(m/n)+(sum1+sum2)*(m/n)^2+....+...  记sum1+sum2=sum这样是无穷等比数列,可以使用公式得到p=sum*[(n)/(n-m)],然后这是总期望,还需要除以面数n,得到答案为sum/(n-m).

其实也可以换一条思路,由于是无限,我们可以看作是
只考虑第一次,获得的金币的平均值为sum/n.sum为所有色子的面的金币值相加。
对于运气好,摇中了可以再来一次,该轮就能获得m/n*(sum/n)
运气好,又再来一次,该轮能获得(m/n)^2*(sum/n)
无穷无尽的摇下去,一共能获得sum/n*(1+q + q^2+`````+q^k + ````)其实就是个等比数列,其中公比为q = m/n
将式子化简,就能得到E = sum/(n-m) 或者 E = a1/(1-q),其中a1 = sum/n
。所以当sum = 0时为0,n = m时为inf。其余就为sum/(n-m)。

不过这个题目需要特判,如果sum = 0首先输出0.00,然后是n == m,输出inf。因为题目会卡你sum==0且n==m这组答案。

/*
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <stack>
using namespace std;
typedef long long ll;
#define PI 3.1415926535897932
#define E 2.718281828459045
#define INF 0x3f3f3f3f
#define mod 100000007


const int M=1005;
int n,m;
int cnt;
int sx,sy,sz;
int mp[1000][1000];
int pa[M*10],rankk[M];
int head[M*6],vis[M*100];
int dis[M*100];
ll prime[M*1000];
bool isprime[M*1000];
int lowcost[M],closet[M];
char st1[5050],st2[5050];
int len[M*6];
typedef pair<int ,int> ac;
//vector<int> g[M*10];
int dp[M];
int has[10500];
int mon[13]= {31,28,31,30,31,30,31,31,30,31,30,31};
int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0};
int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}};


void getpri()
{
    ll i;
    int j;
    cnt=0;
    memset(isprime,false,sizeof(isprime));
    for(i=2; i<1000000LL; i++)
    {
        if(!isprime[i])prime[cnt++]=i;
        for(j=0; j<cnt&&prime[j]*i<1000000LL; j++)
        {
            isprime[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    }
}
struct node
{
    int v,w;
    node(int vv,int ww)
    {
        v=vv;
        w=ww;
    }
};
vector<int> g[M*100];
string str[1000];


ll fib[1000];
void getfib(){
    fib[0]=fib[1]=1;
    for(int i=2;i<=50;i++)
        fib[i]=fib[i-1]+fib[i-2];
}
ll tmp[1000],p;
void dfs(ll n,int k){
    if(n==0){
        printf("%lld=",p);
        for(int i=0;i<k-1;i++)
            printf("%lld+",tmp[i]);
        printf("%lld\n",tmp[k-1]);
        return ;
    }
    if(n<0)return ;
    for(int i=1;fib[i]<=n;i++)
    {
        if(!vis[i])
        {
            vis[i]=1;
            tmp[k]=fib[i];
            dfs(n-fib[i],k+1);
            //vis[i+1]=0;
        }
    }
}
int main()
{
    int i,j,k,t;
    ll ans;
    getfib();
    scanf("%d",&t);
    while(t--){
            memset(tmp,0,sizeof(tmp));
    memset(vis,0,sizeof(vis));
        cin>>ans;
        p=ans;


        dfs(ans,0);
    }
    return 0;
}
*/




#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <stack>
using namespace std;
typedef long long ll;
#define PI 3.1415926535897932
#define E 2.718281828459045
#define INF 0x3f3f3f3f
#define mod 100000007


const int M=1005;
int n,m;
int cnt;
int sx,sy,sz;
int mp[1000][1000];
int pa[M*10],rankk[M];
int head[M*6],vis[M*100];
int dis[M*100];
ll prime[M*1000];
bool isprime[M*1000];
int lowcost[M],closet[M];
char st1[5050],st2[5050];
int len[M*6];
typedef pair<int ,int> ac;
//vector<int> g[M*10];
int dp[M];
int has[10500];
int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0};
int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}};


void getpri()
{
    ll i;
    int j;
    cnt=0;
    memset(isprime,false,sizeof(isprime));
    for(i=2; i<1000000LL; i++)
    {
        if(!isprime[i])prime[cnt++]=i;
        for(j=0; j<cnt&&prime[j]*i<1000000LL; j++)
        {
            isprime[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    }
}
struct node
{
    int v,w;
    node(int vv,int ww)
    {
        v=vv;
        w=ww;
    }
};
vector<int> g[M*100];
string str[1000];
//int dp[10000];
//int a[1000],b[1000];
int ans;


int main()
{
    int i,j,k,t;
    int sum=0;
    while(~scanf("%d",&n))
    {
        sum=0;
        for(i=0; i<n; i++)
        {
            scanf("%d",&t);
            sum+=t;
        }
        scanf("%d",&m);
        for(i=0;i<m;i++)
            scanf("%d",&t);
        if(sum==0)printf("0.00\n");
        else if(n==m)printf("inf\n");
        else printf("%.2lf\n",(sum*1.0)/((n-m)*1.0));
    }
    return 0;
}
数学能力很重要啊。。。
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值