求最大公約數和最小公倍數的標準解法(記住)
阿新 • • 發佈:2017-10-26
button one none esc sam per efault 等級 b-
1012 最大公約數和最小公倍數問題
2001年NOIP全國聯賽普及組
時間限制: 1 s 空間限制: 128000 KB 題目等級 : 白銀 Silver 題目描述 Description輸入二個正整數x0,y0(2<=x0<100000,2<=y0<=1000000),求出滿足下列條件的P,Q的個數
條件: 1.P,Q是正整數
2.要求P,Q以x0為最大公約數,以y0為最小公倍數.
試求:滿足條件的所有可能的兩個正整數的個數.
輸入描述 Input Description二個正整數x0,y0
輸出描述 Output Description滿足條件的所有可能的兩個正整數的個數
樣例輸入 Sample Input3 60
樣例輸出 Sample Output4
#include <bits/stdc++.h> using namespace std; int gcd(int a,int b) //最大公約數的標準寫法(輾轉相除法) { if(b == 0) return a; return gcd(b,a%b); } int lcm(int a,int b) { return a*b/gcd(a,b); } int main() { int x,y; cin>> x >> y; int ans = 0; for(int i=x;i <= y;i = i+x) { int j = x*y/i; if(gcd(i,j) == x && lcm(i,j) == y) ans++; } cout << ans <<endl; }
最大公約數和最小公倍數的乘積和a,b乘積相等
求最大公約數和最小公倍數的標準解法(記住)