题意
给你一个范围是 [ 1 , 10 ^ 18 ] 的数,让你求这个数能不能被一个 素数的平方 整除(即 c % ( i * i ) == 0)
思路1
用大数分解模板,将 c 分解得到 c 的所有质因子,然后去重(模板得到的是无序且没去重的),然后将所有质因子相乘,得到 tmp , 如果 tmp 跟 c 相等,说明 c 不能被素数的平方整除,否则可以。
(记得特判 c == 1 的情况!!! c = 1 是 “ no ” )
代码1
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
#include<queue>
#include<ctime>
#include<cstdlib>
using namespace std;
typedef long long ll;
const int S=8;//随机算法判定次数一般8~10就够了
//计算 ret=(a*b)%c a,b,c<2^63
ll mult_mod(ll a,ll b,ll c)
{
a%=c;
b%=c;
ll ret=0;
ll tmp=a;
while(b)
{
if(b&1)
{
ret+=tmp;
if(ret>c) ret-=c;//直接取模慢很多
}
tmp<<=1;
if(tmp>c) tmp-=c;
b>>=1;
}
return ret;
}
//计算ret=(a^n)%mod
ll pow_mod(ll a,ll n,ll mod)
{
ll ret=1;
ll temp=a%mod;
while(n)
{
if(n&1) ret=mult_mod(ret,temp,mod);
temp=mult_mod(temp,temp,mod);
n>>=1;
}
return ret;
}
//通过a^n(n-1)=1(mod n)来判断 n 是不是素数
//n-1=x * 2^t 中间使用二次判断
//是合数返回true ,不一定是合数返回false
bool check(ll a,ll n,ll x,ll t)
{
ll ret=pow_mod(a,x,n);
ll last=ret;
for(int i=1;i<=t;i++)
{
ret=mult_mod(ret,ret,n);
if(ret==1&&last!=1&&last!=n-1) return true;
last=ret;
}
if(ret!=1) return true;
else return false;
}
// Miller_Rabin算法进行素数测试
//速度快可以判断一个 < 2^63 的数是不是素数
//是素数返回true,(可能是伪素数)
//不是素数返回false
bool Miller_Rabin(ll n)
{
if(n<2) return false;
if(n==2) return true;
if((n&1)==0) return false;//偶数
ll x=n-1;
ll t=0;
while((x&1)==0)
{
x>>=1;
t++;
}
srand(time(NULL));
for(int i=0;i<S;i++)
{
ll a=rand()%(n-1)+1;
if(check(a,n,x,t)) return false;
}
return true;
}
// pollard_rho算法进行质因素分解
set<ll>factor;//质因数分解结果 (刚返回时是无序的)
//int tol;//质因数的个数 编号0~tol-1;
ll gcd(ll a,ll b)
{
ll t;
while(b)
{
t=a;
a=b;
b=t%b;
}
if(a>=0) return a;
else return -a;
}
//找出一个因子
ll pollard_rho(ll x,ll c)
{
ll j=1,k=2;
srand(time(NULL));
ll x0=rand()%(x-1)+1;
ll y=x0;
while(1)
{
j++;
x0=(mult_mod(x0,x0,x)+c)%x;
ll d=gcd(y-x0,x);
if(d!=1&&d!=x) return d;
if(y==x0) return x;
if(j==k)
{
y=x0;
k+=k;
}
}
}
//对n进行素因子分解,存入vector,k设置为107左右即可
void findfac(ll n,int k)
{
if(n==1) return ;
if(Miller_Rabin(n))
{
factor.insert(n);
return ;
}
ll p=n;
int c=k;
while(p>=n) p=pollard_rho(p,c--);
findfac(p,k);
findfac(n/p,k);
}
int main( )
{
int t;
scanf("%d",&t);
while(t--)
{
factor.clear();
ll c,tmp=1;
scanf("%lld",&c);
if(c==1)
{
printf("no\n");
continue;
}
findfac(c,107);
for(set<ll>::iterator it=factor.begin();it!=factor.end();it++)
{
tmp*=(*it);
}
if(tmp!=c) printf("yes\n");
else printf("no\n");
}
return 0;
}
思路2
这是看了网上一位大佬的题解(暴力过的,真是tql),觉得很好,就借鉴写了一下(实则思路白嫖)
枚举 1e6 以内的素数,如果能被 素数平方 整除直接yes;
如果不能,就看能不能被 素数 整除,如果可以,c就除去这个质因子;
最后枚举结束,如果还没判断出yes , 那就看留下的 c;
如果此时 c 是 1 直接 no ;
如果 >1 , 说明 c 中只剩下 > 1e6 的质因子了,同时数量不会超过 2 (因为 e 最大到 1e18 ,而我们把 <= 1e6 的质因子都筛完了,剩下 > 1e6 的质因子如果超过三个就超出 1e18 了),所以我们对此时的 c 开方
如果 开方的结果平方 == c ,说明 c 是由两个相同的质因子相乘得到的,输出yes,否则no;
代码2
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e7+10;
ll len,prime[maxn];
bool vis[maxn];
void su()
{
vis[1]=1;
for(int i=2;i<=1000000;i++)
{
if(!vis[i])
{
prime[len++]=i;
for(int j=i+i;j<=1000000;j+=i)
vis[j]=1;
}
}
}
int main()
{
su();
int t;
scanf("%d",&t);
while(t--)
{
ll c,flag=0;
scanf("%lld",&c);
for(int i=0;i<len;i++)
{
if(c%(prime[i]*prime[i])==0)
{
flag=1;
break;
}
else if(c%prime[i]==0)
{
c/=prime[i];
}
}
if(flag) printf("yes\n");
else if(c>1)
{
ll tmp=sqrt(c);
if(tmp*tmp==c) printf("yes\n");
else printf("no\n");
}
else printf("no\n");
}
return 0;
}