bzoj4802 欧拉函数 素数测试

Description


已知N,求phi(N)

正整数N。N<=10^18

Solution


我调了一个晚上的程序是对的!!!!!!!!!
mdzz为什么加万恶的std就对了??????????????
我tm拍半天拍出的错误是没加std?????!!!

Code


#include <stdio.h>
#include <string.h>
#include <time.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
using namespace std;

typedef long long LL;
typedef long double LD;

int prime[12]={11,2,3,5,7,11,13,17,19,23,29,31};
LL a[205],ans;

LL mul(LL a,LL b,LL MOD) {
    LL tmp=(a*b-(LL)((LD)a/MOD*b+0.1)*MOD)%MOD;
    if (tmp<0) tmp+=MOD;
    return tmp;
}

LL ksm(LL x,LL dep,LL MOD) {
    LL ret=1;
    while (dep) {
        if (dep&1) ret=mul(ret,x,MOD);
        dep>>=1; x=mul(x,x,MOD);
    }
    return ret;
}

bool test(LL n) {
    rep(i,1,prime[0]) if (prime[i]==n) return true;
    if (n<2) return false;
    if (!(n&1)) return false;
    LL m=n-1,k=0;
    for (;!(m&1);m/=2) k++;
    rep(i,1,prime[0]) {
        LL w=ksm(prime[i],m,n);
        if (w==1||w==n-1) continue;
        rep(j,1,k) {
            LL u=mul(w,w,n);
            if (w!=1&&w!=n-1&&u==1) return false;
            w=u;
        }
        if (w!=1) return false;
    }
    return true;
}

LL gcd(LL x,LL y) {
    return !y?x:gcd(y,x%y);
}

LL rho(LL n) {
    LL c=rand()*rand()%(n-1)+1,p=1;
    LL x1=rand()*rand(),x2=x1,k=2;
    for (LL i=1;p==1;i++) {
        x1=(mul(x1,x1,n)+c)%n;
        if (x1==x2) return 1;
        p=gcd(abs(x1-x2),n);
        if (i==k) {
            x2=x1;
            k=k*2;
        }
    }
    return p;
}

void solve(LL n) {
    if (n==1) return ;
    if (test(n)) {
        a[++a[0]]=n;
        return ;
    }
    LL t=n;
    while (t==n) t=rho(n);
    solve(t); solve(n/t);
}

int main(void) {
    srand(19260817);
    LL n; scanf("%lld",&n);
    ans=n; solve(n);
    std:: sort(a+1,a+a[0]+1);
    int size=std:: unique(a+1,a+a[0]+1)-a-1;
    rep(i,1,size) ans=ans/a[i]*(a[i]-1);
    printf("%lld\n", ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值