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的话时间复杂度并没有得到优化。
但我们可以得到以下两条结论
- 由于gcd(x,n)=p,那么x/p与n/p一定互质。
- 由于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<=1e14且m<=n。
Output
输出一个数,表示结果。请使用longlong int
Sample Input
2
1000 1
1000000000000 1
Sample Output
1000
1000000000000
【题意】
题目要求跟上面的完全一样,但数据范围变得更大。这样的话sqrt(n)就达到了1e7的数量级,又会超时了QAQ。。。
【思路】
方法还是有的,这里介绍两个神奇的算法
- Miller_Rabin算法 在O(logn)的时间复杂度下判断一个数是否为素数。
- 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;
}