editorial: http://discuss.codechef.com/questions/10494/amsgame1-editorial
codechef AMSGAME1 - "subtraction game 1" solution: http://ideone.com/JndiG1
#include <iostream>
#include <cstdio>
using namespace std;
unsigned int gcd(unsigned int u, unsigned 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 t, n, c[1005], a, b;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
if(n==1) { scanf("%d", &a); printf("%d\n", a); }
if(n==2) { scanf("%d %d", &a, &b); printf("%d\n", gcd(a, b)); }
if(n>2) {
for(int i=0; i<2; i++) {
scanf("%d %d", &a, &b); a=gcd(a, b); break;
}
for(int i=2; i<n; i++) {
scanf("%d", &b); a=gcd(a, b);
}
printf("%d\n", a);
}
}
return 0;
}
note: i used binary gcd here.
No comments:
Post a Comment