uva 107 - The Cat in the Hat

 The Cat in the Hat 

 

Background

(An homage to Theodore Seuss Geisel)

The Cat in the Hat is a nasty creature,
But the striped hat he is wearing has a rather nifty feature.

With one flick of his wrist he pops his top off.

Do you know what's inside that Cat's hat?
A bunch of small cats, each with its own striped hat.

Each little cat does the same as line three,
All except the littlest ones, who just say ``Why me?''

Because the littlest cats have to clean all the grime,
And they're tired of doing it time after time!

The Problem

A clever cat walks into a messy room which he needs to clean. Instead of doing the work alone, it decides to have its helper cats do the work. It keeps its (smaller) helper cats inside its hat. Each helper cat also has helper cats in its own hat, and so on. Eventually, the cats reach a smallest size. These smallest cats have no additional cats in their hats. These unfortunate smallest cats have to do the cleaning.

The number of cats inside each (non-smallest) cat's hat is a constant, N. The height of these cats-in-a-hat is  times the height of the cat whose hat they are in.

The smallest cats are of height one; 
these are the cats that get the work done.

 

All heights are positive integers.

Given the height of the initial cat and the number of worker cats (of height one), find the number of cats that are not doing any work (cats of height greater than one) and also determine the sum of all the cats' heights (the height of a stack of all cats standing one on top of another).

The Input

The input consists of a sequence of cat-in-hat specifications. Each specification is a single line consisting of two positive integers, separated by white space. The first integer is the height of the initial cat, and the second integer is the number of worker cats.

A pair of 0's on a line indicates the end of input.

The Output

For each input line (cat-in-hat specification), print the number of cats that are not working, followed by a space, followed by the height of the stack of cats. There should be one output line for each input line other than the ``0 0'' that terminates input.

Sample Input

216 125

5764801 1679616

0 0

Sample Output

31 671

335923 30275911

 下面这些,不是我翻译的。。。可以借鉴

一只神奇聪明猫走进了一间乱七八糟的房间,他不想自己动手收拾,他決定要找帮手來工作。于是他从他的帽子中变出了N只小猫來帮他(变出來的猫,高度为原來猫的 1/(N+1) )。這些小猫也有帽子,所以每一只小猫又从他的帽子中变出N隻小小猫來帮他。如此一直下去,直到这些小小小....猫小到不能再小(高度=1),他们的帽子无法再变出更小的猫來帮忙,而这些最小的猫只得动手打扫房间。注意:所有猫的高度都是正整数。

在这个问题中,给你一开始那只猫的高度,以及最后动手工作的猫的数目(也就是高度为1的貓的数目)。要请你求出有多少只猫是沒有在工作的,以及所有猫的高度的总和。

hight number
   216        1
     36        5
       6      25
       1    125

N=5;

671=216*1+36*5+6*25+1*125

N^n=number

(N+1)^n=hight

lg(n)/lg(n+1)=lg(number)/lg(hgiht)

于是用二分查找算出N即可,条件就写成了fabs(lg(n)*lg(hgiht)-lg(number)*lg(n+1)< EPS)

参考http://blog.csdn.net/frankiller/article/details/7726744

 1 #include <cstdio>

 2 #include <iostream>

 3 #include <cmath>

 4 using namespace std;

 5 

 6 #define EPS  1e-9

 7 

 8 int main()

 9 {

10     int hight, num;

11     int left, right, mid;

12     int x, y;

13     while(scanf("%d %d", &hight, &num) && (hight+num))

14     {

15         left = 1;

16         right = 10000000;

17         while(left)

18         {

19             mid = (left + right) / 2;

20             ///这就是在解方程,得到的mid就是 N

21             if(fabs( log(mid)*log(hight) - log(mid+1)*log(num)) <= EPS) ///lg(n)/lg(n+1)可能等于0 ... N=1

22                 break;

23             if(log(mid)*log(hight) - log(mid+1)*log(num) > 0)

24                 right = mid;

25             else

26                 left = mid;

27         }

28 

29         ///x是计算,除去第一只猫,其余猫的数量,y是计算总高度

30         x = 0;

31         y = hight;

32         left = 1;

33         while(hight > 1)

34         {

35             hight /= (1+mid);

36             left *= mid;

37             x += left;

38             y += hight*left;

39         }

40         printf("%d %d\n", x-num+1, y);

41     }

42 

43     return 0;

44 }

 

你可能感兴趣的:(uva)