hdu 4986 Little Pony and Alohomora Part I(找规律,欧拉常数)
2016-07-01 19:53
471 查看
Little Pony and Alohomora Part I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 416 Accepted Submission(s): 220
Problem Description
Trixie is a female unicorn pony and traveling magician, having been rumored to be "the most magical unicorn in all of Equestria". One day, Trixie arrives in Ponyville.
There is n boxes on the stage. There is a random key in each box and each box has a key match it. As a unicorn, Trixie can use "Alohomora"(the unlocking spell) to open any box.
Trixie would like to open all the boxes, also Trixie would like to minimize the spells she used. Help Trixie to calculate the expected number of spells she need to used in order to open all the boxes necessary.
Input
Input contains multiple test cases (less than 100).
For each test case, only one line contains one integer n (1<=n<=1000000000).
Output
For each case, output the corresponding result, rounded to 4 digits after the decimal point.
Sample Input
1
2
Sample Output
1.0000
1.5000
HintThe second sample: There are two different permutations when n equal to 2: (1, 2) and (2, 1). Trixie need to use 2 spells in (1, 2) and 1 spell in (2, 1).
题意:有n个箱子,小马每次可以用魔法打开一个箱子,每个箱子里面可能有任意一把1~n的钥匙,问打开所有箱子平均要使用多少次魔法
思路:写出前几个样例可以发现
f(1)=1.0000
f(2)=1.5000
f(3)=1.8333
f(4)=2.0833
发现了什么 ? 没错f(n)=f(n-1)+1/n
那么就是f(n)=1/1+1/2+1/3+...1/n O(n)递推过去肯定TLE
所以对这个数列求极限,可以得到f(n)=ln(n)+C(C是欧拉常数,等于0.577215664901,当然这道题要求精确到小数点后四位,所以取0.5772即可)
注意前面的项不能用这个极限,这个是n很大才成立的~
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100010
#define C 0.577215664901
double f
;
int main()
{
long long n;
f[0]=0;
for(int i=1;i<=100000;i++)
{
double b=i;
f[i]=f[i-1]+1/b;
}
while(~scanf("%lld",&n))
{
if(n>100000)
printf("%.4lf\n",log((double)n)+C);
else printf("%.4lf\n",f
);
}
return 0;
}
相关文章推荐
- DIV+CSS实现二级菜单
- UVA1343(The rotation Game|旋转游戏)=》状态空间分析+迭代加深搜
- Bugtags - App 测试 · 从未如此简单
- 初探安卓通知功能
- HDOJ2689(树状数组)
- [Leetcode]8. String to Integer (atoi)
- mysql调优
- [精]poj2724
- 真问真答:中国人为何蔑称朝鲜人“棒子”|大象公会
- cvHaarDetectObjects参数意义
- Bugtags 介绍视频 - App 测试 · 从未如此简单
- dns
- Unique Paths II
- Gradle学习网站
- PHP网页缓存技术
- linux 实战二 网关服务器
- 路由表 (RIB) 与转发表 (FIB)
- Project第二天 观察者模式处理定位信息
- hdu 4985 Little Pony and Permutation(水题)
- 使用Gson解析复杂、变态的Json数据(包含中文key)