GCD

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3559    Accepted Submission(s): 1921

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

题解:对于gcd(x,n)>=m;设gcd(a,b)=1,x=a*i,n=b*i,gcd(x,n)=i>=m,b=n/i,只需求小于等于b的与b互质的数的个数(也就是eular(b));

#include<iostream>
#include<algorithm>
#include<string.h>
#define ll long long
using namespace std;
ll eular(ll n)//欧拉函数模板
{
ll res=n;
for(ll i=2;i<=n;i++){
if(n%i==0){
res=res/i*(i-1);//p^i-p^(i-1)
while(n%i==0)
n/=i;
}
}
return n>1?res/n*(n-1):res;//最后不为1的情况
}
int main()
{
int T;
ll N,M;
scanf("%d",&T);
while(T--){
ll ans=0;
scanf("%lld%lld",&N,&M);
for(ll i=1;i*i<=N;i++){
if(!(N%i)){
if(i>=M)
ans+=eular(N/i);//gcd(x,m)=i>=m;
if(i*i!=N&&N/i>=M)//gcd(x,m)=n/i>=m;
ans+=eular(i);//i=N/(N/i);
}
}
printf("%lld\n",ans);
}
return 0;
}

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. BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4436  Solved: 1957[Submit][Status][Discuss ...

  9. HDU 2824 简单欧拉函数

    1.HDU 2824   The Euler function 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=2824 3.总结:欧拉函数 题意:求(a ...

  10. POJ 2773 Happy 2006【GCD/欧拉函数】

    根据欧几里德算法,gcd(a,b)=gcd(a+b*t,b) 如果a和b互质,则a+b*t和b也互质,即与a互质的数对a取模具有周期性. 所以只要求出小于n且与n互质的元素即可. #include&l ...

随机推荐

  1. Tesseract处理背景渐变的图片

    在Tesseract处理背景渐变图片不太理想的情况下, 可以利用Pillow库, 创建一个阈值过滤器来去掉渐变的背景色, 只把文字留下来, 从而让图片更清晰, 便于Tesseract读取: from ...

  2. python用WMI模块获取系统命名空间

    可以和winmgmts的查询页面对应 from win32com.client import GetObject import pywintypes result=[] def enum_namesp ...

  3. rman 示例

    背景 rman,听这名字,好像有点真的男人意思.这玩意其实也简单,只是老忘,作为一个oracle dba爱好者,怎么少了rman呢,这个好像是oracle体系的最后一环,把它掌握了,就完成oracle ...

  4. slice的部分说明

    1.slice是数值的一个引用,并不会新增内存地址. 2.slice的容量和长度是两个概念,这个长度跟数组的长度是一个概念,即在内存中进行了初始化实际存在的元素的个数.何谓容量?如果通过make函数创 ...

  5. 021_mac提效神奇Alfred

    一.破解版下载 (1)https://pan.baidu.com/s/1Kb0HtybvdA1yzHeOWUFM_w 提取码:9tq2 Reference:https://www.jianshu.co ...

  6. ubuntu html5开发工具brackets

    Brackets 是一款使用 HTML,CSS,JavaScript 创建的开源的针对 Web 开发的编辑器.实时预览,快速编辑,跨平台,可扩展,开源,让 Brackets 成为一款非常优秀的编辑器. ...

  7. Ubuntu解除"输入密码以解锁密钥环”

    解决办法有两种: 1.去掉默认密钥环的密码: 打开应用程序->附件->密码和加密密钥(如果你的没有,在终端中输入 seahorse),切换到密码选项卡,会看到一个密码密钥环(我的密钥环是 ...

  8. 【算法】二分查找法&大O表示法

    二分查找 基本概念 二分查找是一种算法,其输入是一个有序的元素列表.如果要查找的元素包含在列表中,二分查找返回其位置:否则返回null. 使用二分查找时,每次都排除一半的数字 对于包含n个元素的列表, ...

  9. iOS9 新功能:Support Universal Links,iOS10 openUrl新函数

    先看官方文档:https://developer.apple.com/library/ios/documentation/General/Conceptual/AppSearch/UniversalL ...

  10. awk-for循环简单用法

    文本: [root@VM_0_84_centos ~]# cat sshd.txt 1 2 3 4 5 6 7 8 9 循环打印上述文本 for 循环的固定格式   i=1设置i的初始变量  i< ...