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. Qemu-KVM管理

    内容: 一.KVM基本配置 二.KVM网络的桥接 三.创建虚拟机 四.虚拟机的关闭和启动 关于KVM: 1).KVM是开源软件,全称是kernel-based virtual machine(基于内核 ...

  2. java知识点1

    本系列文章源自大神--纯洁的微笑的博客 http://www.cnblogs.com/ityouknow/ 基础篇 JVM JVM内存结构 堆.栈.方法区.直接内存.堆和栈区别 内存结构图 控制参数 ...

  3. cartographer 安装

    Debian 8 Jessie 一顿操作梦如虎,最后不知道咋装上的. 参考:https://www.jianshu.com/p/9922a51ce38f https://blog.csdn.net/p ...

  4. backgroud-size属性

    backgroud-size:100% 改为:backgroud-size:100% 100%; 就能解决下面的问题.

  5. Suspend to RAM和Suspend to Idle分析,以及在HiKey上性能对比【转】

    转自:https://www.cnblogs.com/arnoldlu/p/6253665.html 测试环境:AOSP 7.1.1+Kernel 4.4.17 HW:HiKey Ubuntu 14. ...

  6. 即速应用_微信小程序开发 http://www.jisuapp.cn/

    即速应用_微信小程序开发   http://www.jisuapp.cn/

  7. epoll机制详解

    epoll机制详解 大牛的详解 epoll详解 什么是epoll? epoll是为处理大批量句柄而作了改进的poll, 是性能最好的多路I/O就绪通知方法; 只有三个系统调用: epoll_creat ...

  8. ffmpeg-201701[10,16,21,23,25]-bin.7z

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 5 屏幕横向放大 20 像素 6 屏幕横向缩小 20 像素 S 下一帧 [ -2秒 ] +2 ...

  9. Apache服务器和tomcat服务器有什么区别(转)

    Apache与Tomcat都是Apache开源组织开发的用于处理HTTP服务的项目,两者都是免费的,都可以做为独立的Web服务器运行.Apache是Web服务器而Tomcat是Java应用服务器. A ...

  10. 【原创】大数据基础之Logstash(2)应用之mysql-kafka

    应用一:mysql数据增量同步到kafka 1 准备mysql测试表 mysql> create table test_sync(id int not null auto_increment, ...