1. 程式人生 > >light oj 1024 - Eid 大整數乘法

light oj 1024 - Eid 大整數乘法

imp borde mean ron [] public ans test case cas

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 大整數乘法