HDU ACM 4465 candy-> probability-mathematical expectation-permutation combination

Source: Internet
Author: User

Test instructions: There are two boxes, each containing sugar N, each day from each of the two to take a sugar, suddenly one day when the child took sugar found a box empty, ask the other box is now how much sugar (for expectations)?

#include <iostream> #include <cmath>using namespace std;double f[400008];d ouble c_n_m (int n,int M) {return f[ N]-F[M]-F[N-M];} void Init () {int i; F[0]=0;for (i=1;i<=400006;i++) F[i]=f[i-1]+log (i*1.0);} Double sovle (int n,double p) {double sum;int k;for (sum=0,k=0;k<=n-1;k++) sum+= (n-k) * (exp ((C_n_m (n+k,k)) + (n+1) *log ( p) +k*log (1-p)) +    exp ((C_n_m (n+k,k) + (n+1) *log (1-p) +k*log (P))); return sum;} int main () {double p;int n,t=0;init (); while (cin>>n>>p) {printf ("Case%d:%.6f\n", ++t,sovle (N,p));} return 0;}


Reference from: http://blog.csdn.net/sylg_li/article/details/8208514 and Http://www.tuicool.com/articles/zInIja.

First of all, I do this problem went through the misunderstanding, I mistakenly thought the topic said that the other box refers to the acquisition probability for 1-p that, finally saw a problem solving report formula only know that the sum of two expectations. This problem is expected to be a good formula, that is, when the combination of permutations may not know how to calculate, take the log (this is the math in which LN does not think it is) hunger, restore time with exp (). Also, the first box is empty when you still need to take a sugar, because it is not available to know when the sugar is finished.

This place also needs to be noted, why is C (n+k,k) instead of C (n+k+1,k)? k is the number of sugars taken by the other box, we took the n+1 sugar in the first box, but the empty time did not take the candy, so we calculate the probability of the expectation is still to use C (n+k K

The biggest difficulty in calculating the number of combinations is the overflow of data, and for an integer n greater than 150, it is easy to go beyond the range of double type, so when the n=200 in C (n,m), it is hopeless to calculate the basic formula directly with the combinatorial formulas. Another difficulty is efficiency.

For the first data overflow problem, this can be resolved. Because the combination number formula is:
C (n,m) = n!/(m! ( N-M)!)

In order to avoid directly calculating the factorial of N, the logarithm is taken on both sides of the formula, thus obtaining:
ln (C (n,m)) = ln (n!) -ln (m!) -ln ((n-m)!)

Further simplification gets:

In this way we will convert the multiplication to add, because ln (n) is always small, so it is difficult to overflow the data.

In order to solve the problem of the second efficiency, we will make a further simplification of the above formula. The upper formula has turned the multiplication into a linear operation of summation, that is, the upper formula has greatly simplified the computational complexity, but can be further optimized. From the above, it is easy to see that the 3 items on the right are necessarily duplicates. Now let's split the first item on the right into two parts:

In this way, the first item on the right side of the top can be offset, thus getting:

The above method directly reduces the 2m logarithm calculation and the summation operation. But this formula can also be optimized. For the sum of the above formula, when M<N/2, N-m is a very large number, but when M>N/2, N-m will be much smaller. We know:
C (n,m) = C (n,n-m)

So through this formula, we can change m less than N/2 to a n-m greater than N/2, and the result is the same, but it can reduce the computational amount.

When ln (C (n,m)) is calculated, only the natural logarithm is required to get the number of combinations:
C (N,M) = exp (ln (C (n,m)))

This completes the calculation of the combined number.

Using this method to calculate the number of combinations, if only ln (C (n,m)) is calculated, N can be taken to the limit value of the integer data 65535,


HDU ACM 4465 candy-> probability-mathematical expectation-permutation combination

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.