// Va problem 107-The Cat in the Hat // verdict: accepted // submission date: 2011-11-23/Ultraviolet Run Time: 1.132 S // All Rights Reserved (c) 2011, Qiu. Metaphysis # Yeah dot net // [problem solving method] // because the question does not specify the range of input data, it is only possible to cross the river by feeling the stone. This topic is based on a 32-bit integer input. //// When there is only one working cat, there is only one cat in each hat or only the cat initially specified. When only the specified cat is initially entered, // the corresponding input should be: 1, 1. // When each hat has only one cat, the corresponding input should be: X, 1. X is the power of 2. These two special cases can be separated and resolved. /// In general, set the number of nested layers of the hat to m, the number of cats in each hat to N, and the initial height of the cat to, if the number of working cats is/B, the following equation is true (B> = 2, n> = 2, m> = 1): // (n + 1) ^ m = A/n ^ m = B // obviously, both A and B are equal to the sum greater than 1. If the input data is within the 32-bit integer range, A and B can be decomposed into the product of prime numbers smaller than 2 ^ 16. For decomposition of a, assume that the decomposition formula is a = a ^ K * B ^ L * C ^ m *... * G ^ Q. // since a is the M power of a number, the exponent of the prime factor of A must be a multiple of M. Similarly, in the prime factor factorization type of B, the index of each prime factor is also a multiple of M, so m must be the maximum common divisor of all prime factor indexes (why? We only need to prove that the maximum common divisor of all indexes in the factorization formula of N + 1 // and N is 1. We can prove it by Reverse verification, assume that, in the prime factor /// factorization formula of N + 1 and N, the maximum common divisor of all indexes is a number W greater than 1, so we may consider n + 1 = a ^ W, N = B ^ W, // a ^ w-B ^ W = 1, A, B, W> = 2, and it is an integer, due to N + 1> N, so if there is a> B, set a = B + x // where x> = 1, then there is (B + x) ^ w-B ^ W = 1, since W> = 2, x> = 1, the upper formula // cannot be true according to the binary theorem ). //// Then we can perform prime factor decomposition on A and B, and obtain m using the method of finding the maximum common divisor of all indexes, and then obtain n Based on the prime factor decomposition result. //// If the input data exceeds the 32-bit integer range, this method can be used as long as the prime number decomposition algorithm is not too slow, but with the increase of the // given data range, if the number of prime numbers increases, the method of generating prime numbers in the specified range cannot be used in advance. Instead, we should seek other methods that are not too inefficient. //// Another way to solve the problem is to use the binary method to search for M and N values based on the equation. I have not tried this method in detail. # Include <iostream> # include <cstring> # include <cmath> using namespace STD; # define maxint (1 <16) # define maxn 6600int prime [maxn], nprime = 0, factor [maxn]; // prime number determination. Bool isprime (int x) {If (x <= 1) return false; If (x = 2) return true; If (X & 1) {int Top = SQRT (x); For (INT I = 3; I <= top; I + = 2) if (X % I) continue; else return false; return true;} else return false;} // generates all prime numbers within the specified range. Void getprime (void) {for (INT I = 2; I <= maxint; I ++) if (isprime (I) prime [nprime ++] = I ;} // calculate the maximum public approx. Int gcd (int x, int y) {return x <Y? Gcd (Y, X): (Y? Gcd (Y, X % Y): X);} // prime factor decomposition and obtain the maximum approximate number of indexes. Int factoring (int x) {int y = 0; memset (factor, 0, sizeof (factor); For (INT I = 0; I <nprime & x> 1; I ++) while (X % prime [I] = 0) {factor [I] ++; X/= prime [I] ;}for (INT I = 0; I <maxn; I ++) if (factor [I]) {If (y) y = gcd (Y, factor [I]); else y = factor [I];} return y;} // special case processing. Void specialcase (INT initcatheight, int workercatsnumber) {If (initcatheight = 1) cout <"0 1" <Endl; else {int nest = 1; for (INT I = 2; I <initcatheight; I * = 2, nest ++); cout <nest <"" <(POW (2, nest + 1) -1) <Endl ;}} int main (INT argc, char const * argv []) {int nest, N; getprime (); int initcatheight, workercatsnumber; while (CIN> initcatheight> workercatsnumber, initcatheight | workerca Tsnumber) {// special case processing. If (workercatsnumber = 1) {specialcase (initcatheight, workercatsnumber); Continue ;}// use prime factor decomposition to find the number of nested layers. Nest = factoring (initcatheight); nest = gcd (nest, factoring (workercatsnumber); // obtain N using the prime factor decomposition result and nested layers. N = 1; for (INT I = 0; I <maxn; I ++) if (factor [I]) N * = POW (prime [I], factor [I]/nest); // calculates the total number of idle cats and the height of all cats. Int unworkcatsnumber = 0, totalheight = 0; while (workercatsnumber> 1) {workercatsnumber/= N; unworkcatsnumber + = workercatsnumber;} int exponent = 1; while (initcatheight> = 1) {totalheight + = initcatheight * exponent; exponent * = N; initcatheight/= (n + 1) ;}// output. Cout <unworkcatsnumber <"" <totalheight <Endl;} return 0 ;}