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
解题思路:
帽子里的猫,
此题给的条件是第一支猫的高度h和最后的working cats的数量w.
下面分析猫的高度和猫的数量之间的关系:
设开始的猫的高度是 h , N 是每个帽子里面猫的数量,设一个中间变量s。
猫的高度 h h / (N + 1) h / (N + 1)2 ………… h / (N + 1)s
猫的数量 1 N N * N ………… Ns
当最后工作的猫的个数为w时:
lg(n)/lg(n+1)=lg(num)/lg(hgiht)
是用二分查找算出N即可,条件就写成了fabs(lg(n)*lg(height)-lg(num)/lg(n+1)<0.00001)
下面贴代码:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 int main() 6 { 7 int h,num; 8 while(~scanf("%d%d",&h,&num)) 9 { 10 if(0==h&&0==num) 11 break; 12 int left=1,right=10000000,mid; 13 while(left) 14 { 15 mid=(left+right)/2; 16 if(fabs(log(mid)*log(h)-log(mid+1)*log(num))<=0.000001) break; 17 if(log(mid)*log(h)-log(mid+1)*log(num)>0) 18 right=mid; 19 else 20 left=mid; 21 } 22 int x=0,y=h; 23 left=1; 24 while(h>1) 25 { 26 h/=(1+mid); 27 left*=mid; 28 x+=left; 29 y+=h*left; 30 } 31 printf("%d %d ",x-num+1,y); 32 } 33 }