hdu-2588-GCD(欧拉函数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2588

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

题目大意:给你t组数据,每组数据有一个n,一个m。找出所有gcd(x,n)>=m得x得个数,输出

对于每个gcd(x,n)=i;

同时除以i 得:gcd(x/i,n/i)=1;

转化成求n/i的欧拉函数值(求小于一个数的质因数的个数),注意去重和特判

ac:

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 1000000007
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);

ll euler(ll n)
{
	ll res=n;
	for(ll i=2;i*i<=n;++i)
	{
		if(n%i==0)
		{
			res=res/i*(i-1);
			while(n%i==0)
				n=n/i;
		}
	}
	if(n>1)
		res=res/n*(n-1);
	return res;
}

int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		ll n,m;
		cin>>n>>m;
		ll sum=0;
		for(ll i=1;i*i<=n;++i)
		{
			if(n%i==0)
			{
                //两种情况的要求
				if(i>=m)//符合要求
					sum=sum+euler(n/i);
				if(i*i<n&&n/i>=m)//符合要求
					sum=sum+euler(i);
			}
		}
		cout<<sum<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/81502470