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. Redis的Publish/Subscribe

    Publish/Subscribe 从字面上理解就是发布(Publish)与订阅(Subscribe),在Redis中,你可以设定对某一个key值进行消息发布及消息订阅,当一个key值上进行了消息发布 ...

  2. Java内部类详解 2

    Java内部类详解 说起内部类这个词,想必很多人都不陌生,但是又会觉得不熟悉.原因是平时编写代码时可能用到的场景不多,用得最多的是在有事件监听的情况下,并且即使用到也很少去总结内部类的用法.今天我们就 ...

  3. css box-shadow知识点及多重边框

    box-shadow() 参数: h-shadow:水平阴影的位置.允许负值. v-shadow:垂直阴影的位置.允许负值. blur:模糊距离. spread:扩张半径(可正可负.投影面积则可大可小 ...

  4. Ubuntu16.04安装Docker1.12+开发实例+hello world+web应用容器

    本次主要是详细记录Docker1.12在Ubuntu16.04上的安装过程,创建Docker组(避免每次敲命令都需要sudo),Docker常用的基本命令的总结,在容器中运行Hello world,以 ...

  5. Elipse plugin or Bundle & OSGI

    Develop and register service, lookup and use service! Android Design on service's publish-find-bind ...

  6. CSS布局中的问题解决方式

    1.解决搜索框和按钮不对齐的方法 vertical-align属性只有两个元素设置为display:inline-block才有效 2.盒子莫名的分行现象 问题描述:比如父盒子宽度为960px,两个左 ...

  7. asyncio标准库5 TCP echo client and server

    server import asyncio async def handle_echo(reader, writer): data = await reader.read(100) message = ...

  8. 通过vue-cli3构建一个SSR应用程序

    1.前沿 1.1.什么是SSR SSR(服务端渲染)顾名思义就是将页面在服务端渲染完成后在客户端直接展示. 1.2.客户端渲染与服务端渲染的区别 传统的SPA模式 即客户端渲染的模式 Vue.js构建 ...

  9. Python3基本数据类型(一、数字类型)

    第一次写博客,感觉心情比较紧张,有一种要上台演讲的紧张感(虽然可能大概也许不会有人看).在此立个flag,以后每个学习阶段都要写一篇博客,来记录自己学习成长的这段日子.Fighting! 废话不多说, ...

  10. Uva 11922 Splay

    Splay(伸展树)实现可分裂与合并的序列 对于BST,除了Treap树之外,还有一种Splay的伸展树,他能快速的分裂与合并. 重要的操作是伸展操作,将一个指定的结点 x 旋转到根的过程. 分三种情 ...