[Problem]
输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数
条件: 1.P,Q是正整数
2.要求P,Q以x0为最大公约数,以y0为最小公倍数.
试求:满足条件的所有可能的两个正整数的个数.
[Solution]
#include <vector> #include <iostream> using namespace std; /** * swap */ void swap(int &a, int &b){ a ^= b; b ^= a; a ^= b; } /** * greatest common divisor and least common multiple */ void GCD_LCM(int a, int b, int &gcd, int &lcm){ lcm = a * b; if(a > b){ swap(a, b); } while(b % a != 0){ b %= a; if(a > b){ swap(a, b); } } gcd = a; lcm /= a; } /** * main */ int main(){ int a, b; while(cin >> a >> b){ vector<int> tmp; // generate candidates for(int i = 1; i*a <= b; ++i){ if(b % (i*a) == 0){ tmp.push_back(i*a); } } // check result int result = 0; for(int i = 0; i < tmp.size(); ++i){ for(int j = i + 1; j < tmp.size(); ++j){ int gcd = 0, lcm = 0; GCD_LCM(tmp[i], tmp[j], gcd, lcm); if(gcd == a && lcm == b){ if(tmp[i] != tmp[j]){ result += 2; } else{ result++; } } } } // output result cout << result << endl; } return 0; }