lightoj 1024 Eid

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

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 xide-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 nintegers 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


题意:输入一堆数,求这堆数的最小公倍数。

思路: 若  p= a1^b1 * a2^ b2 * ... * an^bn ,  q= a1^c1 * a2^ c2 * ... * an^cn 

      则   p和q的最小公倍数  lcm(p,q) =  p= a1^max(b1,c1) * a2^ max(b2,c2) * ... * an^max(bn,cn); 

 根据上述思路,将每个数因式分解,然后记录各个数的因子,因子数保存最大值。最后将所有因子乘起来就是最小公倍数 ,由于结果很大,所以用到了高精度。



#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn=10010;

int num[maxn],ans[maxn],m,n,T,t;
int pri[30]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101};

void initial()
{
    memset(num,0,sizeof(num));
    memset(ans,0,sizeof(ans));
    ans[0]=m=1;
}

void mul(int num)
{
    int tmp=0,now;
    for(int i=0;i<m;i++)  ans[i]*=num;
    for(int i=0;i<m;i++)
    {
         now=ans[i]+tmp;
         tmp=now/10;
         ans[i]=now%10;
    }
    while(tmp)
    {
        ans[m]=tmp%10;
        tmp/=10;
        m++;
    }
}

int main()
{
    scanf("%d",&T);
    for(int co=1;co<=T;co++)
    {
        initial();
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
             scanf("%d",&t);
             int w=t,cnt=0;
             for(int j=0;pri[j]*pri[j]<=t && j<25;j++)
             {
                   while(w%pri[j]==0)
                   {
                       cnt++;
                       w=w/pri[j];
                   }
                   if(cnt)
                   {
                       num[pri[j]]=max(cnt, num[pri[j]]);
                       cnt=0;
                   }
             }
             if(w>1)  num[w]=max(num[w],1);
        }
        for(int i=2;i<maxn;i++)
        {
            if(num[i]==0)  continue;
            int pt=(int)pow(i,num[i]);
            mul(pt);
        }
        printf("Case %d: ",co);
        for(int i=m-1;i>=0;i--)  printf("%d",ans[i]);
        printf("\n");
    }
    return 0;
}


尝试用java也写了下,不过MLE了,第一次写java大数就这样,哎。。。


import java.io.BufferedInputStream;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		
		BigInteger ans = null;
		int nCase,n,w;
		Scanner cin = new Scanner(System.in);
		
		nCase = cin.nextInt();
		for(int co=1;co<=nCase;co++)
		{
			n = cin.nextInt();
			for(int i=0;i<n;i++) {
				w  = cin.nextInt();
				if(i==0)  ans=BigInteger.valueOf(w);
				else   ans = ans.multiply(BigInteger.valueOf(w)).divide(ans.gcd(BigInteger.valueOf(w)));
			}
			System.out.println("Case "+co+": "+ans);
			ans = null;
		}
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值