The Cat in the Hat
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Submit Status Practice UVA 107
Appoint description:
Description
Download as PDF
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
/*
Author:ZCC;
Time:2015-6-6
Solve:题目大意:有一只高H的猫, 要去打扫一个房间,但是它很懒惰,
就从帽子里变出N只猫来帮它干活, 变出来的N只猫的高度为原来那只猫的1 / (N + 1),
可是被变出来的N只猫也很懒惰, 也不想干活,同样从帽子里变出N只猫帮自己干
(注意,是1只变出N只, 不是总共N只),高度同样是原先的1 / (N + 1),...
直到小猫的高度为1时, 不具有再变小小猫的能力, 只能自己干活, 现在给出始祖猫的高度H,
和最终苦逼干活猫的数量M, 求有多少只猫不用干活,和所有猫的高度总和。
解题思路:首先设变幻了k次,就有
<1>N ^ k = M ==> k * log(N) = log(M) ==> k = log(M) / log(N).
<2>H * (1 / (N + 1)) ^ k = 1 ==> H = (N + 1) ^ k == > k = log(H) / log(N + 1).
<3>N从1开始遍历, 直到log(M) / log(N) - log(H) / log(N + 1) 趋近于0
接下来利用等比数列的求和公式
*/
#include<iostream>
#include<algorithm>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
const int maxn=55;
typedef long long LL;
int a[maxn];
int main(){
#ifndef ONLINE_JUDGE
// freopen("Text//in.txt","r",stdin);
#endif // ONLINE_JUDGE
int H,M;
while(~scanf("%d%d",&H,&M)&&(H||M)){
int n=1;
while(fabs(log(n) / log(n+ 1) - log(M) / log(H) )> 1e-10)n++;
int k=(int)(log(H)/log(n+1)+0.5);
if(n==1)
printf("%d ",k);
else
printf("%d ",(int)(0.5+(1-pow(n,k))/(1-n)));
printf("%d\n",int(0.5 + (1 - pow(n*1.0 / (n + 1), k + 1)) * (n + 1) * H));
}
return 0;
}
/*
Sample Input
2
12
-3646397
Sample Output
7
2701
*/
版权声明:本文为博主原创文章,未经博主允许不得转载。