Wednesday, February 5, 2014

codechef LEDIV - "little elephant divisors" solution

codechef LEDIV - "little elephant divisors" : http://www.codechef.com/problems/LEDIV
codechef LEDIV - "little elephant divisors" editorial : http://discuss.codechef.com/questions/3926/lediv-editorial

codechef LEDIV - "little elephant divisors" solution :  http://ideone.com/rGuLtW

#include <iostream>
#include <cstdio>
#include <cstring>
#include <math.h>
using namespace std;

int gcd(int u, int v) {
if(u==v) return u;
if(u==0) return v;
if(v==0) return u;
if(~u & 1) {
if(v & 1) return gcd(u>>1, v);
else return gcd(u>>1, v>>1)<<1;
}
if(~v & 1) return gcd(u, v>>1);
if(u>v) return gcd((u-v)>>1, v);
return gcd((v-u)>>1, u);
}

int main() {
/*int m, b[100005];
m=100005;
memset(b, 1, sizeof(b));
b[0]=b[1]=0;
for(int i=2; i<sqrt(m); i++) {
if(!b[i]) continue;
for(int j=i+i; j<m; j+=i) {
if(b[j]) b[j]=0;
}
}*/
int t, n, a[100005], c;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for(int i=0; i<n; i++) scanf("%d", &a[i]);
c=a[0];
for(int i=0; i<n; i++) {
c=gcd(c, a[i]);
if(c==1) break;
}
if(c==1) printf("-1\n");
else {
for(int i=2; i<=sqrt(c); i++) {
if(c%i==0) c=i;
}
printf("%d\n", c);
}
}
return 0;
}
note: i got TLE when i used prime number array(a[])

No comments:

Post a Comment