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;
}
uva1639 Candy的更多相关文章
- [水题日常]UVA1639 糖果(Candy,ACM/ICPC Chengdu 2012)
今天来尝试了几道数学期望相关的题,这是我认为比较有趣的一道题 这次不废话啦直接开始~ 一句话题意:两个分别装有n个糖果的盒子,每次随机选一个盒子然后拿走一颗糖(选的概率分别是\(p\)和\((1-p) ...
- [LeetCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- Leetcode Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【leetcode】Candy(hard) 自己做出来了 但别人的更好
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- 【leetcode】Candy
题目描述: There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s
C. Inna and Candy Boxes Inna loves sweets very much. She has n closed present boxes lines up in a ...
- [LintCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
随机推荐
- android中的selector背景选择器的用法
关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法. 首先android的selector是在 ...
- PDF.NET框架操作——工具应用(一)
PDF.NET是个开源的项目其解决UI层(WinForm / Web)控件数据绑定.映射与查询: BLL层实体对象查询(OQL):DAL层SQL语句和.NET数据访问代码映射(查看 SQL-MAP ...
- 剑指offer--面试题17
题目:合并两个排序的单向链表 自己所写代码如下: ListNode* MergeSortedLists(ListNode* pHead1, ListNode* pHead2) { if(pHead1 ...
- PAT-乙级-1053. 住房空置率 (20)
1053. 住房空置率 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 在不打扰居民的前提下,统计住房空 ...
- 【Memcache】下载和安装
下载: Win7 64bit 系统 下载过过很多版本,都无法安装,最后到这里下载,成功安装: http://blog.couchbase.com/memcached-windows-64-bit-pr ...
- 用NPOI导出Excel
用NPOI导出Excel public void ProcessRequest(HttpContext context) { context.Response.ContentType = " ...
- GCC 静态库和动态库
转自GCC 静态库和动态库 //hello.c #include void print_hello() { printf("HelloWorld "); } //main.c #i ...
- 一个碉堡的swing类
今天遇到一个掉了一笔的swing类.可以完美的解决JFrame下设置背景的问题.任意size.太掉了.特记于此 import java.awt.*; import java.awt.image.Buf ...
- VirtualBox下导入CentOS后,无法上网
从VirtualBox的"管理"菜单下,选择"导出虚拟电脑",存一个备份.用时,再从"管理"菜单下,选择"导入虚拟电脑&q ...
- Java中finalize()
垃圾回收器要回收对象的时候,首先要调用这个类的finalize方法(你可以 写程序验证这个结论),一般的纯Java编写的Class不需要重新覆盖这个方法,因为Object已经实现了一个默认的,除非我们 ...