D—GCD (HDU 2588)
2016-03-21 20:39
375 查看
传送门
Total Submission(s): 1438 Accepted Submission(s): 673
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case,output the answer on a single line.
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
题目大意:
给定T组数据,然后两个数 N和 M, 让你求1<=X<=N ,GCD (X,N)>=M,让你X的个数
解题思路:
假设 GCD(X,N) = d,那么 X = q * d , N = p * d,而且p 和 q 肯定是互素的,如果d >= M的话,我们要求的只是在 <=p的情况下与 p 互素的个数,然后累加 ,就是假设 N的约数 i >= M,就是求与 <= N/i中与 N/i的个数,也就是求一个欧拉函数,所以这个题就是相当于 欧拉函数的延伸,还是比较不错的,当知道这个之后就是从 1 - N 中找,但是这样还是会超时的,所以我们就从 sqrt(N)中找 ,N%i
== 0 和 N%(N/i) ==0,最后特判一下是不是完全平方数就ok了,剩下的就是敲代码了:
My Code:
GCD
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1438 Accepted Submission(s): 673
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case,output the answer on a single line.
[align=left]Sample Input[/align]
3 1 1 10 2 10000 72
[align=left]Sample Output[/align]
1 6 260
题目大意:
给定T组数据,然后两个数 N和 M, 让你求1<=X<=N ,GCD (X,N)>=M,让你X的个数
解题思路:
假设 GCD(X,N) = d,那么 X = q * d , N = p * d,而且p 和 q 肯定是互素的,如果d >= M的话,我们要求的只是在 <=p的情况下与 p 互素的个数,然后累加 ,就是假设 N的约数 i >= M,就是求与 <= N/i中与 N/i的个数,也就是求一个欧拉函数,所以这个题就是相当于 欧拉函数的延伸,还是比较不错的,当知道这个之后就是从 1 - N 中找,但是这样还是会超时的,所以我们就从 sqrt(N)中找 ,N%i
== 0 和 N%(N/i) ==0,最后特判一下是不是完全平方数就ok了,剩下的就是敲代码了:
My Code:
#include <iostream> #include <cstdio> #include <cmath> using namespace std; int Eular(int m) { int ret = m; for(int i=2; i*i<=m; i++) { if(m%i == 0) { ret -= ret/i; while(m%i == 0) m /= i; } } if(m > 1) ret -= ret/m; return ret; } int main() { int T, m, n; cin>>T; while(T--) { cin>>n>>m; int sum = 0; for(int i=1; i*i<n; i++)///这样比较省时间 { if(n%i == 0)///n的约数 { if(i >= m) { sum += Eular(n/i); } if(n/i >= m) sum += Eular(i); } } if((int)sqrt(n)>=m)///特判 { if((int)sqrt(n)*(int)sqrt(n) == n) sum += Eular((int)sqrt(n)); } cout<<sum<<endl; } return 0; }
相关文章推荐
- VS2013安装及测试
- 通过匹配绑定select option的文本值 模糊匹配
- Nyoj 星际之门(一)(Cayley定理)
- IOS-数据缓存
- hrbust 1935 哈理工oj 1935 PROBLEM-FIVE【贪心】
- Java核心知识点-Java编译原理
- 数据挖掘学习
- 《构建之法》——1~3章
- 异步dcfifo的读写
- 作业周转时间以及平均等待时间
- Unity3D 内存优化(一)对象池
- 【poj 2251】Dungeon Master BFS
- 赋值操作符
- android:configChanges属性
- 程序阅读
- 词频统计,感觉好难
- 第一个Spark程序
- [leetcode]题解+经验总结 自己做法+评论区mostvotes汇总 (只包含部分easy难度)
- 华为OJ--高次方数的尾数
- 文档分类算法总结