hdu 2588 GCD+欧拉函数

点击打开链接

求多少个x满足gcd(x,n)>=m (1<=x<=n)

对gcd(x,n)=d>=m的d进行分类计数 (d不同,x也不同)

如果gcd(x,n)=d 则 gcd(x/d,n/d)=1  

n=dp x=dq ->则有多少个小于p&&与p互质的数q  就能推出多少个x  x=d×q 

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=5e4+20;
typedef long long ll;
int phi[N+20];

int Phi(int n)
{
	int m=sqrt(n+0.5);
	int ans=n;
	for(int i=2;i<=m;i++)
	{
		if(n%i==0)
		{
			ans=ans/i*(i-1);
			while(n%i==0)
			n/=i;
		}
	}
	if(n>1)
	ans=ans/n*(n-1);
	return ans;
}
int main()
{
	ll n,m;
	int t;
	cin>>t;
	while(t--)
	{
		ll ans=0;
		cin>>n>>m;
		//求多少个x满足gcd(x,n)>=m (1<=x<=n) 
		for(int d=1;d*d<=n;d++)// 枚举gcd(x,n)>=m 的d,n太大在sqrt(n)区间内找即可 
		{
			if(n%d==0)//如果gcd(x,n)=d 则 gcd(x/n,n/d)=1  n=dp x=dq ->则有多少个小于p&&与p互质的数 就能推出多少个x 
			{
				if(d>=m)
				ans+=Phi(n/d);		
				if(n/d>=m&&d*d!=n)//d在右边 //d*d=n在上面计算过一次 
				ans+=Phi(d);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值