您的位置:首页 > 其它

hdu 2588 GCD(积性函数

2017-08-05 09:58 295 查看
传送门


GCD

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2332    Accepted Submission(s): 1185


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,For example,(1,2)=1,(12,18)=6.

(a,b) can be easily found by the Euclidean algorithm. Now Carp is considering a little 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 test cases. 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

 

Source

ECJTU 2009 Spring Contest

 

Recommend

lcy   |   We have carefully selected several similar problems for you:  1695 1787 3501 2824 2582 

 

给定N,M求gcd(i,N)>=M的i的个数

我们可以分解N=a*b, i=a*d(b>=d 且b,d互质),那么我们要求的就是a》=m的时候d的个数(b随a而确定)

由于b>=d且b,d互质,所以这个数目就是φ(b)-1  

但是,如果对于每个a枚举b,铁定超时。(仍然O((N-M)*sqrt(N))的复杂度)

但是如果单纯这样全部枚举的话依旧会超时,所以我们要想一个办法去优化它。

我们可以折半枚举,这里的折半并不是二分的意思。

我们先看,我们枚举时,当i<sqrt(n),假设a=n / i, 当i>sqrt(n)之后 有b=n/i,我们观察到当n%i==0时,会出现一种情况,就是a*b==n。所以我们就可以只需要枚举sqrt(n)种情况,然后和它对应的情况就是 n/i。

我们这种枚举时间会快非常多

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i)
4000
for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1000;
const int maxx=1e7+100;
const double EPS=1e-7;
const int mod=998244353;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
inline LL Scan()
{
LL Res=0,ch,Flag=0;
if((ch=getchar())=='-')Flag=1;
else if(ch>='0' && ch<='9')Res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')Res=Res*10+ch-'0';
return Flag ? -Res : Res;
}

//freopen( "in.txt" , "r" , stdin );
//freopen( "data.out" , "w" , stdout );
//cerr << "run time is " << clock() << endl;
int solve(int n)
{
int ans=n;
if(n%2==0)//只搜n/2次,不然可能超一组 (也可能不超)
{
while(n%2==0) n/=2;
ans=ans/2;
}
for(long long i=3;i*i<=n;i+=2)
//每找到一个就更改上界,这个优化很多,所以直接copy刘汝佳的代码会超时
{
if(n%i==0)
{
while(n%i==0) n/=i;
ans=ans/i*(i-1);
}
}
if(n>1) ans=ans/n*(n-1);
return ans;
}

int main()
{
int t;
t=Scan();
while(t--)
{
int n,m;
n=Scan();m=Scan();
int ans=0;
for(int i=1;i*i<=n;i++)
{
if(n%i==0)
{
if(i>=m) ans+=solve(n/i);
if(n/i>=m&&i*i!=n) ans+=solve(i);
}
}
cout<<ans<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: