https://www.lydsy.com/JudgeOnline/problem.php?id=4802
题意:一个1e18的数求它的欧拉函数
大数分解
然后按容斥定理来算
虽然我还不太明白PR算法
好神奇
随机数都能做题
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector <ll> a;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
ll mul(ll a, ll b, ll n) {
ll ans = 0;
while(b) {
if(b&1) ans = (ans + a) % n;
a = (a + a) % n;
b >>= 1;
}
return ans;
}
ll quickmod(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=quickmod(a,m,n);
for (int i=1;i<=num;x=y,i++){
y=quickmod(x,2,n);
if ((y==1)&&(x!=1)&&(x!=n-1))
return true;
}
return y!=1;
}
bool MR(int t,ll x){
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){
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(){
ll n;
scanf("%lld",&n);
find(n,100);
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;
}