Ultraviolet A 107-The Cat in the Hat

Source: Internet
Author: User
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 rights t 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 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 shoshould be one output line for each input line other than the ''0 0 ''That terminates input.

Sample Input
216 1255764801 16796160 0
Sample output
31 671335923 30275911

Which of the following are not translated by me... For reference

A magical Smart cat walked into a messy room. He didn't want to clean up on his own. He decided to seek help to work. So he changed n kittens from his hat to help him (the height of the changed cat is 1/(n + 1) of the original CAT )). These kittens also have hats, so each of them changes from his hats to the next cat to help him. Keep going until these little ones .... cats can no longer be smaller (Height = 1), their hats cannot be changed to smaller cats to help, and the smallest cats have to clean the room. Note: The height of all cats is a positive integer.

In this case, we will give you the height of the first cat and the number of cats that finally work (that is, the number of cats with a height of 1 ). Please find out how many cats are not working, and the total height of all cats.

Hight number
216 1
36 5
6 25
1 125

N = 5;

671 = 216*1 + 36*5 + 6*25 + 1*125

N ^ n = Number

(N + 1) ^ n = hight

LG (N)/lg (n + 1) = lg (number)/lg (hgiht)

Then we can use the binary search to calculate n, and the condition is written as FABs (lg (n) * lg (hgiht)-lg (number) * lg (n + 1) <EPS)

Reference http://blog.csdn.net/frankiller/article/details/7726744

1 # include <cstdio> 2 # include <iostream> 3 # include <cmath> 4 using namespace STD; 5 6 # define EPS 1e-9 7 8 int main () 9 {10 int hight, num; 11 int left, right, mid; 12 int x, Y; 13 while (scanf ("% d", & hight, & num) & (hight + num) 14 {15 left = 1; 16 Right = 10000000; 17 while (left) 18 {19 mid = (left + right)/2; 20 // This is the solution of the equation. The obtained mid is n21 if (FABS (log (MID) * log (hight)-log (Mid + 1) * log (Num )) <= EPS) // lg (N)/lg (n + 1) may be equal to 0... N = 122 break; 23 if (log (MID) * log (hight)-log (Mid + 1) * log (Num)> 0) 24 Right = mid; 25 else26 left = mid; 27} 28 29 // X is calculated. Except for the first cat, Y is the total height of 30 x = 0 for the number of other cats; 31 y = hight; 32 left = 1; 33 while (hight> 1) 34 {35 hight/= (1 + mid); 36 left * = mid; 37 x + = left; 38 y + = hight * left; 39} 40 printf ("% d \ n", X-num + 1, y ); 41} 42 43 return 0; 44}

 

Ultraviolet A 107-The Cat in the Hat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.