light oj 1024 - Eid 大整数乘法

Posted 风子磊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了light oj 1024 - Eid 大整数乘法相关的知识,希望对你有一定的参考价值。

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

 

题意分析:求出所有数的最小公倍数,转化为求出每一个数包含的素因子,多个数某一素因子相同取包含这一素因子最多的那一个,最后累乘起来

技术分享图片
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define LL long long
using namespace std;
bool book[10005];
int pri[2101], s=0, ss;
int sum[10005], ans[2001], b[1005];
void inin()
{
    memset(book, true, sizeof(book));
    for(int i=2; i<=10000; i++)
    {
        if(book[i]){pri[s++]=i;
        for(int j=i+i; j<=10000; j+=i)
            book[j]=false;
        }
    }
}
int multiply()
{
    int p=0;
    ans[p]=1;
    for(int i=0; i<ss; i++)
    {
        int q=0;
        for(int j=0; j<=p; j++)
        {
            int qq=ans[j]*b[i]+q;
            q=qq/10000;
            ans[j]=qq%10000;//每四位存一下,节省空间节省时间
        }
        if(q>0)
            ans[++p]=q;
    }
    return p;
}
int main()
{
    int T, t=1, a;
    inin();
    scanf("%d", &T);
    while(T--)
    {
        ss=0;
        int n;
        scanf("%d", &n);
        memset(sum, 0, sizeof(sum));
        for(int i=0; i<n; i++)
        {
            scanf("%d", &a);
            for(int j=0; j<s; j++)
            {
                int q=0;
                while(a%pri[j]==0)
                {
                    q++;
                    a/=pri[j];
                }
                sum[pri[j]]=max(q,sum[pri[j]]);
            }
        }
        for(int i=0; i<s; i++)
        {
            int m=1;
            for(int k=0; k<sum[pri[i]]; k++)
                m*=pri[i];
            if(m>1)b[ss++]=m;
        }
        int mm=multiply();
        printf("Case %d: ", t++);
        printf("%d", ans[mm]);
        for(int i=mm-1; i>=0; i--)printf("%04d", ans[i]);
        printf("\n");
    }
    return 0;
}
View Code

还可以用java大整数搞一搞,经济实惠方便

技术分享图片
    import java.util.*;
    import java.io.BufferedInputStream;
    import java.math.*;
    public class Main {
        public static void main(String[] args) {
            Scanner in=new Scanner(System.in);
            int T=in.nextInt();
            int t=1;
            while(T-->0)
            {
                BigInteger ans=BigInteger.ONE;
                int n=in.nextInt();
                while(n-->0)
                {
                    BigInteger a=in.nextBigInteger();
                    ans=ans.multiply(a).divide(ans.gcd(a));
                }
                System.out.print("Case"+" "+(t++)+":"+" ");
                System.out.println(ans);
                System.gc();
            }
            in.close();
        }
    }
View Code

 

以上是关于light oj 1024 - Eid 大整数乘法的主要内容,如果未能解决你的问题,请参考以下文章

Light OJ 1114 Easily Readable 字典树

大整数乘法

LIGHT OJ 1370 欧拉函数(在小于自身的正整数中有n个与自身互素的数,这样的数最小的为n后第一个素数)

Light oj 1125 - Divisible Group Sums (dp)

Lightoj 1024 - Eid

Light oj 1197 - Help Hanzo (素数筛技巧)