GCD & LCM 一个神奇的式子
来源:互联网 发布:阿沁淘宝店叫什么 编辑:程序博客网 时间:2024/06/06 06:56
Description
Given x and y (2 <= x <= 100,000, 2 <= y <= 1,000,000), you are to count the number of p and q such that:
1) p and q are positive integers;
2) GCD(p, q) = x;
3) LCM(p, q) = y.
Input
x and y, one line for each test.
Output
Number of pairs of p and q.
Sample Input
3 60
Sample Output
4
这道题的精髓就在于:
a*b=GCD*LCM
=> (a/GCD) * (b/GCD) = LCM/GCD
这样一来,就可以把a,b(也就是题目中的p,q)的范围大大的缩小;
并且可以发现,a/GCD , b/GCD互质。
根据这条性质,可以排除更多种的情况。
代码实现:
#include<iostream>using namespace std;int GCD(int a,int b){ return b==0?a:GCD(b,a%b);}int main(){ int n,m; while(cin>>n>>m){ int sum=0; if(m%n!=0){ cout<<"0"<<endl; continue; } int m1=m/n; for(int i=1;i<=m1;i++){ if(m1%i==0){ if(GCD(i,m1/i)==1){ sum++; } } } cout<<sum<<endl; } return 0;}
- GCD & LCM 一个神奇的式子
- POJ 2429GCD & LCM Inverse(大数分解)
- GCD and LCM+hdu+利用gcd和lcm的性质
- gcd,lcm
- gcd & lcm
- LCM GCD
- GCD LCM
- lcm/gcd
- gcd & lcm
- GCD LCM
- 【GCD&LCM】
- gcd && lcm
- 一个url的正则验证式子
- 有关GCD和LCM的公式
- 关于lcm,gcd的一些性质
- GCD&&LCM的一些经典问题
- GCD和LCM的一些性质
- hdu-4497-GCD and LCM-gcd的性质
- 三角形的有向面积
- Linux安装JDK1.7
- 删数问题
- Word 2007中添加MathType选项卡
- Letter Grid(动规)
- GCD & LCM 一个神奇的式子
- 图 poj 1985
- 图 poj 1511
- 旋转卡壳
- USC newweek2 H hdu 3335
- USC newweek2 G
- DP 记忆化搜索 poj 1088
- poj 1743 字符串 后缀数组 不可重叠最长重复子串
- 取多次方的前n位