hdu2588-GCD-(欧拉函数+分解因子)
Posted shoulinniao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu2588-GCD-(欧拉函数+分解因子)相关的知识,希望对你有一定的参考价值。
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.
(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.
InputThe 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.OutputFor 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<=x<=n,求x符合gcd(x,n)>=m有多少个。
解题过程:
令d=gcd(x,n),显然d是n的因子,并且是x和n的最大公因子,则gcd(x/d,n/d)=1
对于每个d,令y=n/d,找有多少个x/d满足gcd(x/d,y)=1。
欧拉函数登场,累加y的欧拉函数值。
1 #include <iostream> 2 #include<stdio.h> 3 #include <algorithm> 4 #include<string.h> 5 #include<cstring> 6 #include<math.h> 7 #define inf 0x3f3f3f3f 8 #define ll long long 9 using namespace std; 10 11 ll euler(ll x) 12 { 13 ll res=x; 14 for(ll i=2;i*i<=x;i++) 15 { 16 if(x%i==0) 17 { 18 res=res/i*(i-1); 19 while(x%i==0) 20 x=x/i; 21 } 22 } 23 if(x>1) 24 res=res/x*(x-1); 25 return res; 26 } 27 28 int main() 29 { 30 31 ll t,n,m,sum; 32 scanf("%lld",&t); 33 while(t--) 34 { 35 sum=0; 36 scanf("%lld%lld",&n,&m); 37 int q=sqrt(n); 38 ll i; 39 for(i=1;i*i<=n;i++) 40 { 41 if(n%i==0) 42 { 43 if(i>=m) 44 sum=sum+euler(n/i); 45 if((n/i)>=m) 46 sum=sum+euler(i); 47 } 48 } 49 i--; 50 if(i*i==n && i>=m) 51 sum=sum-euler(i); 52 printf("%lld ",sum); 53 } 54 return 0; 55 }
以上是关于hdu2588-GCD-(欧拉函数+分解因子)的主要内容,如果未能解决你的问题,请参考以下文章