2020ccpc威海D题 ABC Conjecture(规律+pollard_rho大数质因数分解模板)

思路1:打表可得如果有两个以及以上的相同质因数,即可yes; 

/*来自:李双智学长模板*/
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<map>
#include<stdlib.h>
#include<time.h>
#define times 20
using namespace std;
long long total;
long long factor[110];
map<long long,long long>mmp;///最后用来存质因子的幂的
long long qmul(long long a,long long b,long long M){
    a%=M;
    b%=M;
    long long ans=0;
    while (b){
        if (b&1){
            ans=(ans+a)%M;
        }
        a=(a<<=1)%M;
        b>>=1;
    }
    return ans%M;
}//快乘,因为两个longlong的数相乘可能会溢出,所以这里转乘法为加法,思想和快速幂相似
long long qpow(long long a,long long b,long long int M){
    long long ans=1;
    long long k=a;
    while(b){
        if(b&1)ans=qmul(ans,k,M)%M;
        k=qmul(k,k,M)%M;
        b>>=1;
    }
    return ans%M;
}//快速幂
bool witness(long long a,long long n,long long x,long long sum){
    long long judge=qpow(a,x,n);
    if (judge==n-1||judge==1)return 1;
    while (sum--){
        judge=qmul(judge,judge,n);
        if (judge==n-1)return 1;
    }
    return 0;
}
bool miller(long long n){
    if (n<2)return 0;
    if (n==2)return 1;
    if ((n&1)==0)return 0;
    long long x=n-1;
    long long sum=0;
    while (x%2==0){
        x>>=1;
        sum++;
    }
    for (long long i=1;i<=times;i++){
        long long a=rand()%(n-1)+1;
        if (!witness(a,n,x,sum))return 0;//费马小定理的随机数检验
    }
    return 1;
}//判断一个数是否为素数
long long gcd(long long a,long long b){
    return b==0?a:gcd(b,a%b);
}//欧几里得算法
long long pollard(long long n,long long c){
    long long x,y,d,i=1,k=2;
    x=rand()%n;
    y=x;
    while (1){
        i++;
        x=(qmul(x,x,n)+c)%n;
        d=gcd(y-x,n);
        if (d<0)d=-d;
        if (d>1&&d<n)return d;
        if (y==x)return n;
        if (i==k){
            y=x;
            k<<=1;
        }
    }
}
void find(long long n){
    if (miller(n)){
        factor[++total]=n;
        return ;
    }
    long long p=n;
    while (p>=n){
        p=pollard(p,rand()%(n-1)+1);
    }
    find(n/p);
    find(p);
}//寻找这个数的素因子,并存起来
int main(){
    long long n,m,i,t;
    scanf("%lld",&t);
    while (t--){
        mmp.clear();
        scanf("%lld",&n);
        if (miller(n)||n==1){
            printf("no\n");
        }
        else {
            memset(factor,0,sizeof(factor));
            total=0;
            find(n);
            sort(factor+1,factor+total+1);
            bool flag=1;
            for(long long i=1;i<=total;i++){
                ///本题可以这样过
                ///if(n%(factor[i]*factor[i])==0){
                ///    flag=0;
                ///    cout<<"yes"<<endl;
                ///    break;
               /// }
                mmp[factor[i]]++;///更一般的统计质因子的幂
                if(mmp[factor[i]]>=2){
                    flag=0;
                    cout<<"yes"<<endl;
                    break;
                }
            }
            if(flag==1){
                cout<<"no"<<endl;
            }
            ///for(int i=1;i<=total;i++) mmp[factor[i]]++;//质因子的幂
            ///for(int i=1;i<=total;i++) ans*=mmp[factor[i]]+1;//因子个数
            ///printf("%lld\n",factor[1]);///质因子
        }
    }
}

Update:  

此板子在做gym的一道快速质因子分解的时候T掉了。于是在另外一篇放了倍增优化版本的板子。

https://blog.csdn.net/zstuyyyyccccbbbb/article/details/109501603


思路2: https://www.it610.com/article/1293917594214014976.htm

根据素数密度。

代码来自团长:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<deque>
#include<map>
#include<stdlib.h>
#include<set>
#include<iomanip>
#include<stack>
#define ll long long
#define ms(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x & -x
#define fi first
#define ull unsigned long long
#define se second
#define lson (rt<<1)
#define rson (rt<<1|1)
#define endl "\n"
#define bug cout<<"----acac----"<<endl
#define IOS ios::sync_with_stdio(false), cin.tie(0),cout.tie(0)
using namespace std;
const int maxn = 1e5 + 10;
const int maxm = 1e4 + 50;
const double eps = 1e11 - 7;
const int inf = 0x3f3f3f3f;
const ll  lnf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e6 + 7;
const  double pi = 3.141592653589;
int main()
{
/*
    ll t;
    scanf("%lld",&t);
    while(t--){
    ll n,x,ti;
    scanf("%lld%lld%lld",&n,&x,&ti);

    ll ok=x-(2*n-1)*ti;
    if(x<=2*n*ti){
        printf("%lld\n",4*n*ti);
        continue;
    }
   else{

   if(ok<=ti){
    printf("%lld\n",4*n*ti+ti);
    //
    continue;
    }
   else{
   printf("%lld\n",4*n*ti+ x-2*n*ti);
   continue;
   }


   }

  printf("%lld\n",min(2*n*ti+max(2*n*ti,x),2*n*ti+max(2*n*ti+t,ti+x)));
        }
*/

ll t;
scanf("%lld",&t);
while(t--){
 ll n;
 scanf("%lld",&n);
 ll fa=0;
 for(ll i=2;i<=2000000;i++){
 ll ok=i*i;
  if(ok>n){
    break;
  }
 if(n%ok==0){
  fa=1;
  break;
 }
 if(n%i==0){
  n=n/i;
 }



 }
 if(fa){
    printf("yes\n");
    continue;
 }
 if(n>1){
    ll rnm=ceil(sqrt(n));
    ll cnm=floor(sqrt(n));
    if(rnm*rnm==n&&rnm*rnm==n){

    printf("yes\n");
    continue;
  }

 }
 printf("no\n");

}

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值