uva1639 Candy

组合数,对数。

这道题要用到20w的组合数,如果直接相乘的话,会丢失很多精度,所以用去对数的方式实现。

注意指数,因为取完一次后,还要再取一次才能发现取完,所以是(n+1)次方。

double 会爆掉,需要用long double

然后就是scanf和printf读入输出long doube会发生不可逆转的错误(dev-cpp),所以可以读入输出时候强制转换类型,或者用cin,cout(后者我感觉比较麻烦)。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<iomanip>
using namespace std;
const int maxn = 200000;

long double v1,v2,c;
long double p,p1,p2,res;
double pd,resd;
int n,kase;
long double f[maxn*2+10];

double logC(int n,int m) {
    return f[n]-f[m]-f[n-m];
}

inline void init() {
    for(int i=1;i<=maxn*2;i++) f[i]=f[i-1]+log(i);
}

int main() {
    init();
    /*
    while(scanf("%d",&n)==1) {
        scanf("%lf",&pd);
        p=pd;
        p1=p2=1;
        res=0;
        for(int i=1;i<=n;i++) {
            c=logC(2*n-i,n);
            v1=c+(n+1)*log(p)+(n-i)*log(1-p);
            v2=c+(n+1)*log(1-p)+(n-i)*log(p);
            res+=(double) i*(exp(v1)+exp(v2));
        }
        resd=res;
        printf("Case %d: %.6lf\n",++kase,resd);
    }*/
    // 上面的代码是对的,嗯。我就想用cin,cout.
    while(cin>>n) {
        cin>>p;
        p1=p2=1;
        res=0;
        for(int i=1;i<=n;i++) {
            c=logC(2*n-i,n);
            v1=c+(n+1)*log(p)+(n-i)*log(1-p);
            v2=c+(n+1)*log(1-p)+(n-i)*log(p);
            res+=(double) i*(exp(v1)+exp(v2));
        }
        cout << "Case "<<++kase<<": ";
        cout << setprecision(6) <<fixed<< res<<‘\n‘;
    }
    // 明显感觉还是 cstdio大法好。
    return 0;
}
时间: 2024-12-15 12:32:35

uva1639 Candy的相关文章

UVa1639 - Candy(期望+对数精度处理)

注意题意是开始两个盒子各有n个糖果,等吃完就只有两种情况,盒子1没了,或者盒子2没了. 这完全是个求概率期望的数学问题.借用二项分布公式: P(ξ=K)= C(n,k) * p^k * (1-p)^(n-k), 其中C(n, k) = n!/(k! * (n-k)!)注意!:第二个等号后面的括号里的是上标,表示的是方幂. 得出第i次打开盒子1没糖的概率C(2n-1,n)p^(n+1)(1-p)^(n-i) 得出第i次打开盒子2没糖的概率C(2n-1,n)(1-p)^(n+1)p^(n-i) 第i

[LeetCode][Java] Candy

题目: There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more cand

从视频文件中读入数据--&gt;将数据转换为灰度图--&gt;对图像做candy边缘检测

//从视频文件中读入数据-->将数据转换为灰度图-->对图像做candy边缘检测 //作者:sandy //时间:2015-10-10 #include <cv.h> #include <highgui.h> int main(int argc, char *argv[]){ //预备工作 CvCapture* capture=cvCreateFileCapture("E:\\Videos\\xx.avi");//让capture变量指向视频文件 i

[leet code 135]candy

1 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more can

LeetCode 笔记25 Candy (艰难的调试)

There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more candies

Candy

动态规划: There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more ca

hdu 4465 Candy (快速排列组合 )

Candy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2115    Accepted Submission(s): 910 Special Judge Problem Description LazyChild is a lazy child who likes candy very much. Despite being ve

POJ 3083 Children of the Candy Corn

Children of the Candy Corn Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 308364-bit integer IO format: %lld      Java class name: Main The cornfield maze is a popular Halloween treat. Visitors are shown the

【LeetCode】Candy 解题报告

[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more can