Pollard Rho 欧拉函数
找了网上一篇题解打了个PR分解大整数的板子
然后用 φ(x)=x∏(1−1pi) φ ( x ) = x ∏ ( 1 − 1 p i ) 算一算就好了。
代码:
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long LL;
LL n;
vector <LL> a;
LL gcd(LL a,LL b){ return b?gcd(b,a%b):a; }
LL mul(LL a,LL b,LL n){
return(a*b-(LL)(a/(long double)n*b+1e-3)*n+n)%n;
}
LL ksm(LL a,LL b,LL m){
LL ret=1; a%=m;
while (b){
if (b&1) ret=mul(ret,a,m);
a=mul(a,a,m),b>>=1;
}
return ret;
}
bool TD(LL a,LL n){//二次探测
LL m=n-1,x,y; int num=0;
while (!(m&1)) m>>=1,num++;
x=ksm(a,m,n);
for (int i=1;i<=num;x=y,i++){
y=ksm(x,2,n);
if ((y==1)&&(x!=1)&&(x!=n-1))
return true;
}
return y!=1;
}
bool MR(int t,LL x){//MR素数判断
if (x==1) return false;
if (x==2) return true;
if (!(x&1)) return false;
while (t--)
if (TD(rand()%(x-1)+1,x))
return false;
return true;
}
LL PR(LL n,int t){//PR分解大整数
LL i=1,k=2,x=rand()%n,y=x,d;
while (1){
i++,x=(mul(x,x,n)+t)%n,d=gcd(y-x,n);
if (d>1&&d<n) return d;
if (y==x) return n;
if (i==k) y=x,k<<=1;
}
}
void find(LL x,int k){
if (x==1) return;
if (MR(3,x)) return (void)a.push_back(x);
LL t=x;
while (t==x) t=PR(x,k--);
find(t,k),find(x/t,k);
}
int main(){
scanf("%lld",&n);
find(n,2730);//这个2730其实可以变的
sort(a.begin(),a.end());
a.erase(unique(a.begin(),a.end()),a.end());
for (int i=0;i<a.size();i++)
n=(n/a[i])*(a[i]-1);
printf("%lld\n",n);
return 0;
}