Case可以写成一个数b的p次幂。求出p的最大值。其实就是质数分解问题的转化。将Case质数分解后,求出所有指数的最大公约数,如2^3*5^6 = ( 2 * 5 ^ 2 ) ^ 3.如果是负数呢。
如果最大公约数为偶数,那么负数的偶数次幂一定是正数,不合题。所以将最大公约数不断除2直到除为奇数为止。
/*************************************************************************
> File Name: 10622.cpp
> Author: Toy
> Mail: ycsgldy@163.com
> Created Time: 2013年06月03日 星期一 14时44分35秒
************************************************************************/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
const int INF = 0x7fffffff;
typedef pair<int, int> II;
typedef vector<int> IV;
typedef vector<II> IIV;
typedef vector<bool> BV;
typedef long long i64;
typedef unsigned long long u64;
typedef unsigned int u32;
#define For(t,v,c) for(t::const_iterator v=c.begin(); v!=c.end(); ++v)
#define IsComp(n) (_c[n>>6]&(1<<((n>>1)&31)))
#define SetComp(n) _c[n>>6]|=(1<<((n>>1)&31))
const int MAXP = 46341; //sqrt(2^31)
const int SQRP = 216; //sqrt(MAX)
int _c[(MAXP>>6)+1];
IV primes;
IIV opt;
int Case;
void prime_sieve ( ) {
for ( int i = 3; i <= SQRP; i += 2 )
if ( !IsComp ( i ) ) for ( int j = i * i; j <= MAXP; j += i + i ) SetComp ( j );
primes.push_back ( 2 );
for ( int i = 3; i <= MAXP; i += 2 ) if ( !IsComp ( i ) ) primes.push_back ( i );
}
void prime_factorize ( int n, IIV &f ) {
f.clear ( );
int sn = sqrt ( n );
For ( IV, it, primes ) {
int prime = *it;
if ( prime > sn ) break; if ( n % prime ) continue;
int e = 0; for ( ; n % prime == 0; e++, n /= prime );
f.push_back ( II ( prime, e ) );
sn = sqrt ( n );
}
if ( n > 1 ) f.push_back ( II ( n, 1 ) );
}
int main ( ) {
prime_sieve ( );
while ( scanf ( "%d", &Case ) == 1 ) {
if ( Case == 0 ) break;
if ( Case == ( -1 << 31 ) ) {
printf ( "31\n" );
continue;
}
prime_factorize ( abs ( Case ), opt );
int tmp = 1;
bool first = 1;
For ( IIV, it, opt ) {
if ( first ) tmp = it -> second, first = 0;
else tmp = __gcd ( tmp, it -> second );
}
if ( Case < 0 ) {
while ( tmp % 2 == 0 ) {
tmp /= 2;
}
}
printf ( "%d\n", tmp );
}
return 0;
}