LightOJ 1024 - Eid (高精度求n个数的LCM)

1024 - Eid
Time Limit: 2 second(s)Memory Limit: 32 MB

In a strange planet there are n races. They arecompletely different as well as their food habits. Each race has a food-eatingperiod. That means the ith race eats after every xide-sec (de-sec is the unit they use for counting time and it is used forboth singular and plural). And at that particular de-sec they pass the wholeday eating.

The planet declared the de-sec as 'Eid' in which all theraces eat together.

Now given the eating period for every race you have to findthe 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 linewill denote the eating period for the ith race. Theseintegers will be between 1 and 10000.

Output

For each case of input you should print a line containingthe case number and the number of de-sec between two consecutive Eids. Checkthe sample input and output for more details. The result can be big. So, usebig integer calculations.

Sample Input

Output for Sample Input

2

3

2 20 10

4

5 6 30 60

Case 1: 20

Case 2: 60

 



题意:求n个数的LCM


思路:分解每个数耳朵素因子,每种素因子去出现次数最多的那一次,然后累乘就好了,不过要用到高精度



ac代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 11000
#define LL long long
#define ll __int64
#define INF 0x7fffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
int ans[MAXN];
int cnt[MAXN];
int len;
void debug()
{
	for(int i=10;i>=0;i--)
	printf("%d",ans[i]);
	printf("\n");
}
void mul(int x)
{
	int c=0;
	for(int i=0;i<len;i++)
	{
		int k=ans[i]*x+c;
		c=k/10;
		ans[i]=k%10;
		if(i==len-1&&c)
		{
			//ans[len]=c;
			len++;
		}
	}
	//debug();
}
int main()
{
    int t,n,i,j,a;
    int cas=0;
    scanf("%d",&t);
    while(t--)
    {
    	mem(cnt);mem(ans);
    	scanf("%d",&n);
    	for(i=0;i<n;i++)
    	{
    		scanf("%d",&a);
    		int x=a;
    		for(j=2;j<=a;j++)
    		{
    			int ccnt=0;
    			if(x%j==0)
    			{
    				while(x%j==0)
    				{
    					ccnt++;
    					x/=j;
					}
				}
				cnt[j]=max(cnt[j],ccnt);
				if(x==1)
				break;
			}
		}
		ans[0]=1;len=1;
		for(i=2;i<10010;i++)
		{
			if(cnt[i])
			{
				//printf("%d %d\n",i,cnt[i]);
				int num=(int)pow(i,cnt[i]);
				//printf("num=%d\n",num);
				mul(num);
			}
		}
		printf("Case %d: ",++cas);
		for(j=len-1;j>=0;j--)
		printf("%d",ans[j]);
		printf("\n");
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值