UVa 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 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 smallestx 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

 

设输入的第一个数据为x,第二个为y,

题中给的模型刚好类似一棵满n叉树,树高为(m+1),

那么可以推出x=(n+1)^m;y=n^m;

写程序求出m,n即可得出结论

 

 1 #include<iostream>
 2 #include<cstdio>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     long long x,t;
 9     while(scanf("%lld %lld",&x,&t)&&(x||t))
10     {
11         long long m,n;
12 
13         bool flag=true;
14         if(x==1)
15             printf("0 %lld\n",t);
16         else
17         {
18             for(n=1;flag&&n<=t;n++)
19             {
20                 for(m=1;flag;m++)
21                 {
22                     long long temp=1;
23                     for(long long i=1;i<=m;i++)
24                         temp*=n+1;
25                     if(temp==x)
26                     {
27                         long long tmp=1;
28                         for(long long j=1;j<=m;j++)
29                             tmp*=n;
30                         if(tmp==t)
31                             flag=false;
32                     }
33                     else if(temp>x)
34                         break;
35                 }
36             }
37             n--;
38             m--;
39             if(!flag)
40             {
41                 printf("%lld ",n==1?m:(t-1)/(n-1));
42 
43                 long long total=x;
44                 long long power=1;
45                 for(long long i=1;i<=m;i++)
46                 {
47                     power*=n;
48                     x/=n+1;
49                     total+=x*power;
50                 }
51                 printf("%lld\n",total);
52             }
53         }
54     }
55 
56     return 0;
57 }
[C++]

 

posted @ 2014-01-13 11:33  ~~Snail~~  阅读(231)  评论(0编辑  收藏  举报