题目:

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).


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.

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/(n + 1)倍,依次类推,直到变出的猫的高度为1就停止了,打扫卫生的都是高度为1的猫,输入的是没有变出猫的初始猫的高度,然后输入变出高度为1的猫的个数,让输出不打扫卫生,也就是高度大于1的猫有几只,然后输出所有猫的总高度。。有点饶啊啊啊啊,其实总共两个式子,设变了m次后结束了,初始猫有x只,输入的两个值分别是h,N,第一个式子(n + 1)^m = h 第二个式子 x*n^m = N 循环找到正确的解,特殊情况特殊处理。


#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
double m, n, h, N, ans1, ans2, flag1, flag2, flag;
void count (double h) {
	double i, j;
	for (i = 2; i <= h; i++)
		for (j = 1; j <= 32; j++)
			if (pow(i, j) > h)
				break;
			else if (pow(i, j) == h && i != flag1 && j != flag2) {
				m = j;
				n = i - 1;
				return;
			}
}
int judge() {
	if (ans1 < N)
	 	return 1;
	else {
		flag1 = n + 1;
		flag2 = m;
		count(h);
		return 0;
	}
}
int main() {
	while (scanf("%lf %lf", &h, &N) != EOF && (h || N)) {
		if (h == 1 && N == 1) {
			printf("0 1\n");
			continue;
		}
		flag = 0;
		flag1 = flag2 = 0;
		count(h);
		double temp = pow(n ,m);
		double x = N / temp;
		if (n != 1)
			ans1 = x * ((1 - temp) / (1 - n));
		else
			ans1 = x * n * m;
		if (N == 1) 
			flag = 1;
		if (flag == 0)
			while (judge() != 1) {
				temp = pow(n, m);
				x = N / temp;
				ans1 = x * ((1 - temp) / (1 - n));
			}
		temp = pow(n * 1.0 / (n + 1), m + 1);
		ans2 = h * x * ((1 - temp) * 1.0 / (1 - n * 1.0 / (n + 1)));
		printf("%.0lf %.0lf\n", ans1, ans2);
	}
	return 0;
}