Mysterious Bacteria LightOJ - 1220 指数的唯一分解(数论专题)

求满足条件的最大的指数p,使得a^p = x(a是整数)

Input
输入包含多组样例T(T<=50),每个样例包括一个整数x,x的范围在32位二进制数以内, 保证x的绝对值大于等于2。

Output
对于每个样例输出最大的指数p。

Sample Input
3

17

1073741824

25

Sample Output
Case 1: 1

Case 2: 30

Case 3: 2
对指数进行记录质数的指数,并记录指数的gcd,记得会出现负数要化成正数并打标记最后处理

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<stdlib.h>
 
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
 
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=1e7+6;//maxn*maxs
const int maxs=1e8+10;
const int mod=998244353;
ll pri[maxn / 10], cnt;
bool ispri[maxn];
void init() 
{
    for(int i = 2; i <maxn; i++) {
        if(!ispri[i]) {
            pri[cnt++] = i;
        }
        for(int j = 0; j < cnt && pri[j] * i <maxn; j++) {
            ispri[i * pri[j]] = true;
            if(pri[j] % i == 0) break;
        }
    }
}//素数筛
int gcd(int a, int b)
{
    int tmp;
    while(b) {
        tmp = a % b;
        a = b;
        b = tmp;
    }
    return a;
}
int main()
{
	init();
	int t;
	cin>>t;
	ll tmp,ans;
	int cas=0;
	while(t--){
		ll n;
		cin>>n;
		int flag;
		if(n<0) n=-n,flag=-1;
		else flag=1;
		int ans=0,m;
		for(int i=0;i<cnt&&pri[i]<=n;i++){
			if(n%pri[i]==0){
				m=0;
				while(n%pri[i]==0){
					n/=pri[i];
					m++;
				}
				if(ans==0) ans=m;
				else ans=gcd(ans,m);
				if(ans==1) break;
			}
		}
		if(n!=1) ans=1;
		if(flag==-1&&!(ans&1)){
			while(!(ans&1)){
				ans>>=1;
			}
		}
		printf("Case %d: %d\n", ++cas, ans);
	}
    return 0;
}
	

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值