UVA 107 The Cat in the Hat (数论)

本文探讨了一只聪明的猫如何使用其帽子内的小猫来高效地打扫房间,揭示了一个有趣的数学问题及其解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


                                                                                                     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
 题目大意:

              有一只(聪明的)猫要打扫房间,但不想自己一个人干,于是让它帽子里的猫出来帮忙,它帽子里的猫的帽子里还有猫,它帽子里的猫的帽子里的猫的帽子里还有猫,等等。打扫房间的工作量不大,不需要所有的猫来帮忙,于是这只(聪明的)猫想到一个注意:“随着帽子的逐层变小,猫的身高也逐渐变小,会变到最小身高1,由身高为1的猫来打扫房间(输入会让每次下一层的猫的高度满足整数要求,并且会满足最后一层猫的整数(即1)等于workers的输入)。在每层帽子里的猫的数量是个常数,N(要我们自己去找这样的常数)。每层帽子里的猫的身高是上一层的猫的身高的1/(N+1)倍。猫的身高最小只能是1(数的叶子);结果一定有猫要去打扫房间;所有的猫的身高都为整数。会给定最初那只猫(那只聪明的猫)的身高和要打扫房间的猫(身高为1的猫)的数量,请找出不用打扫房间的猫的数量及所有的猫的身高的总和。


解题思路:

            输入h,w.

            设k为层数序号,第0层,第1层,。。。。

             height:   h    h/(N+1)   h/(N+1)^2   h/(N+1)^3   h/(N+1)^4   ……   h/(N+1)^x = 1

            amount:1          N             N^2              N^3              N^4        ……    N^x = workers

            ①h* ( 1/ (N+1) )^k=1; →③h=(N+1)^k;

            ②N^k=w;

             把②代入③,( w ^ (1/k) +1)^ k = h;此时只需要枚举k就可以,k不会枚举到很大,最多枚举到32就超出 int 了。得出k后求N,后略。


代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#define eps 1e-9
using namespace std;

int firstHeight,workers,n,noWorkers,sumHeight,k;

void solve(){
    noWorkers = sumHeight = 0;
    for(k=0;k<100;k++){//k代.
        double left = pow(pow(workers,1.0/k)+1.0,k*1.0);
        if(fabs(left-firstHeight)<eps) break;
    }
    n=pow(workers*1.0,1.0/k)+eps;//强转int要加eps, 本来是double 型千万不要加eps.
    int tempHeight = firstHeight;
    for(int i=0;i<k;i++){
        noWorkers=noWorkers+pow(n*1.0,i) + eps;
        sumHeight=sumHeight+(pow(n*1.0,i)+eps)*tempHeight + eps;
        tempHeight = ceil(tempHeight*1.0/(n+1));//取整
    }
    sumHeight+=pow(n*1.0,k)+eps;
}

void output(){printf("%d %d\n",noWorkers,sumHeight);}

int main(){
    while(scanf("%d%d",&firstHeight,&workers)&&(firstHeight!=0||workers!=0)){
        solve();
        output();
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值