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. Jquery系列:checkbox 获取值、选中、设置值、事件监听等操作

    <div id="divId" class="divTable"> <div class="tableBody"> ...

  2. Cocos2d-js 开发记录:Loading载入界面自定义

    Loading界面是一个cc.Scene具体请看: http://blog.csdn.net/jonahzheng/article/details/38348255 如果仅仅是想把图片(cocos l ...

  3. CRM——讲师与学生

    一.课程记录和学习记录 1.初始化 course_record, study_record.2.学习记录3.录入成绩4.显示成绩 ajax 查询 柱状图展示成绩 highcharts 5.上传作业(o ...

  4. mui使用技巧

    1.document.addEventListener('plusready', function(){ //console.log("所有plus api都应该在此事件发生后调用,否则会出 ...

  5. 【读书笔记】如何高效学习(Learn More ,Study Less)

    导读: 你会不会好奇为什么学习好的人会学的越来越好?那些课下不学习的人却比你考的好?一个人是怎么同时拿到好几个学位?为啥反复背的知识要领总是忘?为啥看个书总是不停走神?为啥总是苦逼似得看书直至厌烦? ...

  6. django视图函数解析(三)

    1 视图views概述 1 作用: 视图接受web请求并响应web请求 2 本质: 视图就是python中的处理函数 3 响应: 一般是一个网页的HTML内容.一个重定向.错误信息页面.json格式的 ...

  7. 使用pm2自动化部署node项目

    1.pm2简介 pm2(process manager)是一个进程管理工具,维护一个进程列表,可以用它来管理你的node进程,负责所有正在运行的进程,并查看node进程的状态,也支持性能监控,负载均衡 ...

  8. Linux --Mysql数据库搭建

    Mysql数据库 安装 准备: [root@localhost /]# rpm -e mysql --nodeps 将rpm方式安装的mysql卸载   [root@localhost /]# gro ...

  9. RAC环境修改数据库字符集

    sql> alter system set cluster_database=false scope=spfile sid='qcjk1';   --------注意sid根据不同环境要修改 在 ...

  10. CSS z-index的用法

    理清 position及z-index的用法: static :  无特殊定位,对象遵循HTML定位规则absolute :  将对象从文档流中拖出,使用left,right,top,bottom等属 ...