码迷,mamicode.com
首页 > 其他好文 > 详细

light oj 1024 - Eid 大整数乘法

时间:2018-01-03 17:33:17      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:none   因子   乘法   and   next   inpu   cas   分析   let   

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

标签:none   因子   乘法   and   next   inpu   cas   分析   let   

原文地址:https://www.cnblogs.com/zhulei2/p/8184398.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!