best coder #35-01<组合数学 || 概率数学>

问题描述
一个盒子里有n个黑球和m个白球。现在DZY每次随机从盒子里取走一个球,取了n+m次后,刚好取完。DZY用这种奇怪的方法生成了一个随机的01串S[1(n+m)]。如果DZY第i次取出的球是黑色的,那么S[i]=1,如果是白色的,那么S[i]=0。 DZY现在想知道,'01'在S串中出现的期望次数。
输入描述
输入有多组测试数据。 (TestCase150)
每行两个整数, n, m(1n,m12)
输出描述
对于每个测试数据,输出一行答案,格式为p/q(p,q互质)。
输入样例
1 1
2 3
输出样例
1/2
6/5
Hint
Case 1: S='01' or S='10', 所以期望次数 = 1/2
Case 2: S='00011' or S='00101' or S='00110' or S='01001' or S='01010' 
or S='01100' or S='10001' or S='10010' or S='10100' or S='11000',
所以期望次数 = (1+2+1+2+2+1+1+1+1+0)/10 = 12/10 = 6/5
*************************************************************************************************
本题的官方解释是数学:
第i位上出现0,第i+1位出现1,这种概率是:(m/(n+m))*(n/(n+m-1)),0的位置可以出现在第一个一直到倒数第二个;所以累加(n+m-1),相承,公式是n*m/(n+m);

我的做法不是这样,组合数学:
刚开始看作所有的1发个在最左边,0放在最右边,一共最多有n或m个01串,(个数是其中较小的那个),枚举串的个数从1到最大,当有1
个01串的时候,从n个1中取一个空当,用来插入0,从m个0中可以选1个0放到空档里,也可以选两个一直到选m个,所以对于每一个1都有m种插法,c(n,1)*c(m,1)*1;
当有两个01串的时候,取1,c(n,2),取0,第一个1有1到m-1种插法,第二个1有剩余的插法,组合数公式,c(m,2);c(n,2)*c(m,2)*2;
累加下去
总共的排列数就是c(n+m,n);
相比一下,约分,就得到答案了;


代码如下:
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<string>
 5 #include<cstring>
 6 #define ll long long
 7 using namespace std;
 8 int N,M;
 9 long long k,t;
10 long long zuhe(ll n,ll x)
11 {
12     ll di=1,gao=1,ans=1;
13     for(ll i=1;i<=x;i++)
14     { 
15         di*=n;
16         n--;
17         gao*=i;
18     }
19     return di/gao;
20 }
21 void yuefen(ll a,ll b)
22 {
23     for(ll i=b;i>=1;i--)
24     {
25         if(a%i==0&&b%i==0)
26         {
27             k=a/i;
28             t=b/i;
29             return ;
30         }
31     }
32 }
33 int main()
34 {
35 //    freopen("in","r",stdin);
36 //    freopen("out.out","w",stdout);
37     while(scanf("%d%d",&N,&M)!=EOF)
38     {
39         k=0;
40         t=0;
41         for(ll i=1;i<=N;i++)
42         {
43             ll temp=0;
44             if(i>M)
45                 break;
46             t+=i*zuhe(N,i)*zuhe(M,i);
47         }
48         k=zuhe(N+M,M);
49         yuefen(k,t);
50         printf("%I64d/%I64d\n",t,k);
51     }
52     return 0;
53 }
View Code

 

转载于:https://www.cnblogs.com/by-1075324834/p/4413399.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将这段代码改写成python#include <iostream> #include "string.h" using namespace std; #define N 4 struct node { char c; double probablity; node* next; }; //输入节点的符号以及出现频率 node* Input() { node* p,*head; double pro = 0.0; //a head = new node; head->c = 'a'; head->probablity = 0.1; p = new node; //b head->next = p; p->c = 'b'; p->probablity = 0.4; p->next = new node; //c p = p->next; p->next = p; p->c = 'c'; p->probablity = 0.2; p->next = new node; //d p = p->next; p->next = p; p->c = 'd'; p->probablity = 0.3; p->next = NULL; /* head->c = 'a'; cin >> pro; head->probablity = pro; p = new node; head->next=p; for (int i = 1; i < N; i++) { p->c = 'a' + i; cin>>pro; p->probablity = pro ; //cout <<i<< p->c << '\t' << p->probablity<<endl; p->next = new node; p = p->next; } cout << endl; p->next = NULL; */ //打印链表 p = head; /*for (int i = 0; p != NULL; i++) { cout << p->c << '\t' << p->probablity << endl; p = p->next; }*/ return head; } //编码 double encoding(node* head) { node* p; p = new node; p = head; double low = 0.0, high = 1.0; double range; double range_high_low[2][N]; double prob = 0.0; //各字符范围 for (int i = 0; i < N; i++) { range_high_low[0][i] = prob; range_high_low[1][i] = p->probablity + prob; prob = range_high_low[1][i]; p = p->next; //cout << range_high_low[0][i] << '\t' << range_high_low[1][i] << endl; } //输入压缩信息a~d //注:double精度会缺失; char ch[] = {"cadacdb"}; //找到对应的概率 for (int j = 0; j < 9; j++) { p = head; for (int i = 0; p!=NULL; i++) { if ((int(ch[j])-int(p->c))==0) { cout << p->c << '\t'; int n = ch[j] - 'a'; //编码 range = high - low; high = low + range * range_high_low[1][n]; low = low + range * range_high_low[0][n]; cout << low << '\t' << high << endl; } p = p->next; } } return low; } int main() { node* head,*p; head = Input(); p = head; double coder=encoding(p); cout << coder; return 0; }
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值