The Cat in the Hat

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 125
5764801 1679616
0 0

Sample Output

31 671
335923 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值