快速乘+快速幂


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#define ll long long
using namespace std;
ll prime[99999],cnt;
ll gcd(ll a,ll b)
{
    if(!b) return a;
    return gcd(b,a%b);
}
ll mul(ll x,ll y,ll mod)//¿ìËÙ¼Ó
{
    ll ans=0;
    while(y)
    {
        if(y%(1ll*2)) ans=(ans+x%mod)%mod;
        y/=(1ll*2);
        x=(x%mod+x%mod)%mod;
    } 
    return ans;
}
ll fast_pow(ll x,ll y,ll m)//¿ìËÙÃÝ
{  
    ll ans=1;  
    x%=m;  
    while(y)  
    {  
        if(y%2)  
          ans=mul(ans, x, m);  
        y/=2;  
        x=mul(x, x, m);  
    }  
    return ans;  
}  
bool MR(ll n)  
{  
    if(n == 2) return 1;  
    if(n < 2 || !(n & 1)) return 0;  
    ll m=n-1;  
    ll k=0;  
    while((k&1)==0)  
    {  
        k++;  
        m/=2;  
    }  
    for(int i=0; i<12; i++)  
    {  
        ll a=rand()%(n - 1)+1;  
        ll x=fast_pow(a, m, n);  
        ll y=0;  
        for(int j=0; j<k; j++)  
        {  
            y=mul(x,x,n);  
            if(y==1&&x!=1&&x!=n-1) return 0;  
            x = y;  
        }  
        if(y!=1) return 0;  
    }  
    return 1;  
}  
ll rho(ll n,ll c)//ÕÒÒò×Ó
{
    ll i=1,k=2;
    ll x=rand()%(n-1)+1;
    ll y=x;
    while(1)
    {
        i++;
        x=(mul(x,x,n)+c)%n;
        ll d=gcd(((y-x)+n)%n,n)%n;
        if(d>1&&d<n) return d;
        if(y==x) return n;
        if(i==k)//Ò»¸öÓÅ»¯£¬ÎÒTMÒ²²»ÖªµÀΪɶ
        {
            y=x;
            k<<=1;
        }
    }
} 
void find(ll n,ll c)//·Ö½âµÄµÝ¹é¹ý³Ì
{
    if(n==1) return;
    if(MR(n))
    {
        prime[++cnt]=n;
        return;
    }
    ll p=n;
    ll k=c;
    while(p>=n) p=rho(n,c--);
    find(n/p,c);
    find(p,c);
}
int main()
{
    ll n;
    scanf("%lld",&n);
    find(n,120);
    sort(prime+1,prime+cnt+1);
    ll t=unique(prime+1,prime+cnt+1)-prime-1;//È¥Öغ¯Êý£¬²»ÒªÍüÁËÈ¥ÁËÖظ´µÄÖÊÊý°¡

    /* for(int i=1;i<=t;i++)
     printf("%lld ",prime[i]);*/
     double ans=n;
     for(int i=1;i<=t;i++)
      ans*=(1.0-(1.0/(double)prime[i]));
     printf("%lld",(ll) ans);
}

猜你喜欢

转载自blog.csdn.net/qq_41286356/article/details/88370364
今日推荐