UVA 107 The Cat in the Hat

来源:互联网 发布:淘宝卖家发布宝贝流程 编辑:程序博客网 时间:2024/06/15 12:11

(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 ick of his wrist he pops his top o .
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!
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 1 times the height of the cat whose hat they are in.
N+1
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), nd 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 speci cations. Each speci cation is a single line consisting of two positive integers, separated by white space. The rst 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 speci cation), 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只猫,高度为H/(N+1),同样这些猫也从帽子里变出N只猫,直到猫的高度为1。
输入第一只猫的高度和最后干活猫的数量 ,输出不用干活的猫的数量和所有猫的高度和。

思路:
利用数学公式和两个关系式,(M为第k代猫的总数)
N ^ k = M
H * ( 1/( N+1 ) ) ^K = 1
注意输入为“1 1”的特殊情况

#include <iostream>#include <cmath>using namespace std;int main(){    int H,W,k,n;    while(cin>>H>>W)    {        int num=0;        int tolH=0;        if(H==0&&W==0)            break;        if(H==1&&W==1)//特殊情况            cout<<"0 1"<<endl;        else        {            for(k=1;H!=int(pow(1.0+pow(W,1.0/k),k)+0.1);k++);//通过枚举得出k的值            n=int(pow(W,1.0/k)+0.1);//由k推出每次变出的兔子数n            for(int i=0;i<=k;i++)            {                num+=pow(n,i);//总数                tolH+=H*pow(n,i);//总高度                H = int(H*(1.0/(n+1))+0.1);//每次变化后的高度            }            cout<<num-W<<" "<<tolH<<endl;        }    }    return 0;}
原创粉丝点击