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.

InputThe 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.OutputFor each test case,output the answer on a single line.Sample Input

3
1 1
10 2
10000 72

Sample Output

1
6
260

这是一道不错的题目,很有启发性。

假设x<=n,n=p*d,x=q*d.假设n与x的最大公约数为d,则能够推出p与q肯定是互质的,因为x<=n所以要求的就是p的欧拉函数值了,那么我们就转化成求满足:n=p*d,并且d>=m的p的欧拉函数值之和了。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#define N 1000010
using namespace std;
typedef long long ll;
int prime[N];
bool vis[N];
int pn=0;
ll a[N];
long long p(long long n){ //返回euler(n)
long long res=n,a=n;
for(long long i=2;i*i<=a;i++){
if(a%i==0){
res=res/i*(i-1);//先进行除法是为了防止中间数据的溢出
while(a%i==0) a/=i;
}
}
if(a>1) res=res/a*(a-1);
return res;
}
int main()
{
for (int i = 2; i < N; i++) {
if (vis[i]) continue;
prime[pn++] = i;
for (int j = i; j < N; j += i)
vis[j] = 1;
}
ll i,j,n,m;
int t;
scanf("%d",&t);
while(t--)
{
ll ans=0;
scanf("%lld%lld",&n,&m);
for(i=1;i*i<=n;i++)
{
if(n%i==0)
{
if(i>=m&&i*i!=n)
ans+=p(n/i);
if(n/i>=m)
ans+=p(i);
}
}
cout<<ans<<endl; } }

  

hdu_2588_GCD的更多相关文章

随机推荐

  1. 绘图和数据可视化工具包——matplotlib

    一.Matplotlib介绍 Matplotlib是一个强大的Python**绘图**和**数据可视化**的工具包. # 安装方法 pip install matplotlib # 引用方法 impo ...

  2. Spring MVC 参数必填项导致客户端报 HTTP 400 并且无法进入断点的问题

    1.问题 Spring MVC 在参数上设置了必填项,post 请求时报 HTTP 400 并且未进入断点,如将“年龄”设置为了必填项: @RequestParam( value="age& ...

  3. Web安全色的意义

    问题: 不同的平台(Mac.PC等)有不同的调色板,不同的浏览器也有自己的调色板.这就意味着对于一幅图,显示在Mac上的Web浏览器中的图像,与它在PC上相同浏览器中显示的效果可能差别很大. 选择特定 ...

  4. Ubuntu16.04安装Hadoop2.6+Spark1.6+开发实例

    Ubuntu16.04安装Hadoop2.6+Spark1.6,并安装python开发工具Jupyter notebook,通过pyspark测试一个实例,調通整个Spark+hadoop伪分布式开发 ...

  5. myeclipse 2014 闪退问题解决

    1.删掉Workspace下面的  \.metadata\.plugins\org.eclipse.core.resources一整个文件夹 2.删掉Workspace 下面的 \.metadata\ ...

  6. (五)TortoiseSVN 客户端-----安装

    svn客户端类型 svn客户端需要通过网络访问svn服务端提交文件.查询文件等,可通过以下客户端类型访问svn服务端: 使用Subversion提供的客户端命令,使用方式:在命令行下输入命令操作. 使 ...

  7. Intel® Manager for Lustre* software(一)

    Intel® Manager for Lustre* software Installation 软件安装指导目录: 安装IML(Intel® Manager for Lustre* software ...

  8. 源码安装mysql,及主从同步

    源码安装mysql [可选] 如果用源码安装cmake软件: cd /home/oldboy/tools/ tar xf cmake-.tar.gz cd cmake- ./configure #CM ...

  9. npm 安装axios和使用增删改查

    1:安装axios(建议安装淘宝镜像) npm install axios 2:项目导入 npm install --save axios vue-axios 3:页面导入 import axios ...

  10. June 17th 2017 Week 24th Saturday

    Absence sharpens love, presence strengthens it. 相聚爱益切,离别情更深. There is almost no such love that can i ...