흠..
/*
Subject: #107 The Cat in the Hat
e-mail: morcavon@gmail.com
Homepage: http://morcavon.byus.net
Building: 10/01/2005 ~ 14/01/2005
Last Update: 14/01/2005
*/
/*
<INPUT>
init_cat num_of_worker
.
.
0 0 : end of input
<OUTPUT>
[number of cats that are not working] [height of th stack of cats]
<CONDITION>
The smallest cats(the cats that get the work done) are of height 1.
The number of cats inside each cat's hat is a constant, N.
The height of these cats-in-a-hat is 1 / N+1 times the height of the cat whose
hat they are in.
height, number of cats > 0 (positive integer)
*/
#include <iostream.h>
#include <limits.h>
#include <math.h>
void main()
{
unsigned int x, N; /* x: 밑수, N: 모자 하나 안에 들어있는 고양이의 수 */
double n; // n: 지수(모자의 단계를 나타냄)
unsigned int init_cat, num_of_worker;
unsigned long total_head, total_heights;
while (cin >> init_cat >> num_of_worker) {
/* termination signal */
if (init_cat == 0)
break;
total_head = 1;
total_heights = init_cat;
/* N은 양의 정수...1이상이라고 생각해야 하나? */
for (x = 2; x <= init_cat; x++) {
if (init_cat % x == 0) {
/*
log(init_cat) == n * log(x)
-
적당한 x를 찾았다면, 위의 식을 이용해서 n를 구한다.
n이 정수가 아닐경우는 맞는 값이 아니므로 건너뛴다.
*/
n = log10(init_cat) / log10(x);
if (n - floor(n + 0.000000000000001) > 0)
continue;
/* initialize */
total_head = 1;
total_heights = init_cat;
N = x - 1;
n = ceil(n);
unsigned long temp, height;
for (height = 1; n > 0; n--, height *= x) {
temp = (unsigned long)pow(N, n);
total_head += temp;
total_heights += height * temp;
}
/* 주어진 조건을 체크. */
if (total_head < num_of_worker || height != init_cat)
continue;
else
break;
}
} /* end of for() */
cout << total_head - num_of_worker << ' ' << total_heights<< endl;
} /* end of while() */
}