首页 > 代码库 > The Cat in the Hat UVA 107

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 homage to Theodore Seuss Geisel)

向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 w

一只聪明的猫走进了一个需要他打扫的房子。他决定让他的帮手猫来做这些,这样他就不用做了。

ork. 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 rea

他把他的帮手猫藏在他的帽子里。每个帮手猫的帽子里同样有自己的帮手猫。最终,他们找到了体型最小的猫。

ch 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 c

在每个猫的帽子里的猫的个数是个常数N。帽子里的猫的高度是拥有这顶帽子的猫的高度的1/(N+1)

at whose hat they are in.

The smallest cats are of height one; 
最小的猫的高度为1
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 (cat

给定最开始那个猫的高度以及工作的猫的个数(高度为1),找到那些不工作的猫(即高度大于1)

s 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 an

并且求出所有猫的总高度

other).

The Input

The input consists of a sequence of cat-in-hat specifications. Each specification is a single line consisting of two positive integers, sepa

输入包含一系列的“帽子里的猫”的参数。每组参数有一行,由两个由空格隔开的正整数组成。

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

由一对0组成的一行代表输入结束

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 o

对于每组输入,输入不工作的猫的个数,之后输出一个空格,接着再输出猫的总高度。‘0 0’的那行不输出

f 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

源代码:

#include <stdio.h>

int main(){
  long long init,work_num,sum,num,t1,t2;
  long long i,j,k;

  //freopen("input.txt","r",stdin);
  while(scanf("%lld%lld",&init,&work_num)){
    if(init==0&&work_num==0)
        break;
    if(init==work_num+1){//初始猫的帽子里就是高度为1的猫
        printf("1 %lld\n",work_num+init);
        continue;
    }

    for(i=1;;i++){//找到连续的两个整数作为底使输入的两个数的次数相等
        t1=init;
        t2=work_num;
        k=0;
        while(t2%i==0&&t1%(i+1)==0){
            t2/=i;
            t1/=(i+1);
            k+=t2;//纪录不为1的猫的个数
        }
        if(t1==1&&t2==1)
            break;
    }

    sum=0;
    num=1;
    while(init!=1){//总高度
        sum+=init*num;
        init/=i+1;
        num*=i;
    }
    sum+=work_num;

    printf("%lld %lld\n",k,sum);
  }

  return 0;
}