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 tex2html_wrap_inline35 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, M。

H代表猫的高度。 这只猫可以分出 N只 身高为H / (N + 1) 的子猫 (注意,分出来的身高一定是整数)。每只子猫还可以在分。直到身高为1.这些身高为1的猫要去工作,他们的数量就是M。。。。

要输出没有工作的猫的数量 和 所有猫的总身高

。。设,分K次。 得到式子 (1)H / ((N + 1) ^ K) = 1,(2)N ^ K = M 。

 由(2)式子,可以推出。N = M ^ (1 / K); 带入 (1) 得H =  (M ^ (1 / K) + 1) ^ K..然后枚举求出K。

然后由K 求出 N。。。

接着求输出的两个值就好办了。 一个循环得出结果。这有要注意的地方, 如果H,M 同时为1, 要输出 0, 1。。

详细见代码。。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int H, M;
int main()
{
	int i;
	while (~scanf("%d%d", &H, &M) && H && M)
	{
		int sumz = 0;
		int sumh = 0;
		int K, N;
		if (H == 1 && M == 1)
		{
			printf("0 1
");
			continue;
		}
		for(K = 1; H != int (pow(1. + pow(M, 1./K), K) + 0.5); K ++);
		N = int(pow(H, 1./K) + 0.1) - 1;
		for (i = 0; i < K; i ++)
		{
			sumz += int(pow(N, i) + 0.5);
		}
		for (i = 0; i <= K; i ++)
		{
			sumh += int (pow(N + 1, K - i) * pow(N, i) + 0.5);
		}
		printf("%d %d
", sumz, sumh);
	}
	return 0;
}



原文地址:https://www.cnblogs.com/aukle/p/3228704.html