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;All heights are positive integers.
these are the cats that get the work done.
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,戴了一个帽子,帽子里面有N只猫(N是常数,且未知),同样帽子里面的猫也戴了帽子,但是这些猫的高度变成了H / (N + 1),
求所有不工作的猫的数量,和所有猫高度的总和。
解析:
设开始的猫的高度是 h , N 是每个帽子里面猫的数量。
猫的高度 h h / (N + 1) h / (N + 1)2 ………… h / (N + 1)^k
猫的数量 1 N N * N ………… N^k
h = (N + 1)^k
输入 h 和 m , 则 m = N^k 。和上个式子联立消去中间变量k,得到 log10(h) * log10(N) = x * log10(N + 1)
剩下的就是等比数列公式了,注意当 N = 1 的时候要讨论。
但是如果先求N的话 N==1时,会有BUG。
看了别人的代码才知道。
最后得到:
N == 1 时
notwork = log2(h) 这里要注意,写代码的时候要用换底公式,并且可能因为精度的问题,要用 ceil 函数。
sumheight = 2 * h - 1;
N != 1 时
notwork = (x - 1) / (N - 1)
sumheight = h * (N + 1) - x * N
其实还有另外一个思路就是先求 k,h= (( m )^(1/k) + 1)^k
就不需要分情况讨论了。因为上面那种情况N=1时,log1 == 0 会有BUG,出现死循环。而先求k就不会了。
N = (m)^(1/k)
#include <stdio.h>
#include <math.h>
int main() {
int h,m;
while(scanf("%d%d",&h,&m) != EOF) {
if( h==0 && m==0) {
break;
}
if( h==1 && m==1) {
printf("0 1\n");
continue;
}
int k;
for( k = 1;; k++) {
if((int)(pow(1+pow(m,1.0/k),k)+ 0.1) >= h) {
break;
}
}
int n = (int)(pow(m*1.0,1.0/k) + 0.1);
int sum = 0;
int height = 0;
for(int i = 0; i < k; i++) {
sum += (int)( pow(n*1.0,i) + 0.1);
height += ((int)( pow(n*1.0,i)+0.1) / ( pow(n+1,i) ) * h);
}
height += m;
printf("%d %d\n",(int)sum,(int)height);
}
return 0;
}