Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

Submit Status

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
 

Source

ECJTU 2009 Spring Contest
题意:给定n和m,m小于n,求x在1到n之间,满足gcd(x,n)>=m。问这样的x有多少个,实际上x是从m到n之间的。
题解:考察对欧拉函数本质的理解,欧拉函数指的是对于一个数来说小于等于该数的数中与其互质的数的个数。找出n的所有大于m的因子x,设phi(i)为i的欧拉函数值,那么答案就是phi(n/x)的和。比如第二组样例n=10,m=2,x=2,5,10,注意x=1是不满足条件的。答案就是phi(10/2)+phi(10/5)+phi(10/10)=4+1+1=6。phi(10/2)=4代表的是2*1=2 , 2*2=4 , 2*3=6 , 2*4=8 这四个数;phi(10/5)=1代表的是5*1=5;phi(10/10)=1代表的是10*1=10。那么为什么是这样呢?因为我们知道该因子x已经满足条件,比如2满足条件,我们可以让这个数变大为x*y,但一定要在10/2=5的范围之内,而且gcd(n,x*y)=x,因为x不能超过n,也就是把2乘以一个小于5的数y,而且这个y一定要与5是互素的。因为我们一定要保证gcd(n=2*5,2*y)=2,否则就出现重复了。举个例子,比如n=12,3是满足条件的,12/3=4,现在让3变大,2与4是不互素的我们让3*2=6,gcd(12,6)=6!=3,这就出现重复了就不对了,6不是现在算的时候。而欧拉函数值恰好是求一个数小于等于该数中与其互质的数的个数,所以用欧拉函数做就可以了,注意n的范围较大要用sqrt(n)算,因为我们在算小的这一侧因子的时候大的因子也会跟着出来,理解了欧拉函数的本质本题也就迎刃而解了。如描述有错误欢迎指正。
#include <iostream>
#include <cmath>
using namespace std;
int enlur(int n) //求欧拉函数
{
int ans=n;
for(int i=;i*i<=n;i++)
{
if(n%i==)
{
ans=ans/i*(i-);
while(n%i==)
n/=i;
}
}
if(n>)
ans=ans/n*(n-);
return ans;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m,ans=;
cin>>n>>m;
int tmp=sqrt(n);
for(int i=;i<=tmp;i++)
{
if(n%i==)
{
if(i>=m)
ans+=enlur(n/i);
if(n/i>=m)
ans+=enlur(i);
}
}
if(tmp*tmp==n&&tmp>=m) //注意得是tmp>=n
ans-=enlur(tmp);
cout<<ans<<endl;
}
}

HDU 2588 GCD (欧拉函数)的更多相关文章

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

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

  2. 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 和 ...

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

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

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

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

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

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

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

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

  7. BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】

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

  8. HDU 2824 简单欧拉函数

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

  9. 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. BZOJ-2037 Sue的小球 DP+费用提前

    似乎很早时学长考过很类似的? 2037: [Sdoi2008]Sue的小球 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 558 Solved: 300 ...

  2. HDU 4901 The Romantic Hero

    The Romantic Hero Time Limit: 3000MS   Memory Limit: 131072KB   64bit IO Format: %I64d & %I64u D ...

  3. CVE: 2014-6271、CVE: 2014-7169 PATCH方案分析

    目录 . RedHat官方给的PATCH第一套方案 . RedHat官方给的PATCH临时方案 . RedHat官方给的PATCH第二套方案 1. RedHat官方给的PATCH第一套方案 0x1: ...

  4. easyloader.js源代码分析

    http://www.cnblogs.com/jasonoiu/p/easyloader_source_code_analysis.html Jquery easyui是一个javascript UI ...

  5. jquery------脚注的使用

    index.jsp <script type="text/javascript" src="../js/my.js"></script> ...

  6. hdu 2046 骨牌铺方格

    #include<stdio.h> int main(void) { long long i,n,narr[55]; narr[1]=1;narr[2]=2;narr[3]=3; for( ...

  7. css3应用之自定义选中文字的背景颜色

    在看很多的博客主题时候发现大多数都对选中文字的背景颜色做了相应的处理.其实这样是很符合用户体验的.因为有很多的人会用鼠标选择着一行一行的阅读.其中就包括本人... 浏览器中默认的选中的文字颜色为白色, ...

  8. get_magic_quotes_gpc函数

    magic_quotes_gpc函数在php中的作用是判断解析用户提示的数据,如包括有:post.get.cookie过来的数据增加转义字符“\”,以确保这些数据不会引起程序,特别是数据库语句因为特殊 ...

  9. ucenter实现原理

    其实Ucenter实现同步登陆的原理就是cookie,一个应用登陆成功之后,向Ucenter传递数据(post方式),让Ucenter通知其他的应用也设置 cookie(get方式),这样用户在访问其 ...

  10. Linux里如何查找文件内容 (转)

    Linux查找文件内容的常用命令方法. 从文件内容查找匹配指定字符串的行: $ grep "被查找的字符串" 文件名例子:在当前目录里第一级文件夹中寻找包含指定字符串的.in文件g ...