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. C++字符串读入

    int read() { ,f=;char ch=getchar(); ;ch=getchar();} +ch-';ch=getchar();} return x*f; } int main() { ...

  2. VIM使用技巧15

    在vim的插入模式下,有时需要插入寄存器中的文本: 1.使用<C-r>{register} 2.使用<C-r><C-p>{register} 3.使用<C-r ...

  3. 转 POJ分类

    OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期: 一. ...

  4. hadoop(1)入门

    hadoop入门(一)   一.概述 1.什么是hadoop hadoop不仅是一个用于存储分布式文件系统,还是设计用来在有通用计算设备组成的大型集群上执行的分布式应用的基础框架. hadoop框架最 ...

  5. dtrace-conf 2016

    https://www.joyent.com/about/events/2016/dtrace-conf

  6. linux 虚拟网卡

    linux中可以通过一个物理网卡,模拟出多个虚拟网卡,并在网卡中配置ip. 下面做一个实验. 实验描述: 我们有server A (ip 10.79.148.205),server B (10.79. ...

  7. DELL T110II Server如何通过RAID 级别迁移的方式在OMSA下实现磁盘阵列扩容?

    目录: RAID 转移规则说明 操作步骤 本文介绍了 通过RAID 级别转换来实现扩容的方法注意:本文相关RAID的操作,仅供在测试环境里学习和理解戴尔PowerEdge服务器RAID控制卡的功能和使 ...

  8. 二叉查找树(BST)

    二叉查找树(BST) 二叉查找树(Binary Search Tree)又叫二叉排序树(Binary Sort Tree),它是一种数据结构,支持多种动态集合操作,如 Search.Insert.De ...

  9. JavaScript 获得代码行号和脚本文件名

    如果你使用的是 V8 引擎,Chrome 和 Node.js 所用的,那么你可以利用 JavaScriptStackTraceApi 来获得行号信息,有两个 API: Error.captureSta ...

  10. Unity3D &amp; C# 设计模式--23

     Unity3D & C#Design Patterns 23 design patterns. Creational Patterns 1. Abstract Factory抽象工厂 创 ...