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<=, 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
解题思路:∵GCD(X,N)>=M,X∈[1,N],∴GCD(X,N)一定是N的约数。假设我们已经知道N的一个约数为P(P>=M),则问题转换成在[1,N]内有多少个数X,满足GCD(X,N)=P(P假设是一个已知值),接下来就是枚举每个P(P>=M),累加每个P对应X的个数。但是对于每个不小于M的N的约数P去计算满足GCD(X,N)>=M的X的个数的情况可能比较复杂,需要考虑的情况比较多,简单的想法是:在[1,N]内用O(NlogN)的时间复杂度判断一下GCD(X,N)是否不小于M,但是题目中N最大为10^10,这肯定是超时的了。因此进一步推导:∵GCD(X,N)=P,∴GCD(X/P,N/P)=1(很明显X/P与N/P互质),又∵X<=N,∴X/P<=N/P,而问题是求X的个数,结合欧拉函数的定义可知即求不大于N/P且与其互质的数X/P的个数,即求ϕ(N/P)。对于N的每个约数P,我们只需从1枚举到根号N,因为N/P可得N的另一个约数(相当于枚举了N的所有约数),这样时间复杂度就大大降低了。
AC代码:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <set>
using namespace std;
typedef long long LL;
const int maxn = 1e6+;
const LL mod = ;
int T; LL n, m, ans;
LL get_Euler(LL x){
LL res = x; ///初始值
for(LL i = 2LL; i * i <= x; ++i) {
if(x % i == ) {
res = res / i * (i - ); ///先除后乘,避免数据过大
while(x % i == ) x /= i;
}
}
if(x > 1LL) res = res / x * (x - ); ///若x大于1,则剩下的x必为素因子
return res;
} int main(){
while(cin >> T) {
while(T--) {
cin >> n >> m; ans = 0LL;
for(LL i = 1LL; i * i <= n; ++i) {
if(n % i) continue; ///跳过不是n的约数
if(i >= m && i * i != n) ans += get_Euler(n / i); ///约数i不小于m,累加phi[n/i],如果i*i==n,只算一次即可
if(n / i >= m) ans += get_Euler(i); ///另一个约数n/i不小于m,累加phi[n/(n/i)]=phi[i]
}
cout << ans << endl;
}
}
return ;
}
												

题解报告:hdu 2588 GCD(欧拉函数)的更多相关文章

  1. HDU 2588 GCD (欧拉函数)

    GCD Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status De ...

  2. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  3. HDU 1695 GCD 欧拉函数+容斥定理

    输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1 由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和 ...

  4. HDU 1695 GCD (欧拉函数,容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  5. hdu 1695 GCD (欧拉函数+容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. hdu 1695 GCD 欧拉函数 + 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K]  和 [L ...

  7. HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...

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

    求gcd(x,y)=p等价于求gcd(x/p,y/p)=1,转化为了n/p内互质的个数 所以欧拉函数,因为有序所以乘2,再特判一下只有在1,1情况下才会重复计算,所以每次都减一 数组开小一时爽,提交w ...

  9. hdu2588 gcd 欧拉函数

    GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  10. Problem I. Count - HDU - 6434(欧拉函数)

    题意 给一个\(n\),计算 \[\sum_{i=1}^{n}\sum_{j=1}^{i-1}[gcd(i + j, i - j) = 1]\] 题解 令\(a = i - j\) 要求 \[\sum ...

随机推荐

  1. AOJ -0033 Ball(DFS)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22516 一道需要思考的搜索题. 题意:十个球按给定顺序从图中所示容器中下落, ...

  2. Ubuntu 16.04安装ntopng流量监控软件

    ntop官方在2012年就已经不再更新,取代的是ntopng.ntopng具有Web页面,适合做网络管理监控软件.比如监控局域网内多台机器的上网情况等. 不过这个东西感觉不太准,最好的方案应该把安装n ...

  3. Servlet处理日期

    以下内容引用自http://wiki.jikexueyuan.com/project/servlet/handling-date.html: 使用Servlet的最重要的优势之一是可以使用核心Java ...

  4. Android GIS开发系列-- 入门季(15) 网络图层加载

    一.首先我们来看一个网络图层: http://services.arcgisonline.com/arcgis/rest/services/World_Street_Map/MapServer,这是全 ...

  5. [React] Prevent Unnecessary Rerenders of Compound Components using React Context

    Due to the way that React Context Providers work, our current implementation re-renders all our comp ...

  6. J2SE基础:11.异常处理

    1:异常的概念: 异常是程序在执行时发生的事件(异常发生在执行期间). 程序出现错误.打断原本的运行流程. 2:Java中处理异常. 在Java中.异常被封装成一个对象.(属性和方法) 3:异常产生 ...

  7. 树状数组 LA 4329 亚洲赛北京赛区题

    复习下树状数组 还是蛮有意思的一道题: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&cat ...

  8. 怎样在gluster的源代码中加入自己的xlator

    本文并不说明怎样编写一个xlator.在glusterfs3.6.1下验证成功 目标在glusterfs-3.6.1/xlators/debug/下建立一个自己的xlator库并能够统一安装到系统文件 ...

  9. [软件分享]aboboo英语复读机 使用心得

    软件名称:aboboo www.aboboo.com 作用:英语复读,社区互动,丰富的材料,可以全方位锻炼听说能力. 技巧1:如何锻炼口语? 注册一个帐号,登陆后下载使用社区自带的课件,然后使用“随意 ...

  10. java Bean及其使用

    1 getter/setter方法 java例子: public class student { private String name; public String getName() { retu ...