您的位置:首页 > 其它

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: