洛谷【 P1029 最大公約數和最小公倍數問題】
阿新 • • 發佈:2019-01-01
題目描述
輸入2個正整數x0,y0(2≤x0<100000,2≤y0<=1000000),求出滿足下列條件的P,Q的個數
條件:
-
P,Q是正整數
-
要求P,Q以x0為最大公約數,以y0為最小公倍數.
試求:滿足條件的所有可能的2個正整數的個數.
輸入輸出格式
輸入格式:
2個正整數x0,y0
輸出格式:
1個數,表示求出滿足條件的P,Q的個數
輸入輸出樣例
輸入樣例#1:
3 60
輸出樣例#1:
4
說明
P,Q有4種
1、3,60
2、15,12
3、12,15
4、60,3
題解:gcd(a, b) * lcm(a, b) = a*b; 根據題目條件可得 P*Q = x0 * y0. 逐一判斷 x0, y0 中較小的那個數的倍數即可。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <vector> #define ll long long #define mod 100000000 using namespace std; const int maxn = 1000010; ll gcd(ll a, ll b){ return b ? gcd(b, a%b) : a; } ll lcm(ll a, ll b){ return a*b/gcd(a, b); } int main() { int x0, y0; cin >> x0 >> y0; ll s = x0*y0; int m = min(x0, y0); int cnt = 0; for(ll i = m; i <= s/m*m; i += m){ if(s % i == 0){ if(gcd(i, s/i) == x0 && lcm(i, s/i) == y0) cnt++; } } cout << cnt << endl; return 0; }