GCD(欧拉函数)

Posted fighting-sh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GCD(欧拉函数)相关的知识,希望对你有一定的参考价值。

GCD

http://acm.hdu.edu.cn/showproblem.php?pid=2588

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3782    Accepted Submission(s): 2053


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
 
技术图片
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define IT set<ll>::iterator
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 200005
 9 #define eps 1e-6
10 #define PI acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<ll,ll>pll;
15 typedef pair<ll,int> pli;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long MOD=1000000007;
19 const double oula=0.57721566490153286060651209;
20 const int INF=0x3f3f3f3f;
21 using namespace std;
22 
23 
24 ll Euler(ll n){
25     ll ans=n;
26     for(ll i=2;i*i<=n;i++){
27         if(n%i==0) ans=ans/i*(i-1);
28         while(n%i==0) n/=i;
29     }
30     if(n>1) ans=ans/n*(n-1);
31     return ans;
32 }
33 
34 int main(){
35     std::ios::sync_with_stdio(false);
36     int t;
37     cin>>t;
38     while(t--){
39         ll n,m;
40         cin>>n>>m;
41         ll ans=0;
42         for(int i=1;i*i<=n;i++){
43             if(n%i==0){
44                 if(i>=m) ans+=Euler(n/i);
45                 if(i*i!=n&&n/i>=m) ans+=Euler(i);
46             }
47         }
48         cout<<ans<<endl;
49     }
50 }
View Code

 

以上是关于GCD(欧拉函数)的主要内容,如果未能解决你的问题,请参考以下文章

bzoj 2818 gcd 线性欧拉函数

HDOJ 1787 GCD Again(欧拉函数)

[题解](gcd/欧拉函数)luogu_P2568_GCD

HDU1695-GCD(数论-欧拉函数-容斥)

bzoj 2818 Gcd(欧拉函数)

GCD - Extreme (II) UVA - 11426(欧拉函数!!)