Eid LightOJ - 1024 (枚举质因子+高精度)

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个数,让你求出它们的最小公倍数

思路:将每个数分解成若干个质因数的乘积比如10可以分解成2^1*5^1,20分解成2^2*5^1,类似这样,然后我们对出现的相同的质因数取它们最大的次方,然后将所有的质因数相乘即为它们的最小公倍数。
注意:最后结果会很大,需要用到大数板子

代码如下

#include<bits/stdc++.h>
using namespace std;
#define LL long long 
#define ULL unsigned long long 

int cnt[1500];//cnt[i]保存素数为prim[i]对应的最大次方
int prim[1500] = {2,3,5};
//素数是分为基本素数{2,3}、阳素数{6N+1,N>=1}形式的、阴素数{6N-1,N>=1}形式的
//为了代码的好写,在这里这样写的 :
//数除了{2,3,5}为素数,其他的数可以写成6N,6N+1,6N+2,6N+3,6N+4,6N+5  N>=1 可以表示全部的数
//6N,6N+2,6N+4都为偶数,不是素数,6N+3 == 3(2N+1) 不是素数,那么就只筛6N+1和6N+5就可以了
void get_prim()//预处理出小于等于10007的所有素数
{
    int i, j, flag;
    int gcd = 2;
    int k = 3;

    for(i=7;i<=10007;i+=gcd)
    {
        gcd = 6-gcd;//6N+1和6N+5的变换。
        flag = 1;
        for(j=0;prim[j]*prim[j]<=i;j++)//判断一个数是否为素数,只需判断这个数在2--sqrt(x)范围即可。
            if(i%prim[j] == 0){//因为一个开根号比乘法是要慢的,所以用乘法速度更快。
                flag = 0;
                break;
            }
        if(flag)//若这个数没有被其他素数整除 说明为素数。
            prim[k++] = i;
    }
}

int ans[1001];
void Mul(int a[], int num)
{
    for(int i=0; i<1000; i++)
        a[i] = a[i]*num;
    for(int i=0; i<1000; i++)
    {
        a[i+1] += a[i]/10000;
        a[i] = a[i]%10000;
    }
}

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

int main(void)
{
    get_prim();
    int T;
    scanf("%d",&T);

    for(int cas=1;cas<=T;cas++)
    {
        memset(cnt,0,sizeof(cnt));
        memset(ans,0,sizeof(ans));
        int n,x;
        scanf("%d",&n);

        for(int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            int k=0;
            while(x>1)
            {
                int num=0;
                while(x%prim[k]==0)
                {
                    num++;
                    x/=prim[k];
                }
                if(cnt[k]<num) cnt[k]=num;
                k++;
            }
        }
        ans[0] = 1;

        for(int i=0;i<1229;i++)//第1229个素数的值为10007大于n的取值范围,所以从这里开始就不用再考虑后面的素数了
        {
            if(cnt[i]>0)
            {
                int ret=1;
                while(cnt[i]--)
                {
                    ret*=prim[i];
                }
                 Mul(ans, ret);
            }
        }

        printf("Case %d: ",cas);
        PUTS(ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值