题意:给你一个x,求满足x=a^p这个式子的最大的p
题解:唯一分解定理
要使p最大,那么a必然最小,根据因子公式可得a为所有素因子的gcd。
注意判断正负,对于负数p必然是奇数,循环右移即可。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<map>
#define ll long long
using namespace std;
//素数筛选和合数分解
const int MAXN = 1e7 + 10;
bool notprime[MAXN];
int prime[MAXN / 10];
void getPrime() {
memset(notprime, false, sizeof(notprime));
notprime[0] = notprime[1] = true;
prime[0] = 0;
for (int i = 2; i < MAXN; i++) {
if (!notprime[i])prime[++prime[0]] = i;
for (int j = 1; j <= prime[0] && prime[j] < MAXN / i; j++) {
notprime[prime[j] * i] = true;
if (i % prime[j] == 0) break;
}
}
}
long long factor[1000][2];
int fatCnt;
int getFactors(long long x) {
fatCnt = 0;
long long tmp = x;
for (int i = 1; i < prime[0] && prime[i] <= tmp / prime[i]; i++) {
factor[fatCnt][1] = 0;
if (tmp % prime[i] == 0) {
factor[fatCnt][0] = prime[i];
while (tmp % prime[i] == 0) {
factor[fatCnt][1]++;
tmp /= prime[i];
}
fatCnt++;
}
}
if (tmp != 1) {
factor[fatCnt][0] = tmp; //存放分解的素数
factor[fatCnt++][1] = 1; //存放此素数出现次数
}
return fatCnt; //分解的素数个数(不相同)
}
int t;
ll n;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int main() {
getPrime();
scanf("%d", &t);
int cas = 0;
while (t--) {
scanf("%lld", &n);
int flag = 1;
ll ans;
if (n < 0) {
n = -n;
flag = 0;
}
getFactors(n);
ans = factor[0][1];
for (int i = 1; i < fatCnt; i++) {
ans = gcd(ans, factor[i][1]);
}
if (flag) {
printf("Case %d: %lld\n", ++cas, ans);
}
else {
while (!(ans & 1)) {
ans /= 2;
}
printf("Case %d: %d\n", ++cas, ans);
}
}
return 0;
}