Light OJ 1024(高精度乘)

来源:互联网 发布:java常用api详解 编辑:程序博客网 时间:2024/06/18 17:14

题目分析

这道题要求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;}
0 0