HDOJ 2588 GCD 【欧拉函数】 附数据加强版 【Miller_Rabin+Pollard_Rho算法】


GCD

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 2347    Accepted Submission(s): 1194

Problem Description

The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the largest divisor common to a and b,Forexample,(1,2)=1,(12,18)=6.
(a,b) can be easily found by the Euclidean algorithm. Now Carp is considering alittle more difficult problem:
Given integers N and M, how many integer X satisfies 1<=X<=N and(X,N)>=M.

 

 

Input

The first line of input is an integer T(T<=100) representing the number of testcases. The following T lines each contains two numbers N and M (2<=N<=1000000000,1<=M<=N), representing a test case.

 

 

Output

For each test case,output the answer on a single line.

 

 

Sample Input

3

1 1

10 2

10000 72

 

 

Sample Output

1

6

260

 

【题意】


给出n,m,问[1,n]中有多少数x,满足gcd(x,n)>=m.


【思路】


由于数据范围较大,暴力显然不行。


本题重要突破口:


gcd(x,n)的结果一定是n的约数。


由这个结论我们可以想到,由于n的约数个数并不是很多,我们可以考虑去枚举n的约数D,且要求D>=m。


那么确定了某个D后如何计数呢,这时候问题就转化为了求[1,n]中有多少数x满足gcd(x,n)=D。


显然如果还是去一个个枚举x的话时间复杂度并没有得到优化。


但我们可以得到以下两条结论


  1. 由于gcd(x,n)=p,那么x/p与n/p一定互质。
  2. 由于x<=n,那么x/p<=n/p。


由这两个结论我们容易发现,这样的话x的个数不就等于不大于(n/p)且与其互质的(x/p)的个数了吗,即phi(n/p)。这个可以在sqrt(n)的时间复杂度内实现,显然这样做是可行的。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn= 100005;
const ll mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;

ll euler(ll num)
{
    ll res=num;
    for(ll i=2; i*i<=num; i++)
    {
        if(num%i==0)
            res=res/i*(i-1);     //先进行除法防止中间数据的溢出
        while(num%i==0)
            num/=i;             //保证n一定是素数
    }
    if(num>1)
        res=res/num*(num-1);
    return res;
}

int main()
{
    ll n,m;
    rush()
    {
        while(~scanf("%I64d%I64d",&n,&m))
        {
            ll ans=0;
            for(int i=1; i*i<=n; i++)
            {
                if(n%i==0)
                {
                    if(i>=m)
                    {
                        ans+=euler(n/i);
                    }
                    if(n/i>=m&&i*i!=n)
                    {
                        ans+=euler(i);
                    }
                }
            }
            printf("%I64d\n",ans);
        }
    }
    return 0;
}

做完了这道题,来挑战一下下面这题。

GCD(数据加强版)

Time Limit : 3000/1000ms (Java/Other)   MemoryLimit : 65535/32768K (Java/Other)

Total Submission(s) : 4   Accepted Submission(s) :2

Problem Description

gcd(a,b)是非负数a,b的最大公约数。现在给定n,m。请你输出在区间[1,n]中:
gcd(n,i)>=m
i的正整数个数。

Input

一开始读入数据组数T,表示有T个测试点。保证T<=10
每个测试点读入n,m两个数。数据保证 2<=n<=1e14m<=n

Output

输出一个数,表示结果。请使用longlong int

Sample Input

2

1000 1

1000000000000 1

Sample Output

1000

1000000000000

 


【题意】


题目要求跟上面的完全一样,但数据范围变得更大。这样的话sqrt(n)就达到了1e7的数量级,又会超时了QAQ。。。


【思路】


方法还是有的,这里介绍两个神奇的算法


  1. Miller_Rabin算法 在O(logn)的时间复杂度下判断一个数是否为素数。
  2. Pollard_Rho算法 在O(n^(1/4))的时间复杂度下处理出一个数的所有质因子及其个数。


