跳到正文

剪贴板

剪贴板 0sp50rwh

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<bits/stdc++.h>
#include<cmath>
#define ll long long
#define ull unsigned long long
#define N 100010
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Rof(i,a,b) for(int i=a;i>=b;i--)
#define ls x<<1
#define rs x<<1|1
#define lson ls,l,mid
#define rson rs,mid+1,r
#define pb push_back
#define mk make_pair
#define pque priority_queue
#define pii pair<int,int>

using namespace std;
ll prime[13]={0,2,3,5,7,11,13,17,19,23,29,31,37};
ll T,n;

ll read(){
	ll x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
ll p;
ll ksm(ll x,ll y){
	ll ans=1;
	while(y){
		if(y&1) ans=(__int128)ans*x%p;
		x=(__int128)x*x%p;
		y>>=1;
	}
	return ans;
}
bool chk(ll x,ll u,ll v){
	if(p%x==0) return 1;
	ll now=ksm(x,u);
	if(now==1) return 1;
	For(i,1,v){
		if(now==p-1) return 1;
		now=(__int128)now*now%p;
	}
	return 0;
}
bool isprime(ll now){
	if(now==1) return 0;
	if(now==2) return 1;
	if(now==3) return 1;
	if(now%2==0) return 0;
	p=now;
	ll u=now-1,v=0;
	while(u%2==0) u>>=1,v++;
	For(i,1,12) if(!chk(prime[i],u,v)) return 0;
	return 1;
}
ll f(ll x,ll y,ll z){
	return ((__int128)x*x+y)%z;
}
ll mx=0;
ll PR(ll x){
	ll s=0,t=0,c=rand()%(x-1)+1;
	ll val=1;
	for(int i=1;;i<<=1,s=t,val=1){
		For(j,1,i){
			t=f(t,c,x);
			val=(__int128)val*abs(t-s)%x;
			if(j%127==0){
				ll d=__gcd(val,x);
				if(d>1) return d;
			}
		}
		ll d=__gcd(val,x);
		if(d>1) return d;
	}
}
void fac(ll x){
	if(x<=mx || x<2) return;
	if(isprime(x)){
		mx=max(mx,x);
		return;
	}
	ll pp=x;
	while(pp>=x) pp=PR(pp);
	while(x%pp==0) x/=pp;
	fac(x),fac(pp);
}

int main()
{
	//freopen("run.in","r",stdin);
	//freopen("run.out","w",stdout);
	srand(time(NULL));
	T=read();
	while(T--){
		n=read();
		mx=0;
		fac(n);
		if(mx==n) printf("Prime\n");
		else printf("%lld\n",mx);
	}
	return 0;
}