Eid (n个数的LCM + 高精度乘法)

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

2

3

2 20 10

4

5 6 30 60

Sample Output

Case 1: 20

Case 2: 60

题意:求给出的n个数的最小公倍数。

思路:根据唯一分解定理

          gcd = p1 ^min(m1,m2,..,mn) * p2 ^min(m1,m2,..,mn) * ....pk ^min(m1,m2,..,mn)

          lcm =  p1 ^max(m1,m2,..,mn) * p2 ^max(m1,m2,..,mn) * ....pk ^max(m1,m2,..,mn)

所以我们只需把每一个数进行分解,记录最大的幂就行,然后根据素数幂累乘即可,不过数太大了,所以要用高精度(差点吓的用JAVA写)。注意好像这道题每一个是<=10000

代码如下:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
int p=0,book[1000010],pre[100010];
int len,a[10010],b[100010];
void init()
{
    p=0;
    for(int i=2; i<1000010; i++)
        if(!book[i])
        {
            pre[p++]=i;
            for(int j=2*i; j<1000010; j+=i)
                book[j]=1;
        }
}
void work(int x)
{
    for(int i=0; i<p&&pre[i]*pre[i]<=x; i++)
    {
        if(x%pre[i]==0)
        {
            int cnt=0;
            while(x%pre[i]==0)
            {
                cnt++;
                x/=pre[i];
            }
            b[pre[i]]=max(b[pre[i]],cnt);
        }
    }
    if(x!=1)
        b[x]=max(b[x],1);
}
void cal(int n)
{
    int r=0;
    for(int i=0; i<len; i++)
    {
        int x=a[i]*n+r;
        r=x/10;
        a[i]=x%10;
        if(i==len-1&&r)
            len++;
    }
}
int main()
{
    init();
    int t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(int i=0; i<n; i++)
        {
            int x;
            scanf("%d",&x);
            work(x);
        }
        a[0]=1,len=1;
        for(int i=2; i<10010; i++)
            if(b[i])
            {
                int x=1;
                for(int j=0; j<b[i]; j++)
                    x*=i;
                cal(x);
            }
        printf("Case %d: ",cas++);
        for(int i=len-1; i>=0; i--)
        {
            printf("%d",a[i]);
        }
        printf("\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值