lightoj 1024

来源:互联网 发布:性快感 知乎 编辑:程序博客网 时间:2024/06/02 02:34
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

 


PROBLEM SETTER: JANE ALAM JAN

题意:给你n个数,求这n个数的最小公倍数。

这道题按照一般思路,两两求最后累乘的话,会爆精度。这里可以采用求n个数的最小公倍数的另一种求法:

将每个数的质因数都分解出来,相同的质因数取出现次数最多的,然后累乘这些质因数的最高次幂。
例如:

5的质因数为: 5
6的质因数为:2 3
60的质因数为:2个2,1个3,1个5
90的质因数为:1个2,2个3,1个5.

对于2来说,最高次幂为2(60的时候出现了2次),对于3来说,最高次幂为2(90的时候出现了2次),对于5来说,最高次幂为1

因此,5、6、60、90的最小公倍数为2*2*3*3*5=180

我们可以将所有质因数的最高次幂存在一个数组里,然后累乘这些数组里的数就行了。

#include<bits/stdc++.h>#define N 10000+10#define MOD 10000using namespace std;typedef long long ll;int a[N],ans[N];int fun(int x,int n){    ll rev=1;    for(int i=1;i<=n;i++)        rev*=x;    return rev;}void multi(int n){    for(int i=0;i<1000;i++)        ans[i]*=n;    for(int i=0;i<1000;i++)    {        ans[i+1]+=ans[i]/MOD;        ans[i]=ans[i]%MOD;    }}void print(){    int i=1000;    while(i>=0&&ans[i]==0)        i--;    printf("%d",ans[i]);    i--;    while(i>=0)        printf("%04d",ans[i--]);    printf("\n");}int main(){    int t,n,num;    cin>>t;    for(int cas=1; cas<=t; cas++)    {        memset(a,0,sizeof(a));        memset(ans,0,sizeof(ans));        scanf("%d",&n);        for(int i=0; i<n; i++)        {            scanf("%d",&num);            int j;            for(j=2; j*j<=num; j++)            {                int cnt=0;                while(num%j==0)                {                    num=num/j;                    cnt++;                }                a[j]=max(a[j],cnt);            }            if(num>1)                a[num]=max(a[num],1);        }        ans[0]=1;        for(int i=1; i<N; i++)        {            if(!a[i])                continue;            int flag=fun(i,a[i]);            multi(flag);        }        printf("Case %d: ",cas);        print();    }    return 0;}



原创粉丝点击