Ultraviolet A 107 The Cat in the Hat (number theory)

Source: Internet
Author: User


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 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
Question:

There is a (SMART) cat who wants to clean the room, but does not want to do it by himself. So let the cat in his hat come out for help. There is a cat in the cat's hat, there are cats in the cat's hats in the hats, and so on. I didn't need any cats to help clean the room, so this (SMART) Cat thought of a note: "As the hats become smaller layer by layer, the height of the CAT also decreases, change to the minimum height of 1 and clean the room from the cat with the height of 1 (input will make the height of the cat on the next layer meet the integer requirements, and the integer (1) of the last cat is equal to the input of workers ). The number of cats in each layer of HATS is a constant, n (we need to find such a constant ourselves ). The height of the CAT in each layer of the hat is 1/(n + 1) times that of the cat on the previous layer. The minimum height of a cat is 1 (the number of leaves). As a result, a cat must clean the room. The height of all cats is an integer. Given the height of the first cat (the Smart cat) and the number of cats (the one in height) to clean the room, find out the number of cats that do not need to clean the room and the total height of all cats.


Solution:

Input H, W.

Set K as the number of layers, Layer 1, Layer 2 ,....

Height: H/(n + 1) h/(n + 1) ^ 2 H/(n + 1) ^ 3 H/(n + 1) ^ 4 ...... H/(n + 1) ^ x = 1

Amount: 1 N ^ 2 N ^ 3 N ^ 4 ...... N ^ x = workers

① H * (1/(n + 1) ^ k = 1; → ③ H = (n + 1) ^ K;

② N ^ K = W;

Place ② into ③, (w ^ (1/K) + 1) ^ K = H. In this case, you only need to enumerate K, and K will not enumerate very large, A maximum of 32 is exceeded Int. Obtain K, calculate n, and then slightly.


Code:

# Include <iostream> # include <cstdio> # include <cmath> # define EPS 1e-9using namespace STD; int firstheight, workers, N, noworkers, sumheight, K; void solve () {noworkers = sumheight = 0; For (k = 0; k <100; k ++) {// K generation. double left = POW (workers, 1.0/K) + 1.0, K * 1.0); If (FABS (left-firstheight) <EPS) break ;} N = POW (Workers * 1.0, 1.0/K) + EPS; // use strong int to add EPS. If it is a double type, do not add EPS. int tempheight = firstheight; For (INT I = 0; I <K; I ++ ) {Noworkers = noworkers + POW (N * 1.0, I) + EPS; sumheight = sumheight + (POW (N * 1.0, I) + EPS) * tempheight + EPS; tempheight = Ceil (tempheight * 1.0/(n + 1); // integer} sumheight + = POW (N * 1.0, k) + EPS;} void output () {printf ("% d \ n", noworkers, sumheight);} int main () {While (scanf ("% d", & firstheight, & Workers) & (firstheight! = 0 | workers! = 0) {solve (); output ();} return 0 ;}





Ultraviolet A 107 The Cat in the Hat (number theory)

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.