C - Eid LightOJ - 1024

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

2

3

2 20 10

4

5 6 30 60

扫描二维码关注公众号,回复: 187502 查看本文章
Sample Output

Case 1: 20

Case 2: 60

#include <cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef unsigned long long ll;

const int maxn=1010;
int val[maxn];

int pri[maxn*10],num[maxn*10];
int cnt=0;
int ans[maxn*10];
/*
超时不一定是我们的结果正确,只是我们的计算已经超过了时间,自然就是错的

本来挺好的一道题,自己做的稀烂,忘记初始化num,结果一直T,不断的优化,还是没找出来,
最近真的很傻逼
*/

void init(){
    for(int i=2;i<=10000;i++){
        int m=(int)sqrt(i),flag=0;
        for(int j=2;j<=m;j++)
            if(i%j==0){
                flag=1;break;
            }
        if(!flag) pri[cnt++]=i;
    }
}
int bit[100],tmp[maxn*10];
void mul(int su,int cnt,int& len){
    memset(tmp,0,(cnt+len)*sizeof(int));
    for(int i=0;i<len;i++){
        for(int j=0;j<cnt;j++){
            tmp[i+j]+=ans[i]*bit[j];
        }
    }
    int car=0;len=len+cnt;
    for(int i=0;i<len;i++){
        ans[i]=(tmp[i]+car)%10;
        car=(tmp[i]+car)/10;
    }
    while(len>1&&ans[len-1]==0)len--;
}


void get_cntt(int& cnt,int base,int n){
    int ans=1;
    for(int i=1;i<=n;i++)ans*=base;
    cnt=0;
    while(ans){
        bit[cnt++]=ans%10;
        ans/=10;
    }
}

int main()
{
    init();
    int T,kase=1;
    scanf("%d",&T);
    while(T--){
        int len=1;
        memset(ans,0,sizeof(ans));
        memset(num,0,sizeof(num));
        ans[0]=1;

        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&val[i]);
        int max_=1;
        for(int i=1;i<=n;i++){
            for(int j=0;j<cnt;j++){
                 max_=max(max_,j);
                 int tmp=0;
                 while(val[i]%pri[j]==0){
                     tmp++;
                     val[i]/=pri[j];
                 }
                 if(tmp>num[j])num[j]=tmp;
                 if(val[i]==1) break;//最后是1,不是0,傻逼了
            }
        }
        printf("Case %d: ",kase++);
        for(int i=0;i<=max_;i++){
            if(!num[i])continue;
            int cntt;
            get_cntt(cntt,pri[i],num[i]);
            mul(pri[i],cntt,len);
        }
        for(int i=len-1;i>=0;i--) printf("%d",ans[i]);
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/80233089