HOJ---12498 Indomie[组合数学]

Indomie
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 14, Accepted users: 14
Problem 12498 : Special judge
Problem description
During recession, Amjad needs to queue for SembakoPlus. Sembako, as we all know, stands for “Sembilan Bahan Pokok” which consists of 9 kinds of item: Rice, Sugar, Cooking-oil,Meat, Egg, Milk, Corn, Kerosene and Iodized Salt. SembakoPlus consists of Sembako and one more item: Indomie! Amjad’s favorite of all time!! (therefore, no wonder why he could stand for this long queue).
Each person in the queue is allowed to pick only one item. No need to ask, Amjad wants only Indomie. Unfortunately, they are running out of SembakoPlus stock and currently there are three kinds of item left: Rice, Sugar and Indomie. As he could see from afar, he is quite sure that Rice and Sugar will be enough for everybody.
Given the number of remaining Indomie and the number of people queuing in front of Amjad, your task is to count the probability that he will get his Indomie. Amjad can’t do programming right now as he is very nervous so he can’t think logically. He needs your help!

Input
There will be multiple test cases for this problem. Each test case contains two integers N (1 ≤ N ≤ 50) and S (0 ≤ S ≤ 50), where N is the number of people queuing in front of Amjad and S is the remaining number of Indomie.

Output
For each case, print in a single line the probability in percentage that he will get his Indomie with 5 digits precision (he’s being paranoid)

Sample Input
2 1

3 2

4 0

4 1

10 10

14 9

30 14
Sample Output
50.00000

76.92308

0.00000

33.33333

99.99831

98.65515

95.16071
Judge Tips
There are two peoples queuing in front of Amjad, so those two peoples could pick of the following combination {1st people, 2nd people}: 1. Rice, Rice 2. Rice, Sugar 3. Rice, Indomie 4. Sugar, Rice 5. Sugar, Sugar 6. Sugar, Indomie 7. Indomie, Rice 8. Indomie, Sugar Since there is only one Indomie left, there are only 4 out of 8 combinations that ensure Amjad to get his Indomie (1, 2, 4 and 5), hence the probability is 4/8 = 50%.

Problem Source
The ACM International Collegiate Programming Contest Sponsored By IBM

 

 

 

 

题意:在一列队伍中,Amjad前面排着n个人,有三种食物,两种无数量限制,一种Indomie有m个,问Amjad能取到Indomie的概率有多大。

 

分析:     eg.n=3     m=2

             三种情况:

                       1、n个人中,拿了0个Indomie             1*2^3

                       2、n个人中,拿了1个Indomie             C(n,1)*(2^(n-1))

  总      -----------------------------------------------------------------------------------------

                       3、n个人中,拿了2个Indomie             C(n,2)*(2^(n-2))

 

      故,1、2两种情况里,Amjad是可以拿到Indomie,第三种情况不可以,

               因此:符合情况的数量为:(1*2^3+C(n,1)*(2^(n-1)))

                        总数:(1*2^3+C(n,1)*(2^(n-1))+C(n,2)*(2^(n-2)))

 

用__int64就可以了

code: 

 1 #include<iostream>

 2 using namespace std;

 3 

 4 __int64 p[55];

 5 int n,m;

 6 double t1,t2;

 7 

 8 void fun_pow()

 9 {

10     __int64 t=1;

11     p[0]=1;

12     for(int i=1;i<=54;i++)

13         p[i]=p[i-1]*2;

14 }

15 

16 double fun_c(double n,double m)

17 {

18     if(m==0)

19         return 1.0;

20     double ans=1;

21     for(int i=0;i<m;i++)

22     {

23         ans*=((n-i)/(m-i));

24     }

25     return ans;

26 }

27 

28 int main()

29 {

30     fun_pow();

31     while(~scanf("%d%d",&n,&m))

32     {

33         if(m==0)

34         {

35             printf("0.00000\n");

36             continue;

37         }

38         if(n<m)

39         {

40             printf("100.00000\n");

41             continue;

42         }

43         t1=0;

44         for(int i=0;i<=m;i++)

45         {

46             t1+=(fun_c(n,i)*p[n-i]);

47             if(i==m-1)

48             {

49                 t2=t1;

50             }

51         }

52         printf("%.5lf\n",100.0*t2/t1);

53     }

54     return 0;

55 }

 

 

 

 

 

 

 

 

你可能感兴趣的:(dom)