hiho一下144周题,题意是找出随机取出两个数刚好是n,m公因数的概率。思路是分别找出两个数所有因数,相乘得到取数情况,易得公因数个数。再调用求最大公因数函数,输出化简后的数字就行。
map存放n的因数,m每次找到因数时到map去找是否已包含,以此确定公因数,将map理解为一个hashset即可。
#include <iostream>
#include <map>
using namespace std;
typedef long long LL;
//求取最大公因数
LL gcd(LL a, LL b) {
return b == 0 ? a : gcd(b, a % b);
}
int main() {
//n,m读取数字,nCount, mCount分别统计n和m的因数个数,same统计n和m公因数个数
LL n, m, nCount = 0, mCount = 0, same = 0;
cin >> n >> m;
//map存放n的因数
map<LL, bool> factorMap;
//找出n因数
for (LL i = 1; i * i <= n; ++i) {
if (n % i == 0) {
factorMap[i] = true;
nCount++;
if (n / i != i) {
factorMap[n / i] = true;
nCount++;
}
}
}
//找出m因数,同时找出公因数
for (LL i = 1; i * i <= m; ++i) {
if (m % i == 0) {
if (factorMap[i]) same++;
mCount++;
if (m / i != i) {
if (factorMap[m / i]) same++;
mCount++;
}
}
}
LL base = nCount*mCount;
//计算最大公因数
LL gcdMax = gcd(base, same);
cout << base / gcdMax << " " << same / gcdMax;
return 0;
}