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 |
#include<bits/stdc++.h>
#define N 10000+10
#define MOD 10000
using 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;
}