HDU 4465-Candy (probability and mathematical optimization)

Source: Internet
Author: User

In question B of Chengdu Regional in 2012, it took an hour to release the formula, but after a long time, I found that I could not control the accuracy. Then I suddenly thought that the number of combinations could be optimized using log, after the change, the probability question of comparison with water is AC.

# Include <stdio. h> # include <math. h> # define maxn 200005 double f [2 * maxn]; double c (int a, int B) {return f [a]-f [a-B]-f [B];} int main () {f [0] = 0; for (int I = 1; I <2 * maxn; I ++) {f [I] = f [I-1] + log (1.0 * I);} double ans; double p; int n, cas = 0; while (scanf ("% d % lf", & n, & p) = 2) {ans = 0; double u = log (p ), v = log (1-p); for (int I = 1; I <= n; I ++) {double k1 = c (2 * n-I, n-I) + (n-I) * u + (n + 1) * v; double k2 = c (2 * n-I, n-I) + (n-I) * v + (n + 1) * u; ans + = I * (exp (k1) + exp (k2);} printf ("Case % d: % lf \ n ", ++ cas, ans);} return 0 ;}

 


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.