Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1730
Question:
For a number N, obtain the maximum Q so that n = a ^ Q, where a, Q, and n are all 32-bit integers. (Multiple groups of data)
Analysis: As long as N is decomposed, gcd of all prime factor numbers can be obtained. Note: N may be a negative number. Therefore, you need to divide the obtained result into 2 for a negative number until the result is not an even number.
The following code breaks down an integer N into a prime power in the form of N = p1 ^ T1 *... * PK ^ TK;
# Include <iostream> <br/> using namespace STD; <br/> int res [1000] [2]; <br/>__ int64 N; <br/> bool s [100010]; <br/> int prime [10000]; <br/> int gcd (int A, int B) <br/>{< br/> If (B = 0) <br/> return a; <br/> return gcd (B, A % B ); <br/>}< br/> void prime () // evaluate the prime number <br/> {<br/> int I, J, K, T; <br/> for (I = 2; I <= 100000; I + = 2) {s [I] = 0; s [I-1] = 1 ;} <br/> S [1] = 0; s [2] = 1; <br/> for (I = 3; I <= 500; I + = 2) <br/> {<br/> If (s [I]) <br/ >{< Br/> K = 2 * I; t = I + k; <br/> while (T <= 100000) {s [T] = 0; T = T + k ;}< br/>}< br/> K = 1; prime [1] = 2; <br/> for (I = 3; I <= 100000; I + = 2) <br/> {<br/> If (s [I] = 1) {k ++; prime [k] = I ;}< br/>}< br/> prime [0] = K; <br/>}< br/>/* int fun () // integer decomposition n = p1 ^ T1 *... * PK ^ TK <br/> {// you do not know where the error is ~~ Wa several times, <br/> int maxn =-1, I; <br/> int sum = 0; <br/> for (I = 1; I <= prime [0]; I ++) <br/>{< br/> If (N % prime [I] = 0) <br/>{< br/> sum ++; <br/> res [Sum] [1] = prime [I]; <br/> res [Sum] [0] = 0; <br/> while (N % prime [I] = 0) <br/>{< br/> res [Sum] [0] ++; <br/> N/= prime [I]; <br/>}< br/> return sum; <br/>}< br/> */<br/> int fun () // integer decomposition n = p1 ^ T1 *... * PK ^ TK <br/>{< br/> int I, S, K = 0; <br/> for (I = 1; I <= prime [0]; I ++) <br/>{< br/> S = 0; <br/> while (N % prime [I] = 0) <br/>{< br/> S ++; <br/> N/= prime [I]; <br/>}< br/> If (S> 0) <br/>{< br/> K ++; <br/> res [k] [1] = prime [I]; // memory factor <br/> res [k] [0] = s; // storage power <br/>}< br/> If (n = 1) break; <br/> If (N/prime [I] <prime [I]) <br/>{< br/> K ++; <br/> res [k] [1] = N; <br/> res [k] [0] = 1; <br/> n = 1; <br/> break; <br/>}< br/> re S [0] [1] = res [0] [0] = K; <br/> return K; <br/>}< br/> int main () <br/>{< br/> int I, ANS, sum, flag; <br/> prime (); <br/> while (scanf ("% i64d ", & N) <br/>{< br/> flag = 0; <br/> If (n <0) // negative ~~~ <Br/>{< br/> N =-N; <br/> flag = 1; <br/>}< br/> sum = fun (); <br/> ans = res [1] [0]; <br/> for (I = 2; I <= sum; I ++) <br/> ans = gcd (ANS, res [I] [0]); <br/> If (flag & Ans % 2 = 0) // n is a negative number, which can be processed in multiple steps. <br/>{< br/> while (ANS % 2 = 0) <br/> ANS/= 2; <br/> printf ("% d/N", ANS); <br/>}< br/> else <br/> printf ("% d/N ", ans); <br/>}< br/> return 0; <br/>}< br/>
The following code is written using the POW () function. However, you must pay attention to the precision when using this function. After reading discuss, you will know that the accuracy must be set to 1e-12 before you can use the AC ~~
# Include <iostream> <br/> # include <cmath> <br/> using namespace STD; <br/> # define maxn 1000 <br/> # define IP 1e-12 // accuracy problems ~ <Br/>__ int64 N; <br/> double TEP1, tep2; <br/> void fun () <br/>{< br/> int I, start = 32, flag = 1; // case of integer flag = 1; <br/> If (n <0) <br/>{< br/> Start = 31; <br/> flag = 2; // flag = 2 for negative numbers; <br/> N =-N; <br/>}< br/> Double P; <br/> for (I = start; I> = 1; I-= Flag) <br/> {<br/> P = POW (N * 1.0, 1.0/I); <br/> TEP1 = floor (p); // round down <br/> tep2 = Ceil (P ); // rounded up <br/> // cout <ABS (p-tep1) <Endl; 0abs (x): X is an integer; <br/> // cout <FABS (p-tep1) <Endl; 0 .~~ FABS (x): X is a floating point number; returns the absolute value; <br/> If (FABS (p-TEP1) <IP | FABS (tep2-p) <= IP) <br/>{</P> <p> printf ("% d/N", I); <br/> break; <br/>}< br/> int main () <br/>{< br/> while (scanf ("% i64d", & N) <br/>{< br/> fun (); <br/>}< br/> return 0; <br/>}