LightOJ - 1024 - Eid(唯一分解定理+手写大数乘法)

1024 - Eid

   PDF (English)StatisticsForum
Time Limit: 2 second(s)Memory Limit: 32 MB

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

题意:求n(n<=1000)个数的LCM(最小公倍数,1<=每个数<=1e4)。

思路:由唯一分解定理可知每个数都可以分解成若干质数的乘积,那么这n个数的LCM就是这n个数的每一个数分解的质因数取最高次幂的乘积。

那么答案肯定是要爆long long的,java又给我MLE,那只能手写C++大数乘法了。

代码:

#include<bits/stdc++.h>
#define ll long long
#define rep(i,x,y) for(register int i=(x);i<=(y);i++)
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=3e6+10;
int n,m,k,l,w;
int ans,cnt,tmp[10010];
int c[maxn];
int prime[10010],ws,sta;
int a[10010];
void init()
{
	for(int i=2;i<=10000;i++)
	{
		if(!tmp[i])
		{
			prime[cnt++]=i;
			for(int j=i*2;j<=10000;j+=i)
				tmp[j]=1;
		}
	}
}
void cal(int x)
{
	int ws=3000000;
	while(ws>=sta)
	{
		c[ws]*=x;
		ws--;
	}
	ws=3000000;
	while(ws>=sta)
	{
		c[ws-1]+=c[ws]/10;
		c[ws]%=10;
		ws--;
		if(c[sta-1]) sta--;
	}
	/*
	for(int i=sta;i<=3000000;i++)
		{
			printf("%d",c[i]);
		}
		puts("");
		*/
}
int main()
{
	init();
	int T,cas=1;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		c[3000000]=1;sta=3000000;
		for(int i=0;i<cnt;i++) a[i]=0;
                ans=0;
		rep(i,1,n)
		{
			scanf("%d",&m);
			for(int j=0;j<cnt&&m>=prime[j];j++)
			if(m%prime[j]==0){
				int t=0;
				while(m%prime[j]==0)
				{
					m/=prime[j];
					t++;
					if(a[j]<t)
					{
						cal(prime[j]);
						a[j]++;
						//cout<<prime[j]<<" "<<a[j]<<endl;
					}
				}
				//System.out.println(prime[j]+" "+t);
			}
		}
		printf("Case %d: ",cas++);
		for(int i=sta;i<=3000000;i++)
		{
			printf("%d",c[i]);
			c[i]=0;
		}
		puts("");
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值