【bzoj4802】欧拉函数

题目链接

想瞎搞过去的可能就只有我一个。。。。。

正解Pollard_rho算法(啥rho算法?)

Pollard_rho算法是专门解决这类大数质因数分解的,当然还要搭配Miller_Rabin判素数(推荐一篇入门博文

先质因数分解,然后按 φ(i) φ ( i ) 的式子算就行了

代码:

#include<cstdio>
#include<vector>
#include<queue>
#include<ctime>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<cstring>
#include<cmath>
using namespace std;

typedef long long LL;

const int INF = 2147483647;
const int maxn = 10000000;

LL ans;
LL n,pri[maxn],tot,tme = 0;

inline LL getint()
{
    LL ret = 0,f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') ret = ret * 10 + c - '0',c = getchar();
    return ret * f;
}

inline LL mul(LL x,LL y,LL mod)
{
    LL tmp=(x*y-(LL)((long double)x/mod*y+1.0e-8)*mod);
    return tmp<0 ? tmp+mod : tmp;
}

inline LL qpow(LL a,LL b,LL n)
{
    LL ret = 1;
    while (b)
    {
        if (b & 1) ret = mul(ret,a,n);
        a = mul(a,a,n);
        b >>= 1;
    }
    return ret;
}

inline int miller(LL n)
{
    int test;
    if (n == 3)
        test = 1;
    if (n == 2) return 1;
    if (n < 2 || n % 2 == 0) return 0;
    LL m = n - 1,k = 0;
    while (m % 2 == 0) m >>= 1 , k++;
    for (int I = 1; I <= 5; I++)
    {
        LL a = rand() % (n - 1) + 1,b;
        a = qpow(a,m,n);
        for (int i = 1; i <= k; i++)
        {
            b = mul(a,a,n);
            if (b == 1 && a != n - 1 && a != 1) return 0;
            a = b;
        }
        if (a != 1) return 0;
    }
    return 1;
}

inline LL gcd(LL a,LL b)
{
    return !b ? a : gcd(b,a % b);
}

inline LL rho(LL n)
{
    LL x = rand() % (n - 1) + 1,y = x;
    LL c = tme % (n - 1) + 1,g,k = 2,i = 0;
    tme++;
    while (233)
    {
        x = (mul(x,x,n) + c) % n;
        if (x == y) return n;
        g = gcd(abs(x - y),n);
        if (g != 1) return g;
        if (++i == k)
            y = x , k <<= 1;
    }
}

inline void split(LL n)
{
    if (n == 1) return;
    if (miller(n)) {pri[++tot] = n; return;}
    LL p = n;
    while (p == n) p = rho(p);
    split(n / p);
    split(p);
}

int main()
{
    #ifdef AMC
        freopen("AMC1.txt","r",stdin);
    #endif
    n = getint();
    split(n);
    sort(pri + 1,pri + tot + 1);

    ans = 1;
    for (int i = 1; i <= tot; i++)
    {
        if (pri[i] != pri[i - 1]) ans *= pri[i] - 1;
        else ans *= pri[i];
    }
    printf("%lld\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值