The Cat in the Hat

来源:互联网 发布:ios icon制作软件 编辑:程序博客网 时间:2024/05/21 09:41

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 1255764801 16796160 0

Sample Output

31 671335923 30275911

题目大意:有一只猫,高h,它有一只帽子,帽子里面放N只猫,这N只猫的高为h1=h/(N+1),里面的猫有分别有一只帽子,里面再有N只猫,h2=h1/(N+1),.......依次类推,直到猫的高度为一时,它的帽子不放任何的猫,这些高度为一的猫必须工作。(除了高度为一的猫,每只帽子里的猫的个数都一样。)

解决方案:

可得:

猫的高度:h     h/(N+1)   h/(N+1)^2   h/(N+1)^3 ....... h/(N+1)^s 

猫的数量:1      N               N^2               N^      .......        N^s

输入的是h,x;

则:h=(N+1)^s;

x=N^s;得公式:log(h)*log(n)==log(x)*log(N+1);枚举N,求出了N,就是等比数列的事了。

另外:当N=1时,要另外处理,而且要注意精度问题,因为notwork=(ceil)((log(h)/log(2));ceil返回大于或者等于表达式的最小整数;

#include<iostream>#include<cstdio>#include<cmath>using namespace std;int main(){    int h,x;    while(~scanf("%d%d",&h,&x)&&(h+x))    {        int i;        for(i=1;i<h;i++)        {            if(fabs(log(h)*log(i)-log(x)*log(i+1))<10e-8)                break;        }        if(i!=1)        {            cout<<(x-1)/(i-1)<<" "<<(h*(i+1))-(x*i)<<endl;        }        else        {            cout<<(ceil)(log(h)/log(2))<<" "<<2*h-1<<endl;        }    }    return 0;}


0 0
原创粉丝点击