1024 - Eid
PDF (English) | Statistics | Forum |
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;
}