您现在的位置是:首页 > 电脑 > 

查一系列大整数的LCM时如何避免溢出错误(How to avoid overflow error when finding LCM for series of large integers)

2025-07-21 04:58:21
查一系列大整数的LCM时如何避免溢出错误(How to avoid overflow error when finding LCM for series of large integers) 我需要到一系列数字的最小公约数(最多10万个数字,每个数字在[0,2 000 000 000]范围内) 我使用以下算法lcm(a,b)=(a / gcd(a,b)
查一系列大整数的LCM时如何避免溢出错误(How to avoid overflow error when finding LCM for series of large integers)

我需要到一系列数字的最小公约数(最多10万个数字,每个数字在[0,2 000 000 000]范围内)

我使用以下算法lcm(a,b)=(a / gcd(a,b))* b

到1cm以上的lcm(a,lcm(b,c))...的标准方法适用于小输入值。

但是,对于大输入,即使我使用long long int,它也会开始出现溢出错误...

如何避免许多较大整数值出现溢出错误?

感谢您的帮助

I need to find least common divisor for a series of numbers (up to 100 000 numbers, each of them is in the range of [0, 2 000 000 000])

I am using the following algorithm lcm(a,b) = (a/gcd(a,b)) * b

The standard method of finding lcm for for than 2 numbers lcm(a, lcm(b,c))... is working for small input values.

However, for large input, it starts giving overflow error even if i used long long int...

How can I avoid having overflow error for many larger integer values?

Thank you for the help

最满意答案

在这种情况下,最小公倍数可以是具有数百个数字的大数。 你需要处理这么大的整数。

gmplib库 ( -lgmp )支持任意精度整数运算:

int have_read_first_number = 0; mpz_t result, arg; mpz_inits(result, arg, ULL); mpz_set_ui(arg, 1u); /* read decimal integers from stdin: one per line */ while((len = getline(&token, &capacity, stdin)) > 0) { if (!have_read_first_number) { /* read the first integer */ parse_mpz(token, result); have_read_first_number = 1; /* successfully read first integer */ } else { /* read the rest of the numbers */ parse_mpz(token, arg); } /* lcm(a, b, c, d) = lcm(lcm(lcm(a, b), c), d) */ mpz_lcm(result, result, arg); } if (!have_read_first_number) /* error */ panic("no numbers read"); /* print result as decimal */ mpz_out_str(stdout, 10, result); puts("");

例:

$ gcc -o lcm -lgmp && { seq 1 100 | ./lcm; } 6972075229712477164580895120556800

完整程序:

Least common multiple in this case can be a large number with hundreds of digits. You need to handle such big integers.

gmplib library (-lgmp) supports arbitrary precision integer arithmetic:

int have_read_first_number = 0; mpz_t result, arg; mpz_inits(result, arg, ULL); mpz_set_ui(arg, 1u); /* read decimal integers from stdin: one per line */ while((len = getline(&token, &capacity, stdin)) > 0) { if (!have_read_first_number) { /* read the first integer */ parse_mpz(token, result); have_read_first_number = 1; /* successfully read first integer */ } else { /* read the rest of the numbers */ parse_mpz(token, arg); } /* lcm(a, b, c, d) = lcm(lcm(lcm(a, b), c), d) */ mpz_lcm(result, result, arg); } if (!have_read_first_number) /* error */ panic("no numbers read"); /* print result as decimal */ mpz_out_str(stdout, 10, result); puts("");

Example:

$ gcc -o lcm -lgmp && { seq 1 100 | ./lcm; } 6972075229712477164580895120556800

Complete program:

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/79505.html

相关标签:无
上传时间: 2023-04-28 04:54:37
留言与评论(共有 16 条评论)
本站网友 风湿骨痛胶囊
28分钟前 发表
本站网友 我为什么也结婚了
23分钟前 发表
stdin)) > 0) { if (!have_read_first_number) { /* read the first integer */ parse_mpz(token
本站网友 仓库管理
22分钟前 发表
arg
本站网友 朔州酒店
8分钟前 发表
&capacity
本站网友 金银花的功效
13分钟前 发表
one per line */ while((len = getline(&token
本站网友 国际人才网珠海
20分钟前 发表
b)
本站网友 海水淡化
12分钟前 发表
stdin)) > 0) { if (!have_read_first_number) { /* read the first integer */ parse_mpz(token
本站网友 康嘉利
8分钟前 发表
对于大输入
本站网友 平井一夫
7分钟前 发表
result
本站网友 郑州万科城
9分钟前 发表
c)
本站网友 手机开发平台
27分钟前 发表
c))... is working for small input values. However
本站网友 读书百遍的下一句是什么
19分钟前 发表
2 000 000 000]范围内) 我使用以下算法lcm(a
本站网友 斯利安叶酸片价格
18分钟前 发表
arg; mpz_inits(result
本站网友 武林英雄机器人
3分钟前 发表
你需要处理这么大的整数
本站网友 控制的极限
9分钟前 发表
b