GCD and LCM(数学 + GCD)

 

GCD and LCM
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

Write a program which computes the greatest common divisor (GCD) and the least common multiple (LCM) of given a and b (0 < a, b ≤ 2,000,000,000). You can supporse that LCM(a, b) ≤ 2,000,000,000.

Input

Input consists of several data sets. Each data set contains a and b separated by a single space in a line. The input terminates with EOF.

Output

For each data set, print GCD and LCM separated by a single space in a line.

Sample Input

8 6
50000000 30000000

Output for the Sample Input

2 24
10000000 150000000

 

    题意:

    给出 a 和 b(0 <= a,b <= 2000000000),算出 a 和 b 的最大公约数(GCD)和最小公倍数(LCM)。

 

    思路:

    GCD。LCM = (A * B)/ GCD。注意写法:

    1.2 * 10 ^ 9 不会超int,故可以用 int 型,故 gcd 返回的值可以是 int;

    2.LCM 的 A * B 的时候有可能会爆 int 故相除后乘,写成 A / GCD (A,B) * B,有可能最后相乘的时候也会爆 int ,所以最好将其一转化为long long;

    3.不需要将 A 和 B 比较大小再GCD,因为当 A < B,第一次递归的时候 GCD(B,A % B)会等于 GCD(A,B)(因为 A % B == B)。

 

     AC:

#include <stdio.h>

int gcd(int a,int b) {
    return (b ? gcd(b,a % b): a);
}

int main() {
    int a,b;
    while(~scanf("%d%d",&a,&b)) {
        printf("%d %lld\n",gcd(a,b),a / gcd(a,b) * (long long)b);
    }

    return 0;
}

 

### 关于 C++ STL 中 `gcd` 和 `lcm` 的函数用法 在 C++17 及更高版本中,标准库提供了 `<numeric>` 头文件中的两个函数:`std::gcd` 和 `std::lcm`。这两个函数分别用于计算最大公约数(Greatest Common Divisor, GCD)和最小公倍数(Least Common Multiple, LCM)。如果使用的不是 C++17 或更新的标准,则可以依赖 GNU 编译器提供的扩展功能来实现类似的运算[^1]。 #### 使用 C++17 标准下的 `std::gcd` 和 `std::lcm` 以下是基于 C++17 的示例代码: ```cpp #include <iostream> #include <numeric> // std::gcd, std::lcm int main() { int a = 8; int b = 12; // 计算最大公约数 (GCD) int gcd_result = std::gcd(a, b); std::cout << "GCD of " << a << " and " << b << " is: " << gcd_result << std::endl; // 计算最小公倍数 (LCM) int lcm_result = std::lcm(a, b); std::cout << "LCM of " << a << " and " << b << " is: " << lcm_result << std::endl; return 0; } ``` 上述程序展示了如何利用 `<numeric>` 提供的功能完成基本的数学运算。需要注意的是,`std::gcd` 和 `std::lcm` 都接受整型参数并返回相同类型的值。对于负数输入,它们的行为如下: - 如果任意一个参数为零,则 `std::gcd` 返回另一个参数的绝对值。 - 对于 `std::lcm`,当任一参数为零时,结果始终为零[^2]。 #### 在非 C++17 环境下使用 GNU 扩展 如果不支持 C++17 而仅限于较旧编译环境(如 g++),则可以通过调用 GNU 特定的内置函数 `__gcd()` 来替代官方接口。然而,GNU 并未提供直接对应的 `__lcm()` 实现,因此需手动定义该逻辑: ```cpp #include <iostream> // 自定义 LCM 函数 template<typename T> T __lcm(T a, T b) { if (!a || !b) return 0; // 若存在零,则 LCM 定义为零 return abs((a / __gcd(a, b)) * b); } int main() { int x = __gcd(8, 12); // 利用 GNU 内置函数求解 GCD int y = __lcm<int>(8, 12); // 结合自定义方法获取 LCM std::cout << "Using GNU extensions:\n"; std::cout << "GCD of 8 and 12 is: " << x << "\n"; std::cout << "LCM of 8 and 12 is: " << y << "\n"; return 0; } ``` 此片段说明了即使缺乏现代标准的支持,也可以借助其他手段达成目标。不过建议尽可能升级至最新工具链以便享受标准化带来的便利性和兼容性保障[^3]。 ### 注意事项 尽管某些平台可能允许混合不同风格的技术方案解决实际需求,但从长远维护角度出发还是推荐遵循统一规范开发软件产品。此外,在跨平台项目里务必确认所选技术栈能在所有预期部署环境中正常运作[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值