HDU 6623 Minimal Power of Prime

米勒罗宾超时,别试着优化了
比较容易想到先把小区间的素数筛出来,然后除掉这部分,然后剩下的因子肯定不会很多,直接特判就行了,我看的题解是用的[1-1e4]的区间去筛,然后剩下的质因子不会超过四个。
则:

  • n = x 4 n= x^{4} n=x4,ans = 4
  • n = x 3 n= x^{3} n=x3,ans = 3
  • n = x 2 n= x^{2} n=x2,ans = 2

否则 ans =1 ,可以尝试用手写一遍有哪几种情况.

然后问题变为了如何判断 n 为一个数的几次方,很容易想到二分(这个很稳定,也好写),或者 pow 函数,但是 pow 会有误差,毕竟 double 和 long double 精度都没有long long 高,并且 pow真香

注:我起初用的是( 判断 n 是否为 x 的3次方 )

LL s=pow(n+0.5,1.0/3);

然后WA到自闭,然后用

LL s=pow(n,1.0/4)+0.3;

就过了,经过漫长的debug,后面发现 当 上面这个式子误差第一个式子误差挺大的,比如 n = 10000 7 3 n=100007^{3} n1000073两个s不一样!!!,就这样有了较大误差(玄学误差)

#include<vector>
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<map>
#include<cmath>
#include<queue>
#include<cstring>
#define LL long long
using namespace std;
const int N=1e4+5;
const int M=3e6+5;
const int INF=1e5;
int pri[N],len;
void getpri(){
    for(int i=2;i<N;i++)
    if(!pri[i]){
        pri[++len]=i;
        for(int j=i+i;j<N;j+=i)
            pri[j]=1;
    }
}
int main(){

    getpri();
    int t;cin>>t;
    while(t--){
        LL n;scanf("%lld",&n);
        int ans=INF;
        for(int i=1;i<=len;i++)
        {
            if(n==1)break;
            if(n%pri[i])continue;
            int x=0;
            while(n%pri[i]==0)n/=pri[i],x++;
            ans=min(x,ans);
        }
        if(n==1){printf("%d\n",ans);continue;}

        LL s=pow(n,1.0/4)+0.5;
        if(s*s*s*s==n){printf("%d\n",min(ans,4));continue;}
        s=pow(n,1.0/3)+0.5;
        if(s*s*s==n){printf("%d\n",min(ans,3));continue;}
        s=pow(n,1.0/2)+0.5;
        if(s*s==n){printf("%d\n",min(ans,2));continue;}
        printf("1\n");
    }

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值