Uva107 the Cat in the Hat

Source: Internet
Author: User

The Cat in the Hat
Time limit:3000ms Memory limit:0kb 64bit IO format:%lld &%llu
Submit Status Practice UVA 107
Appoint Description:

Download as PDF


(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 tex2html_wrap_inline35 times the height of the the cat whose hat they was in.

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 and than the "0 0" that terminates input.

Sample Input

216 125
5764801 1679616
0 0

Sample Output

31 671
335923 30275911

/*AUTHOR:ZCC; Time:2015-6-6solve: The main idea: there is a high-H cat, to clean a room, but it is very lazy, from the hat to make N a cat to help it work, the height of the cat changed out of the original 1/(n + 1), but the change out of the N cat is also very lazy, do not want to work, Also from the hat out of n cats to help themselves dry (note that it is 1 to change the N, not a total of n), the height is also the original 1/(N + 1),... Until the kitten's height of 1 o'clock, do not have the ability to change the small cat, can only work on their own, now give the ancestor of the height of the cat H, and the end of the hard work of the cat number m, ask how many cats do not work, and the height of all cats combined. Problem solving ideas: First set the change k times, there is <1>n ^ k = M ==> k * log (n) = log (m) ==> k = log (m)/log (n). <2>h * (1/(n + 1)) ^ k = 1 ==> H = (n + 1) ^ k = = > k = log (h)/log (n + 1). <3>n is traversed from 1 until log (M)/log (n)-log (h)/log (n + 1) Approaching 0 next use the summation formula of geometric series * /#include <iostream>#include <algorithm>#include <map>#include <cstdio>#include <cstdlib>#include <vector>#include <cmath>#include <cstring>#include <string>using namespace STD;Const intmaxn= -;typedef Long LongLL;intA[MAXN];intMain () {#ifndef Online_judge   //Freopen ("Text//in.txt", "R", stdin);    #endif //Online_judge     intH,m; while(~scanf("%d%d", &h,&m) && (h| | M)) {intn=1; while(fabs(Log(n)/Log(n+1) -Log(M)/Log(H)) >1e-10) n++;intK= (int)(Log(H)/Log(n+1)+0.5);if(n==1)printf("%d", k);Else            printf("%d",(int)(0.5+(1-POW(n,k)) /(1-n)));printf("%d\n",int(0.5+ (1-POW(n1.0/(n +1), K +1)) * (n +1) (H)); }return 0;}/*sample input212-3646397sample output72701*/

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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