那么这两个算法有什么用呢,我们还需要引入一个欧拉函数关于欧拉函数的知识点

其中pi表示这个数的质因子。


用这种方法去求一个数的欧拉函数值就会变得相当高效,时间复杂度为O(n^(1/4)*c),c为一个最大为几十数量级的常数(所求数的不同质因子个数),这样的复杂度对付n最大为1e14的范围就绰绰有余了(其实1e18都可以处理)

#include <cstdio>
#include <cstdlib>
#include <map>
#include <time.h>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 105;
const ll mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int times=6;

int cnt;
ll fac[maxn];

ll fast_mul(ll a,ll b,ll Mod)
{
    a%=Mod,b%=Mod;
    ll ans=0;
    while(b)
    {
        if(b&1)
            ans=(ans+a)%Mod;
        a=a*2;
        if(a>=Mod) a-=Mod;
        b>>=1;
    }
    return ans;
}

ll fast_mod(ll a,ll b,ll Mod)
{
    ll ans=1;
    a%=Mod;
    while(b)
    {
        if(b&1)
            ans=fast_mul(ans,a,Mod);
        b>>=1;
        a=fast_mul(a,a,Mod);
    }
    return ans;
}

ll gcd(ll a,ll b)
{
    if(a<0) return gcd(-a,b);
    return b?gcd(b,a%b):a;
}

bool check(ll a,ll n,ll x,ll t)
{
    ll ret=fast_mod(a,x,n);
    ll last=ret;
    for(int i=1; i<=t; i++)
    {
        ret=fast_mul(ret,ret,n);
        if(ret==1&&last!=1&&last!=n-1)
            return true;
        last=ret;
    }
    if(ret!=1) return true;
    return false;
}

bool Miller_Rabin(ll n)  //次数越多越准确,一般8~10
{
    if(n<2) return false;
    if(n==2) return true;
    if(n%2==0) return false;
    ll x=n-1,t=0;
    while((x&1)==0)
    {
        x>>=1,t++;
    }
    srand(100);
    for(int i=0; i<times; i++)
    {
        ll a=rand()%(n-1)+1;
        if(check(a,n,x,t))
            return false;
    }
    return true;
}

ll pollard_rho(ll x,ll c)
{
    ll i=1,k=2;
    srand(100);
    ll x0=rand()%(x-1)+1;
    ll y=x0;
    while(1)
    {
        i++;
        x0=(fast_mul(x0,x0,x)+c)%x;
        ll d=gcd(y-x0,x);
        if(d!=1&&d!=x) return d;
        if(y==x0) return x;
        if(i==k)
        {
            y=x0,k+=k;
        }
    }
}

void findfac(ll n,int k) //k设置为107左右
{
    if(n==1) return;
    if(Miller_Rabin(n))
    {
        fac[cnt++]=n;
        return;
    }
    ll p=n;
    int c=k;
    while(p>=n)
    {
        p=pollard_rho(p,c--);
    }
    findfac(p,k);
    findfac(n/p,k);
}

ll solve(ll x)
{
    cnt=0;
    findfac(x,107);
    map<ll,int>mp;
    for(int i=0; i<cnt; i++)
    {
        mp[fac[i]]++;
    }
    map<ll,int>::iterator it=mp.begin();
    ll ans=x;
    for(; it!=mp.end(); it++)
    {
        ans=(ans*(it->first-1))/(it->first);
    }
    return ans;
}

ll n,m;

int main()
{
    rush()
    {
        scanf("%I64d%I64d",&n,&m);
        ll ans=0;
        for(ll i=1; i*i<=n; i++)
        {
            if(n%i==0)
            {
                if(i>=m)
                {
                    ans+=solve(n/i);
                }
                if(i*i!=n&&n/i>=m)
                {
                    ans+=solve(i);
                }
            }
        }
        printf("%I64d\n",ans);
    }
    return 0;
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值