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. 粒子群优化算法-python实现

    PSOIndividual.py import numpy as np import ObjFunction import copy class PSOIndividual: ''' individu ...

  2. spring 第一篇(1-1):让java开发变得更简单(下)

    切面(aspects)应用 DI能够让你的软件组件间保持松耦合,而面向切面编程(AOP)能够让你捕获到在整个应用中可重用的组件功能.在软件系统中,AOP通常被定义为提升关注点分离的一个技术.系统由很多 ...

  3. poj 3261 二分答案+后缀数组 求至少出现k次的最长重复子序列

    #include "stdio.h" #define maxn 20010 int wa[maxn],wb[maxn],wv[maxn],ws[maxn]; int rank[ma ...

  4. ZOJ 2110 Tempter of the Bone

    Tempter of the Bone Time Limit: 2 Seconds      Memory Limit: 65536 KB The doggie found a bone in an ...

  5. Codeforces 85D Sum of Medians

    传送门 D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standa ...

  6. hdu acmsteps 2.1.3 Cake

    Cake Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

  7. hdu 2084 数塔(动态规划)

    本题是一个经典的动态规划题. 直接利用记忆化搜索:见图解 Ac code : #include<stdio.h> #include<string.h> #define max( ...

  8. 快速tab应用

    ZCTabNav-master https://github.com/zcsoft/ZCTabNav 层次构架清楚,很适合快速,导入

  9. linux kernel thread(Daemons)

    内核线程是直接由内核本身启动的进程.内核线程实际上是将内核函数委托给独立的进程,与系统中其他进程“并行”执行(实际上,也并行于内核自身的执行),内核线程经常被称为内核“守护进程”.它们主要用于执行下列 ...

  10. ubuntu系统自带的火狐(firefox)如何安装Adobe Flash

    当你刚装完系统,发现打开某些网站时,提示你“需要安装flash”,然后你点击确定,过了一会,提示你安装失败. 我也是遇到这种情况.我第一个反应是,我先不用firefox,我安装chrome. 可是当你 ...