Last active
September 24, 2015 03:09
-
-
Save sourcedelica/6faa9d27b97da48ac118 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.hackerearth.com/submission/34813/ | |
import java.io.*; | |
import java.util.*; | |
import java.lang.Math.*; | |
class ques3 | |
{ | |
public static void main(String args[])throws Exception | |
{ | |
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); | |
PrintWriter pw = new PrintWriter(System.out); | |
StringTokenizer st; | |
int i,j,k,l; | |
/*storing the prime numbers till 100000(the upper limit of the time limits)*/ | |
int prime[]=new int[10001]; | |
Arrays.fill(prime,1); | |
prime[0]=prime[1]=0; | |
for(i=2;i<100;i++) | |
{ | |
if(prime[i]!=0) | |
{ | |
for(j=i*i;j<10001;j+=i) | |
{ | |
prime[j]=0; | |
} | |
} | |
} | |
int ctr=0; | |
for(i=1;i<=10000;i++) | |
{ | |
if(prime[i]!=0) | |
ctr++; | |
} | |
int fact[]=new int[ctr+1]; | |
int occ[]=new int[ctr+1]; // BUG: this should be moved to after while (t>0) | |
k=1; | |
for(i=1;i<=10000;i++) | |
{ | |
if(prime[i]!=0) | |
{ | |
fact[k++]=i; | |
prime[i]=k-1; | |
} | |
} | |
/*the main user interaction starts here*/ | |
int t=Integer.parseInt(br.readLine()); | |
while(t>0) | |
{ | |
t--; | |
int n=Integer.parseInt(br.readLine()); | |
int a[]=new int[n]; | |
st=new StringTokenizer(br.readLine()); | |
for(i=0;i<n;i++) | |
{ | |
a[i]=Integer.parseInt(st.nextToken()); | |
} | |
for(i=0;i<n;i++) | |
{ | |
for(j=1;j<=ctr;j++) | |
{ | |
if(prime[a[i]]!=0) | |
{ | |
occ[prime[a[i]]]=Math.max(occ[prime[a[i]]],1); | |
a[i]=1; | |
break; | |
} | |
else if(a[i]!=1) | |
{ | |
int count=0; | |
while(a[i]%fact[j]==0) | |
{ | |
count++; | |
a[i]/=fact[j]; | |
} | |
occ[j]=Math.max(occ[j],count); | |
} | |
else | |
{ | |
break; | |
} | |
} | |
} | |
/*for(i=0;i<10;i++) | |
{ | |
pw.println(fact[i]+" "+occ[i]); | |
}*/ | |
long prod=1; | |
for(i=1;i<ctr+1;i++) | |
{ | |
if(occ[i]!=0) | |
prod=(prod*pow(fact[i],occ[i],1000000007))%1000000007; | |
} | |
pw.println(prod); | |
} | |
pw.flush(); | |
} | |
/* public static int gcd(int a,int b) | |
{ | |
if(a==0)return b; | |
else | |
{ | |
return gcd(a%b,a); | |
} | |
}*/ | |
public static int pow(int a, int b, int MOD) | |
{ | |
long x = 1, y = a; | |
while(b > 0) | |
{ | |
if(b%2 == 1) | |
{ | |
x=(x*y); | |
if(x>MOD) | |
x%=MOD; | |
} | |
y = (y*y); | |
if(y>MOD) | |
y%=MOD; | |
b /= 2; | |
} | |
return (int)x%(1000000007); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment