Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions 数论(GCD和LCM关系)
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// 函数:计算两个数的最大公约数 (GCD)
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

int main() {
const int g = 12; // 已知的 gcd
const int l = 360; // 已知的 lcm

// 检查输入是否合法:必须满足 g*l = a*b 的约束,但这里我们直接找 x*y = l/g
int product_xy = l / g;

if (l % g != 0) {
cout << "Invalid input: LCM must be a multiple of GCD." << endl;
return 0;
}

cout << "Finding all pairs (a, b) such that:" << endl;
cout << " gcd(a, b) = " << g << ", lcm(a, b) = " << l << endl;
cout << " a and b are positive integers, and a <= b" << endl << endl;

vector<pair<int, int>> solutions;

// 枚举所有 x*y = product_xy,且 gcd(x, y) == 1,x <= y
for (int x = 1; x * x <= product_xy; ++x) {
if (product_xy % x == 0) {
int y = product_xy / x;

if (gcd(x, y) == 1) {
int a = g * x;
int b = g * y;

if (a <= b) {
solutions.emplace_back(a, b);
}
}
}
}

// 输出结果
cout << "All possible (a, b) pairs are:" << endl;
for (const auto& p : solutions) {
cout << "(" << p.first << ", " << p.second << ")" << endl;
}

cout << "\nTotal number of solutions: " << solutions.size() << endl;

return 0;
}