CODE VS 4939 欧拉函数 质因数启发式分解

本文介绍了一种基于欧拉函数的质因数分解算法,利用Miller-Rabin素数测试和Pollard's Rho启发式分解方法,实现了高效的大数质因数分解。代码采用C++实现,展示了从读取输入到输出结果的完整流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

/**
CODE VS 4939 欧拉函数
质因数启发式分解 
链接:http://codevs.cn/problem/4939/
*/

#include<bits/stdc++.h>
#define ll long long
using namespace std;

/**********************************************Head-----Template****************************************/
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
inline ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b);}
inline ll lcm(ll a,ll b){ll gg=gcd(a,b);a/=gg;if(a<=LLONG_MAX/b) return a*b;return LLONG_MAX;}
/********************************Head----Temlate**********************************************/


inline ll mul(ll a, ll b, ll mod){
	return a%=mod, b%=mod, (a*b-(long long)((long double)a*b/mod+1e-3)*mod+mod)%mod;
}

inline ll ksm(ll x, ll y, ll mod){
	ll ans=1;
	for (;y;y>>=1, x=mul(x, x, mod)) if (y&1) ans=mul(ans, x, mod);
	return ans;
}

/**********
Miller_Rabin 素数判定
利用费马小定理,以prime为基,不以随机函数进行测试;
利用a^(p-1)=1(mod p) 利用较大概率默认p为素数,也就是忽略了伪素数的情况
优化:提取p-1中2的倍数,p-1=2^(k)*t,先算出a^t mod p 
再进行不断平方,暴力次数[0,k],直到中间过程存在ans = p-1 则所判数字为素数
*/
bool Miller_Rabin(ll n){
	static const ll prime[]={2, 3, 5, 7, 11, 13, 17, 19, 23};
	if (n<=23){ for (int i=0;i<9;i++) if (n==prime[i]) return true;return false;}
	if (~n&1) return false;
	for (int t=0;t<9;t++){
		ll y=n-1; int bit=0;
		for (;~y&1;y>>=1,bit++);
		ll x=ksm(prime[t],y,n);
		if (x==1) goto ed;//goto 减少标记次数
		for (;bit--;x=mul(x, x, n)) if (x==n-1) goto ed;//判处最后的ans
		return false;
		ed:;
	}
	return true;
}

vector<ll>vec;//存储质因数分解结果(无序)
/*************
Pollard_Rho质因数分解:启发式分解

时间复杂度:O(n^(1/4)),空间复杂度:lg(n)

首先确定一个数x为n的质因数,随机找的话概率为1/p;
如果通过某种倍增的规律进行递推x=(x*x+c)%n,可以得到两个数x1,x2
利用gcd(abs(x1-x2),n)>1,使得x1 - x2 不断向左式进行靠近
由此分解出来的差值为n的质因数的概率显著增加
 
最后返回两种结果:
op 1:因数
op 2 :x1 == x2 也就是说当前的倍增数c不满足当前解,那么就需要换个c进行尝试;


换c进行尝试的过程,必然很随机,因得到的ans是存在重复且无序的

*/
void Pollard_Rho(ll n){
	if (Miller_Rabin(n)) return void (vec.push_back(n));
	for (ll c=1;;c++){
		for (ll i=1,x2=0,x1=c;;x1=mul(x1,x1,n)+c,i++){
			if (x1==x2) break;
			ll d=gcd(n, x1>=x2?x1-x2:x2-x1);
			if (1<d&&d<n) return Pollard_Rho(d),Pollard_Rho(n/d),void();
			if (i==(i&-i)) x2=x1;
		}
	}
}
int main(){
	for (;;){
		ll n;read(n); if(n==0) break;
		Pollard_Rho(n);
		sort(vec.begin(),vec.end()); int num = unique(vec.begin(),vec.end())-vec.begin();
		ll ans=n;
		for(int i=0;i<num;i++) ans=ans/vec[i]*(vec[i]-1);
		printf("%lld\n",ans);
		vec.clear();
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值