HDU 6623 Minimal Power of Prime

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

  • n=x4n= x^{4},ans = 4
  • n=x3n= x^{3},ans = 3
  • n=x2n= x^{2},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,后面发现 当 上面这个式子误差第一个式子误差挺大的,比如n1000073n=100007^{3}两个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");
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章