Uva oj 107-The Cat in the Hat (cat wearing a hat)

Source: Internet
Author: User

Time Limit: 3.000 seconds

 

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 is1/(n + 1)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 125
5764801 1679616
0 0

 

Sample output

31 671
335923 30275911

 

Analysis

AlgorithmIt is not complicated, mainly because of the processing of floating point Precision issues. When you use log to retrieve the logarithm and then perform an integer operation, pay attention to 4 homes and 5 inputs. Starting with the number of working cats, because it must be the number of workers, all possible factors in the examples can find the value of N. Experiments show that in the data given by OJ, the height of the first cat must be the sum of an equi-ratio series (including 1). Therefore, if we can directly calculate the ratio based on the Equi-ratio series formula, that is, n + 1. However, I have not found a better solution. I can only calculate it based on the old method.

 

Solution

 

# Include <iostream> # include <math. h> using namespace STD; int main (void) {int nfirstheight, nworking; while (CIN >>> nfirstheight >>> nworking) {If (nfirstheight = 0) {break ;} int ncatcnt = 0, ntotalheight = 0; If (nworking = 1) {for (int K = nfirstheight; k> = 1; k/= 2, ++ ncatcnt) {ntotalheight + = K;} cout <ncatcnt-1 <''<ntotalheight <Endl; continue;} float flog = Log (float) nworking), flevel; int nsqrt = (INT) (SQRT (float) nworking) + 0.5); int nlevel = 0, nheight = nfirstheight, nbranch = 2; for (; nbranch <= nsqrt; ++ nbranch) {flevel = flog/log (float) nbranch); int ntemplevel = (INT) (flevel + 0.5); If (FABS (flevel-ntemplevel )) <1e-4f) {nlevel = ntemplevel + 1; if (INT) (POW (float) (nbranch + 1), ntemplevel) + 0.5) = nfirstheight) {break ;}}if (nlevel = 0) {nlevel = 2; nbranch = nworking;} For (int K = 0, ncats = 1; k <nlevel-1; ++ K) {ncatcnt + = ncats; ntotalheight + = ncats * nheight; ncats * = nbranch; nheight/= (nbranch + 1 );} cout <ncatcnt <''<ntotalheight + nworking <Endl;} return 0 ;}

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.