POJ 3716

题意:给4个骰子,每面染有红色或者蓝色,概率相等,然后告诉你前两次扔后红色面朝上的骰子各有几个,问你再扔一次红色面朝上概率是多大

题解:对于推不出公式的童鞋就只好爆搜了,但是,条件概率公式还是要知道才行,即P(A|B)=P(B|A)*P(A)/P(B),通过这个,单独看每一个骰子,可以求出当掷出骰子状态为00,01,10,11时骰子6面为t时的概率。

   接着,就是4^4枚举每个骰子状态*7^4枚举每个骰子染色状况*2^4计算期望,然后,还100MS+给过了

View Code
  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<cmath>
  5 using namespace std;
  6 const double eps=1e-10;
  7 double C6[7]= {1.0,6.0,15.0,20.0,15.0,6.0,1.0};
  8 double PT[7],PXT[4][7],PTX[7][4],PX[4];
  9 double ans,num;
 10 void cal(int t1,int t2,int t3,int t4,double pp)
 11 {
 12     double p1,p2,p3,p4;
 13     for(int n1=0; n1<2; n1++)
 14     {
 15         if(t1<n1||(n1==0&&t1==6))
 16             continue;
 17         p1=(n1==0)?(6-t1)/6.0:t1/6.0;
 18         for(int n2=0; n2<2; n2++)
 19         {
 20             if(t2<n2||(n2==0&&t2==6))
 21                 continue;
 22             p2=(n2==0)?(6-t2)/6.0:t2/6.0;
 23             for(int n3=0; n3<2; n3++)
 24             {
 25                 if(t3<n3||(n3==0&&t3==6))
 26                     continue;
 27                 p3=(n3==0)?(6-t3)/6.0:t3/6.0;
 28                 for(int n4=0; n4<2; n4++)
 29                 {
 30                     if(t4<n4||(n4==0&&t4==6))
 31                         continue;
 32                     p4=(n4==0)?(6-t4)/6.0:t4/6.0;
 33                     double p=p1*p2*p3*p4*pp;
 34                     num+=p;
 35                     ans+=(n1+n2+n3+n4)*p;
 36                 }
 37             }
 38         }
 39     }
 40 }
 41 int main()
 42 {
 43     for(int t=0; t<7; t++)
 44         PT[t]=C6[t]/64.0;
 45     memset(PX,0,sizeof(PX));
 46     memset(PXT,0,sizeof(PXT));
 47     memset(PTX,0,sizeof(PTX));
 48     for(int t=0; t<7; t++)
 49     {
 50         for(int x=0; x<4; x++)
 51         {
 52             int a=x%2,b=(x>>1)%2;
 53             PXT[x][t]=((a==0)?(6-t)/6.0:t/6.0)*((b==0)?(6-t)/6.0:t/6.0);
 54             PX[x]+=PXT[x][t];
 55         }
 56     }
 57     for(int t=0; t<7; t++)
 58     {
 59         for(int x=0; x<4; x++)
 60         {
 61             PTX[t][x]=PXT[x][t]*PT[t]/PX[x];
 62         }
 63     }
 64     int T;
 65     for(scanf("%d",&T); T; T--)
 66     {
 67         int a,b;
 68         scanf("%d%d",&a,&b);
 69         ans=0.0;
 70         num=0.0;
 71         for(int x1=0; x1<4; x1++)
 72         {
 73             for(int x2=0; x2<4; x2++)
 74             {
 75                 for(int x3=0; x3<4; x3++)
 76                 {
 77                     for(int x4=0; x4<4; x4++)
 78                     {
 79                         int aa=x1%2+x2%2+x3%2+x4%2;
 80                         int bb=x1/2%2+x2/2%2+x3/2%2+x4/2%2;
 81                         if(aa==a&&bb==b)
 82                         {
 83                             for(int t1=0; t1<7; t1++)
 84                             {
 85                                 if(PTX[t1][x1]>eps)
 86                                 for(int t2=0; t2<7; t2++)
 87                                 {
 88                                     if(PTX[t2][x2]>eps)
 89                                     for(int t3=0; t3<7; t3++)
 90                                     {
 91                                         if(PTX[t3][x3]>eps)
 92                                         for(int t4=0; t4<7; t4++)
 93                                         {
 94                                             if(PTX[t4][x4]>eps)
 95                                             {
 96                                                 cal(t1,t2,t3,t4,PTX[t1][x1]*PTX[t2][x2]*PTX[t3][x3]*PTX[t4][x4]);
 97                                             }
 98                                         }
 99                                     }
100                                 }
101                             }
102                         }
103                     }
104                 }
105             }
106         }
107         printf("%.3lf\n",ans/num);
108     }
109     return 0;
110 }

转载于:https://www.cnblogs.com/tmeteorj/archive/2012/09/17/2689630.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值