码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1289 UVA107 The Cat in the Hat【暴力】

时间:2019-02-20 09:47:18      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:原来   except   ide   sel   print   help   ecif   event   min   

The Cat in the Hat
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1694 Accepted: 589

Description

(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!

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/(N+1)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).

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.

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

Source

Duke Internet Programming Contest 1991,UVA 107

问题链接POJ1289 UVA107 The Cat in the Hat
问题简述
????一只猫高为H, 去打扫一个房间,它很懒惰不想干活, 就从帽子里变出N只猫来帮它干活, 变出来的N只猫的高度为原来那只猫的1 / (N + 1);被变出来的N只猫也很懒惰不想干活,同样从帽子里变出N只猫(每1只变N只)帮自己干活,高度也变成原先的1 / (N + 1),......,直到猫的高度变为1(不具备再变小猫的能力,只能自己干活)为止, 计算有多少只猫不用干活,以及所有猫的高度总和。
问题分析
????假设变了k次:
????N^k=M,所以klog(N)=log(M),所以k=log(M)/log(N);
????H
(1/(N+1))^k=1,所以H=(N+1)^k,所以k=log(H)/log(N+1);
????N从1开始遍历,直到log(M)/log(N)-log(H)/log(N+1) 趋近于0为止。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ1289 UVA107 The Cat in the Hat */

#include <bits/stdc++.h>

using namespace std;

const double ESP = 1e-10;

int main()
{
    int h, num;
    while(~scanf("%d%d", &h, &num) && (h || num)){
        double n = 1;
        while (fabs(log(n) / log(n + 1) - log(num) / log(h) )> ESP)
            n++;
        double k = int(log(h) / log(n + 1) + 0.5);
        if (int(n) == 1)
            printf("%d ", (int)k);
        else
            printf("%d ", int(0.5 + (1 - pow(n, k)) / (1 - n)));
        printf("%d\n", int(0.5 + (1 - pow(n / (n + 1), k + 1)) * (n + 1) * h));
    }

    return 0;
}

POJ1289 UVA107 The Cat in the Hat【暴力】

标签:原来   except   ide   sel   print   help   ecif   event   min   

原文地址:https://www.cnblogs.com/tigerisland45/p/10404410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!