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

  1. 3
  2. 1 1
  3. 10 2
  4. 10000 72

Sample Output

  1. 1
  2. 6
  3. 260

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

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

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<algorithm>
  5. #include<cstdio>
  6. #define N 1000010
  7. using namespace std;
  8. typedef long long ll;
  9. int prime[N];
  10. bool vis[N];
  11. int pn=0;
  12. ll a[N];
  13. long long p(long long n){ //返回euler(n)
  14. long long res=n,a=n;
  15. for(long long i=2;i*i<=a;i++){
  16. if(a%i==0){
  17. res=res/i*(i-1);//先进行除法是为了防止中间数据的溢出
  18. while(a%i==0) a/=i;
  19. }
  20. }
  21. if(a>1) res=res/a*(a-1);
  22. return res;
  23. }
  24. int main()
  25. {
  26. for (int i = 2; i < N; i++) {
  27. if (vis[i]) continue;
  28. prime[pn++] = i;
  29. for (int j = i; j < N; j += i)
  30. vis[j] = 1;
  31. }
  32. ll i,j,n,m;
  33. int t;
  34. scanf("%d",&t);
  35. while(t--)
  36. {
  37. ll ans=0;
  38. scanf("%lld%lld",&n,&m);
  39. for(i=1;i*i<=n;i++)
  40. {
  41. if(n%i==0)
  42. {
  43. if(i>=m&&i*i!=n)
  44. ans+=p(n/i);
  45. if(n/i>=m)
  46. ans+=p(i);
  47. }
  48. }
  49. cout<<ans<<endl;
  50.  
  51. }
  52.  
  53. }

  

hdu_2588_GCD的更多相关文章

随机推荐

  1. 使用durid的ConfigFilter对数据库密码加密

    <!-- 配置dbcp数据源 --> <bean id="remoteDS" class="org.apache.commons.dbcp.BasicD ...

  2. PAT 1066 Root of AVL Tree

    #include <cstdio> #include <cstdlib> class Node { public: Node* L; Node* R; int height; ...

  3. Myeclipse中进行JUnit单元测试

    最近学习了在myeclipse中进行单元测试,写点东西总结总结. JUnit单元测试: 测试对象为一个类中的方法. juint不是javase中的部分,所以必须导入jar包,但是myeclipse自带 ...

  4. pl/sql实现金额转换为大写

    FUNCTION upper_case_currency(vrmb char) RETURN varchar2 IS rmbwodecimal varchar2(20); result varchar ...

  5. SpannableString与SpannableStringBuilder使用

    转自:http://blog.it985.com/14433.html1.SpannableString.SpannableStringBuilder与String的关系 首先SpannableStr ...

  6. 将CSV文件中的数据导入到SQL Server 数据库中

    导入数据时,需要注意 CSV 文件中的数据是否包含逗号以及双引号,存在时,导入会失败 选择数据库 -> 右键 -> 任务 -> 导入数据 ,然后根据弹出的导入导出向导(如下图)中的提 ...

  7. c#中abstract、override、new、virtual、sealed使用和示例

    原文地址:http://blog.csdn.net/richerg85/article/details/7407544 abstract      修饰类名为抽象类,修饰方法为抽象方法.如果一个类为抽 ...

  8. 手写vector

    看过JDK源码,现在自己想实现一个vector. 最开始的时候,我大概构想了一下怎么设计,一种是设置一个指针数组来存放对象,这样修改的时候可以不用大量的元素复制,但后来仔细想了想,它需要设置一个额外的 ...

  9. WAKE-WIN10-SOFT-软件-Matlab配置及工具箱

    1Matlab 1,1Matlab下载,安装,配置,,, 1,2 2工具箱 2,1LibSVM 必应:https://www.bing.com/search?q=libsvm&qs=n& ...

  10. vue v-on 带参事件

    1.js代码 var list=[ //定义一个数组 {title:} ] var box=new Vue({ el:'.box', data:{ list:list }, methods:{ //d ...