lightoj 1024 - Eid (高精度乘法+n个数的最小公倍数)

本文介绍了一种求多个整数最小公倍数的有效算法,适用于处理精度较高的大规模数据问题。通过分解质因数并计算最高次幂的方式,避免了传统方法中可能遇到的精度溢出问题。

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

1024 - Eid
Time Limit: 2 second(s)Memory Limit: 32 MB

In a strange planet there are n races. They are completely different as well as their food habits. Each race has a food-eating period. That means the ith race eats after every xi de-sec (de-sec is the unit they use for counting time and it is used for both singular and plural). And at that particular de-sec they pass the whole day eating.

The planet declared the de-sec as 'Eid' in which all the races eat together.

Now given the eating period for every race you have to find the number of de-sec between two consecutive Eids.

Input

Input starts with an integer T (≤ 225), denoting the number of test cases.

Each case of input will contain an integer n (2 ≤ n ≤ 1000) in a single line. The next line will contain n integers separated by spaces. The ith integer of this line will denote the eating period for the ith race. These integers will be between 1 and 10000.

Output

For each case of input you should print a line containing the case number and the number of de-sec between two consecutive Eids. Check the sample input and output for more details. The result can be big. So, use big integer calculations.

Sample Input

Output for Sample Input

2

3

2 20 10

4

5 6 30 60

Case 1: 20

Case 2: 60

 


PROBLEM SETTER: JANE ALAM JAN

题意:给你n个数,求这n个数的最小公倍数。

这道题按照一般思路,两两求最后累乘的话,会爆精度。这里可以采用求n个数的最小公倍数的另一种求法:

将每个数的质因数都分解出来,相同的质因数取出现次数最多的,然后累乘这些质因数的最高次幂。
例如:

5的质因数为: 5
6的质因数为:2 3
60的质因数为:2个2,1个3,1个5
90的质因数为:1个2,2个3,1个5.

对于2来说,最高次幂为2(60的时候出现了2次),对于3来说,最高次幂为2(90的时候出现了2次),对于5来说,最高次幂为1

因此,5、6、60、90的最小公倍数为2*2*3*3*5=180

我们可以将所有质因数的最高次幂存在一个数组里,然后累乘这些数组里的数就行了。

#include<bits/stdc++.h>
#define N 10000+10
#define MOD 10000

using namespace std;
typedef long long ll;
int a[N],ans[N];

int fun(int x,int n)
{
    ll rev=1;
    for(int i=1;i<=n;i++)
        rev*=x;
    return rev;
}

void multi(int n)
{
    for(int i=0;i<1000;i++)
        ans[i]*=n;
    for(int i=0;i<1000;i++)
    {
        ans[i+1]+=ans[i]/MOD;
        ans[i]=ans[i]%MOD;
    }
}

void print()
{
    int i=1000;
    while(i>=0&&ans[i]==0)
        i--;
    printf("%d",ans[i]);
    i--;
    while(i>=0)
        printf("%04d",ans[i--]);
    printf("\n");
}

int main()
{
    int t,n,num;
    cin>>t;
    for(int cas=1; cas<=t; cas++)
    {
        memset(a,0,sizeof(a));
        memset(ans,0,sizeof(ans));
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&num);
            int j;
            for(j=2; j*j<=num; j++)
            {
                int cnt=0;
                while(num%j==0)
                {
                    num=num/j;
                    cnt++;
                }
                a[j]=max(a[j],cnt);
            }
            if(num>1)
                a[num]=max(a[num],1);
        }
        ans[0]=1;
        for(int i=1; i<N; i++)
        {
            if(!a[i])
                continue;
            int flag=fun(i,a[i]);
            multi(flag);
        }
        printf("Case %d: ",cas);
        print();
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值