Light OJ 1024(高精度乘)

本文介绍了一种使用高精度算法求解多个整数最小公倍数的方法。通过质因数分解并利用快速幂运算,该算法可以有效处理大量整数的最小公倍数计算问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目分析

这道题要求n个数的最小公倍数,但是需要进行高精度进行处理。

#include <map>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e6+100;
map <int, int> M;

int ans[maxn], len;

void mul(int x){
    for(int i = 0; i < len; i++) ans[i] *= x;
    for(int i = 0; i < len; i++){
        ans[i+1] += ans[i]/10;
        ans[i] = ans[i]%10;
    }
    int temp = ans[len];
    while(temp){
        ans[len++] = temp%10;
        temp /= 10;
    }
}

int quick_pow(int a, int b){
    int ret = 1;
    while(b){
        if(b&1) ret *= a;
        b >>= 1;
        a = a*a;
    }
    return ret;
}

int main(){
    int T, n, x;
    scanf("%d", &T);
    for(int kase = 1; kase <= T; kase++){
        scanf("%d", &n);
        M.clear();
        memset(ans, 0, sizeof(ans));
        for(int i = 1; i <= n; i++){
            scanf("%d", &x);
            int len = sqrt(0.5 + x);
            for(int j = 2; j <= len; j++) if(x%j == 0){
                int tot = 0;
                while(x%j==0){
                    tot++;
                    x /= j;
                }
                if(tot > M[j]) M[j] = tot;
            }
            if(x != 1 && M[x] == 0) M[x] = 1;
        }
        ans[0] = len = 1;
        for(map <int, int>::iterator it = M.begin(); it != M.end(); it++)
            mul(quick_pow(it->first, it->second));

        printf("Case %d: ", kase);
        for(int i = len-1; i >= 0; i--) printf("%d", ans[i]);
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值