UVa 107-the Cat in the Hat

Source: Internet
Author: User

Title Source: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 hat he was wearing has a rather nifty feature.

With one flick of him 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 has to-clean all the grime,
And they ' re tired of doing it time after time!

The problem

A clever cat walks into a messy the which he needs to clean. Instead of doing the work alone, it decides to has its helper cats does the work. It keeps It (smaller) helper cats inside its hat. Each helper cat also have helper cats in it own hat, and so on. Eventually, the cats reach a smallest size. These smallest cats has no additional cats in their hats. These unfortunate smallest cats has 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 the cat whose hat they am in.

The
smallest cats is of height one;
These is the cats that get the work done.

All heights is positive integers.

Given the height of the initial cat and the number of worker cats (of height one), find the number of cats that is not does ing 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 are a single line consisting of the 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 is not working, followed by a space, follow Ed by the height of the stack of cats. There should is 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
Problem Solving Ideas:
The Cat in the Hat,
This question gives the condition that the first cat's height H and the last working cats the number of W.
The following analyses the relationship between the height of the cat and the number of cats:

The height of the starting cat is H, and N is the number of cats inside each hat, set an intermediate variable s.

Height of the cat h/(n + 1) H/(n + 1) 2 ... h/(n + 1) s

The number of cats 1 n n * N ..... Ns

When the number of cats that last worked is w:
LG (N)/lg (n+1) =lg (num)/lg (HGIHT)
is to use binary search to calculate n, the condition is written as fabs (LG (n) *lg (height)-lg (num)/lg (n+1) <0.00001)
Paste the following code:
1#include <bits/stdc++.h>2 3 using namespacestd;4 5 intMain ()6 {7     intH,num;8      while(~SCANF ("%d%d",&h,&num))9     {Ten         if(0==h&&0==num) One              Break; A         intleft=1, right=10000000, mid; -          while(left) -         { theMid= (left+right)/2; -             if(Fabs (mid) *log (h)-log (mid+1) *log (num)) <=0.000001) Break; -             if(mid) *log (h)-log (mid+1) *log (num) >0) -right=mid; +             Else -left=mid; +         } A         intx=0, y=h; atleft=1; -          while(h>1) -         { -H/= (1+mid); -left*=mid; -x+=Left ; iny+=h*Left ; -         } toprintf"%d%d\n", x-num+1, y); +     } -}



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