uva107 The Cat in the Hat

The Cat in the Hat

Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu

Submit Status Practice UVA 107

Appoint description:

Description

Download as PDF

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

/*
Author:ZCC;
Time:2015-6-6
Solve:题目大意:有一只高H的猫, 要去打扫一个房间,但是它很懒惰,
就从帽子里变出N只猫来帮它干活, 变出来的N只猫的高度为原来那只猫的1 / (N + 1),
可是被变出来的N只猫也很懒惰, 也不想干活,同样从帽子里变出N只猫帮自己干
(注意,是1只变出N只, 不是总共N只),高度同样是原先的1 / (N + 1),...
直到小猫的高度为1时, 不具有再变小小猫的能力, 只能自己干活, 现在给出始祖猫的高度H,
 和最终苦逼干活猫的数量M, 求有多少只猫不用干活,和所有猫的高度总和。
解题思路:首先设变幻了k次,就有
<1>N ^ k = M   ==>  k * log(N) = log(M) ==>  k = log(M) / log(N).
<2>H * (1 / (N + 1)) ^ k = 1 ==>  H = (N + 1) ^ k  == >  k = log(H) / log(N + 1).
<3>N从1开始遍历, 直到log(M) / log(N) - log(H) / log(N + 1) 趋近于0
接下来利用等比数列的求和公式
*/

#include<iostream>
#include<algorithm>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
const int maxn=55;
typedef long long LL;
int a[maxn];
int main(){
    #ifndef ONLINE_JUDGE
   // freopen("Text//in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int H,M;
    while(~scanf("%d%d",&H,&M)&&(H||M)){
        int n=1;
        while(fabs(log(n) / log(n+ 1) - log(M) / log(H) )> 1e-10)n++;
        int k=(int)(log(H)/log(n+1)+0.5);
        if(n==1)
            printf("%d ",k);
        else
            printf("%d ",(int)(0.5+(1-pow(n,k))/(1-n)));
        printf("%d\n",int(0.5 + (1 - pow(n*1.0 / (n + 1), k + 1)) * (n + 1) * H));
    }
    return 0;
}

/*
Sample Input
2
12
-3646397
Sample Output
7
2701
*/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-09-29 08:28:36

uva107 The Cat in the Hat的相关文章

POJ1289 UVA107 The Cat in the Hat【暴力】

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. Wi

The Cat in the Hat UVA 107

说说:这道题开始还挺难理解的,一直搞不清到底要干嘛.其实,题意就是有一只猫,高度就是输入的init.它的帽子里有N只猫,每只猫的高度都是init的N+1分之一.然后这N只猫的帽子里同样有N只猫(其中中N为常数),大小类推....最后当猫的高度为1时,结束.输出高度不为1的猫的个数,并且输出这些猫的总高度.这里有几个特殊情况需要注意,就是第一只猫的帽子里就是高度为1的猫,另一种情况就是每个猫的帽子里都只有一只猫. 题目: The Cat in the Hat Background (An homa

UVa 107 - The Cat in the Hat

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=43  The Cat in the Hat  Background (An homage to Theodore Seuss Geisel) The Cat in the Hat is a nasty creature,But the striped

UVA 107 The Cat in the Hat (数论)

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

杂题 UVAoj 107 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

更换Red Hat Enterprise Linux 7 64位的yum为centos的版本

查看redhat原有的yum包有哪些: [[email protected] ~]# rpm -qa|grep yum yum-utils-1.1.31-24.el7.noarch yum-langpacks-0.4.2-3.el7.noarch yum-metadata-parser-1.1.4-10.el7.x86_64 yum-rhn-plugin-2.0.1-4.el7.noarch PackageKit-yum-0.8.9-11.el7.x86_64 yum-3.4.3-118.el7

HDU-1053-Entropy(Huffman编码)

Problem Description An entropy encoder is a data encoding method that achieves lossless data compression by encoding a message with "wasted" or "extra" information removed. In other words, entropy encoding removes information that was

【Python五篇慢慢弹(5)】‘类’过依然继续前行,直至ending再出发

‘类’过依然继续前行,直至ending再出发 作者:白宁超 2016年10月10日22:36:57 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给出的pythondoc入门资料包含了基本要点.本文是对文档常用核心要点进行梳理,简单冗余知识不再介绍,作者假使你用c/java/c#/c++任一种语言基础.本系列文章属于入门内容,老鸟可以略看也可以略过,新鸟可以从篇一<快速上手学python>先接触下python怎样安装与运行,以及pychar

Hdu 1053 Entropy

Entropy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4171    Accepted Submission(s): 1703 Problem Description An entropy encoder is a data encoding method that achieves lossless data compress