zoukankan      html  css  js  c++  java
  • 【BZOJ4802】欧拉函数(Pollard_rho)

    【BZOJ4802】欧拉函数(Pollard_rho)

    题面

    BZOJ

    题解

    这么大的范围肯定不好杜教筛。
    考虑欧拉函数的计算式,显然只需要把(n)分解就好了。
    直接(Pollard\_rho)

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<set>
    #include<map>
    #include<vector>
    #include<queue>
    using namespace std;
    #define ll long long
    ll N;
    ll Multi(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 fpow(ll a,ll b,ll MOD)
    {
    	ll s=1;
    	while(b){if(b&1)s=Multi(s,a,MOD);a=Multi(a,a,MOD);b>>=1;}
    	return s;
    }
    bool Miller_Rabin(ll n)
    {
    	if(n==2)return true;
    	for(int tim=10;tim;--tim)
    	{
    		ll a=rand()%(n-2)+2,p=n-1;
    		if(fpow(a,n-1,n)!=1)return false;
    		while(!(p&1))
    		{
    			p>>=1;ll nw=fpow(a,p,n);
    			if(Multi(nw,nw,n)==1&&nw!=1&&nw!=n-1)return false;
    		}
    	}
    	return true;
    }
    vector<ll> fac;
    ll Pollard_rho(ll n,int c)
    {
    	ll i=0,k=2,x=rand()%(n-1)+1,y=x;
    	while(233)
    	{
    		++i;x=(Multi(x,x,n)+c)%n;
    		ll d=__gcd((y-x+n)%n,n);
    		if(d!=1&&d!=n)return d;
    		if(x==y)return n;
    		if(i==k)y=x,k<<=1;
    	}
    }
    void Fact(ll n,int c)
    {
    	if(n==1)return;
    	if(Miller_Rabin(n)){fac.push_back(n);return;}
    	ll p=n;while(p>=n)p=Pollard_rho(p,c--);
    	Fact(p,c);Fact(n/p,c);
    }
    int main()
    {
    	cin>>N;Fact(N,233);
    	sort(fac.begin(),fac.end());
    	int l=unique(fac.begin(),fac.end())-fac.begin();
    	for(int i=0;i<l;++i)N=N*(1.0*(fac[i]-1)/fac[i]);
    	cout<<N<<endl;
    	return 0;
    }
    
    
  • 相关阅读:
    activemq 异步和同步接收
    简单的activemq的封装和使用
    activemq 一个不错的ppt
    activemq 的小实验
    activemq api的封装
    观察者设计模式例子
    【转载】自定义类加载器例子
    京东拍拍网 笔试 搞java的去考C++ 苦逼
    java中用equals比较两个内容相同的字符数组
    云端飘 String s=new String("abc")创建了几个对象?
  • 原文地址:https://www.cnblogs.com/cjyyb/p/9251431.html
Copyright © 2011-2022 走看看