欧拉函数专项

来源:互联网 发布:油性皮肤洗面奶知乎 编辑:程序博客网 时间:2024/06/01 10:26

这里写图片描述
这里写图片描述

rho详解:http://blog.csdn.net/u012061345/article/details/48316811
欧拉函数应用与性质及求法:http://blog.csdn.net/sentimental_dog/article/details/52002608

首先,先知道一个性质φ(n)=n(1-1/a1)(1-1/a2)……(1-1/ak);
a1,a2……,ak为n分解的质因数(去重)。
然后这道题,n<=10^18,需要用到rho(大整数分解)+Miller-Rabin。

#include<iostream>#include<cmath>#include<cstring>#include<cstdio>#include<algorithm>#include<queue>#include<cstdlib>#define LL long longusing namespace std;LL N,cnt;LL prime[]={0,2,3,5,7,11,13,17,19,23};LL a[100009];LL gcd(LL a,LL b){return (!b)?a:gcd(b,a%b);} LL q_mul(LL a,LL b,LL mod)//快速加 {    LL s=0;    while(b)    {        if(b&1) s=(s+a)%mod;        a=(a+a)%mod;        b>>=1;    }    return s;} LL q_pow(LL a,LL b,LL mod)//快速幂 {    LL s=1;    while(b)    {        if(b&1) s=q_mul(s,a,mod);        a=q_mul(a,a,mod);        b>>=1;    }    return s;}bool MR(LL n){    if (n==2) return 1;    if (n%2==0) return 0;    int lg=0;LL w=n-1;    while (w%2==0) lg++,w/=2;    for (int i=1;i<=9;i++)    {        if (n==prime[i]) return 1;        LL x=q_pow(prime[i],w,n);        if (x==1||x==n-1) continue;        for (int j=1;j<=lg;j++)        {            LL y=q_mul(x,x,n);            if (x!=1&&x!=n-1&&y==1) return 0;            x=y;        }        if (x!=1) return 0;    }    return 1;}LL rho(LL n){    LL c=rand()%(n-1)+1,x1=rand()%n,x2=x1,k=2,p=1;    for(int i=1;p==1;i++)    {        x1=(q_mul(x1,x1,n)+c)%n;        if (x1==x2) return 1;        p=gcd(n,abs(x1-x2));        if (i==k) k<<=1,x2=x1;    }    return p;}void divi(LL n){    if(n==1) return;    if(MR(n))    {        a[++cnt]=n;return;    }    LL p=1;    while(p==1) p=rho(n);    divi(p);divi(n/p);}int main(){    freopen("phi.in","r",stdin);    freopen("phi.out","w",stdout);    scanf("%lld",&N);    divi(N);    sort(a+1,a+cnt+1);    cnt=unique(a+1,a+cnt+1)-a-1;    for(int i=1;i<=cnt;i++) N=N/a[i]*(a[i]-1);    printf("%lld",N);    return 0;}

这里写图片描述

bzoj
不知道为什么bzoj是PE

这里写图片描述

那么由上述式子可以逆推得出,
N=φ(N)(p1/(p1-1))(p2/(p2-1))…(pk/(pk-1))
那我们用dfs,来递归实现。

#include<iostream>#include<cmath>#include<cstring>#include<cstdio>#include<algorithm>#include<queue>#include<cstdlib>#define LL long long#define M 10000009using namespace std;LL N,ans[M],prime[M];int k,cnt,cnt2;bool np[M];void prepare(){    np[0]=1,np[1]=1;    for(int i=2;i<M;i++)    {        if(!np[i])         {            prime[++cnt]=i;        }        for(int j=1;j<=cnt&&1ll*i*prime[j]<M;j++)        {            np[i*prime[j]]=1;            if(i%prime[j]==0) break;        }    }}LL q_mul(LL a,LL b,LL mod)//快速加 {    LL s=0;    while(b)    {        if(b&1) s=(s+a)%mod;        a=(a+a)%mod;        b>>=1;    }    return s;} LL q_pow(LL a,LL b,LL mod)//快速幂 {    LL s=1;    while(b)    {        if(b&1) s=q_mul(s,a,mod);        a=q_mul(a,a,mod);        b>>=1;    }    return s;}bool MR(LL n){    if (n==2) return 1;    if (n%2==0) return 0;    int lg=0;LL w=n-1;    while (w%2==0) lg++,w/=2;    for (int i=1;i<=9;i++)    {        if (n==prime[i]) return 1;        LL x=q_pow(prime[i],w,n);        if (x==1||x==n-1) continue;        for (int j=1;j<=lg;j++)        {            LL y=q_mul(x,x,n);            if (x!=1&&x!=n-1&&y==1) return 0;            x=y;        }        if (x!=1) return 0;    }    return 1;}bool pd_prime(LL x){    if(x<M) return (!np[x]);    else return MR(x);}void dfs(int dep,LL x,LL res){    if(x+1>prime[cnt]&&pd_prime(x+1)) {ans[++cnt2]=res*(x+1);}    LL tres=res,tx=x,s;    for(int i=dep;i>=1;i--)    {        if(x%(prime[i]-1)==0)        {            s=1;            tres=res;            tx=x/(prime[i]-1);            while(tx%s==0)            {                tres*=prime[i];                dfs(i-1,tx/s,tres);                s*=prime[i];            }        }    }     if(x==1){ans[++cnt2]=res;return;} }int main(){    freopen("arc.in","r",stdin);    freopen("arc.out","w",stdout);    prepare();//cout<<MR(17);    scanf("%lld%d",&N,&k);    dfs(cnt,N,1);    sort(ans+1,ans+cnt2+1);    printf("%lld ",ans[1]);k--;    for(int i=2;k&&i<=cnt2;i++)    {        if(ans[i]!=ans[i-1]) printf("%lld ",ans[i]),k--;    }    return 0;}

这里写图片描述
60分线性筛暴力

#include<iostream>#include<cmath>#include<cstring>#include<cstdio>#include<algorithm>#include<queue>#include<cstdlib>#define LL long long#define M 10000009using namespace std;int prime[M],cnt,phi[M],n;LL ans;bool np[M];void work(){    np[0]=1,np[1]=1;    phi[1]=1;    for(int i=2;i<M;i++)    {        if(!np[i])        {            prime[++cnt]=i;            phi[i]=i-1;        }        for(int j=1;j<=cnt&&i*prime[j]<M;j++)        {            np[i*prime[j]]=1;            if(i%prime[j]==0)            {                phi[i*prime[j]]=phi[i]*prime[j];                break;            }            else phi[i*prime[j]]=phi[i]*(prime[j]-1);        }    }}int main(){    freopen("sum.in","r",stdin);    freopen("sum.out","w",stdout);    work();    scanf("%d",&n);    for(int i=1;i<=n;i++)     ans+=phi[i];    printf("%lld",ans);    return 0;} 
原创粉丝点击