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;
}