杭电2588-GCD
来源:互联网 发布:金融互助网站源码 编辑:程序博客网 时间:2024/06/05 02:14
GCD
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 813 Accepted Submission(s): 364
Problem 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.
(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
31 110 210000 72
Sample Output
16260/*我们可以在sqrt(n)的时间内找到n的所有约数,而这些约数和n的最大公约数就是这个约数本身我们可以寻找x>=m且x是n的约数,最后答案就是每一个大于m的约数xans=sigma(eular(n/x))可以大概的说一下为什么这样处理因为对于x的倍数和n取最大公约数,则其值完全可能大于x。那么可以证明出,必然在m<=k*x<=n之间一定存在eular(n/x)个数与n的最大公约数是x*/#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cmath>#include<queue>#include<stack>#include<set>#include<map>#include<string>#include<cstring>#include<iomanip>const int MAX=100000;__int64 divsor[MAX];__int64 top;using namespace std;void Div(__int64 n){ __int64 i; for(i=1;i*i<n;i++) { if(n%i==0) { divsor[top++]=i; divsor[top++]=n/i; } } if(n%i==0) divsor[top++]=i; sort(divsor,divsor+top);}__int64 eular(__int64 n){ __int64 i; __int64 ans; ans=n; for(i=2;i*i<=n;i++) { if(n%i==0) { ans-=ans/i; while(n%i==0) n/=i; } } if(n>1) ans-=ans/n; cout<<ans<<endl; return ans;}int main(){ int t; __int64 n,m,i,sum; cin>>t; while(t--) { cin>>n>>m; top=0; sum=0; Div(n); for(i=0;i<top;i++) { if(divsor[i]>=m) { //sum+=1; sum+=eular(n/divsor[i]); } } cout<<sum<<endl; } return 0;}
- 杭电2588-GCD
- 杭电2588GCD
- 杭电acm 2588GCD
- 杭电 Problem1787 GCD Again
- 【杭电】[1787]GCD Again
- GCD HDU杭电2588 【欧拉函数的应用】
- 杭电2588 GCD(欧拉函数+gcd的应用)
- 又见GCD(杭电2504)(gcd)
- 【杭电oj】2504 - 又见GCD(GCD)
- 杭电“GCD Again ”解题报告
- GCD Again HDU杭电1787
- 杭电acm 2504又见GCD
- 杭电5656 CA Loves GCD
- 【杭电】[2504]又见GCD
- 杭电-oj】-2504-又见GCD
- 杭电-1108 最小公倍数 (LCM&GCD)
- 杭电-2504 又见GCD
- 杭电-1722 Cake (GCD)
- Poj 3320(two pointers)
- Servlet
- 用Activity的onTouchEvent方法实现监听手指上下左右滑动
- 巴什博奕小结 HDU2188 HDU1846 HDU2149
- 补一个简单的数据结构——带权并查集
- 杭电2588-GCD
- 关于AES加密(上)
- django book学习笔记
- 奇偶剪枝
- [leetcode刷题系列]Plus One
- C# 获取本机所有IP、默认网关、子网掩码
- War Games 2
- 没有'\0'标记的字符串输出会是怎样的呢?
- hdu-Knight Moves