UVA 107 The Cat in the Hat
2016-07-14 22:19
453 查看
UVA-107
题意:猫有一个帽子,可以分出N只它高度1/(N+1)高的猫,分身出来的猫也都能这样分身,但是身高最小为1。给了初始猫的高度h和最终干活的猫的只数w。求不干活猫的只数所有猫的总高度。解题思路:
次数 0 —– 1 —– 2 ———- 3 ———– 4 —– …… —– k
个数 1 —– N —– N^2 —– N^3 —– N^4 ———– N^k
高度 h –h/(N+1)–h/(N+1)^2–h/(N+1)^3 ———-h/(N+1)^k
可以得到 N^k = w , h/(N+1)^k = 1。
k=logN(w) ,带入另一个式子中 h = (N+1)^logN(w)。
两边关于h取对数。1 = logh(N+1) * logN(w)
因为C++自带的只有以10和以e为底的函数,换底公式一下
log(N+1)*log(W) / (log(N)*log(h)) =1
log(N+1) / log (N) - log (h) / log(w) = 0
此时只有一个N是未知数,枚举N。
考虑到精度问题,取abs(log(N+1) / log (N) - log (h) / log(w)) < 1e-10就好了。
剩下的就是简单的计算了。
/************************************************************************* > File Name: UVA-107.cpp > Author: Narsh > > Created Time: 2016年07月14日 星期四 10时12分42秒 ************************************************************************/ #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> using namespace std; int m,w,h,N,k; long long sum,num,l; int main () { while (scanf("%d%d",&h,&w) && h+w ) { N=1; while (abs(log(N)/log(N+1)-log(w)/log(h))>1e-10) N++; k = log(h)/log(N+1)+1e-10+0.5; l=1;sum = 0; num = 0; for(int i = 1; i <= k ; i++) { sum+=h*l; num+=l; l*=N; h=h/(N+1); } sum+=l; printf("%lld %lld\n",num,sum); } }
相关文章推荐
- Windows查看CPU的内核个数
- HDU 4258 Covered Walkway 斜率优化DP
- 三国的演义
- 畅通工程(并查集)
- Unsupported compiler 'com.apple.compilers.llvmgcc42' selected for architecture 'armv7'
- C语言实现 迪杰斯特拉算法
- xgboost/gbdt在调参时为什么树的深度很少就能达到很高的精度?
- 劣质的草 (连通分量)
- C语言培训_009
- Oracle常用的一些命令
- Swift基本语法(3)-控制流
- Jade学习篇
- junit命令行和testNg命令行
- 44. Wildcard Matching
- 修改表常用语法
- mysql 乱码
- Java反射:数组
- iOS打包成 ipa包(简单粗暴方法)
- Apache Kylin的前世今生
- .NET Core采用的全新配置系统[1]: 读取配置数据