23dfcca431 2011-02-23 kinaba: ############################################################################# 23dfcca431 2011-02-23 kinaba: # Written in 23dfcca431 2011-02-23 kinaba: # Blue 1.7.5 23dfcca431 2011-02-23 kinaba: # http://www.lechak.info/blue/ 23dfcca431 2011-02-23 kinaba: ############################################################################# 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: global FROM_STRING = func { 23dfcca431 2011-02-23 kinaba: arg s; 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: a = []; 23dfcca431 2011-02-23 kinaba: i = 0; 23dfcca431 2011-02-23 kinaba: loop { 23dfcca431 2011-02-23 kinaba: a = a.append( s.substr(i,i+1).num() ); 23dfcca431 2011-02-23 kinaba: (i=i+1) >= s.length() ? return; 23dfcca431 2011-02-23 kinaba: }; 23dfcca431 2011-02-23 kinaba: return a; 23dfcca431 2011-02-23 kinaba: }; 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: global TO_STRING = func { 23dfcca431 2011-02-23 kinaba: arg x; 23dfcca431 2011-02-23 kinaba: lexical s = ""; 23dfcca431 2011-02-23 kinaba: x.map( func{ s = s + this; } ); 23dfcca431 2011-02-23 kinaba: return s; 23dfcca431 2011-02-23 kinaba: }; 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: global LESS = func { 23dfcca431 2011-02-23 kinaba: arg x; 23dfcca431 2011-02-23 kinaba: arg y; 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: xl = x.length(); 23dfcca431 2011-02-23 kinaba: yl = y.length(); 23dfcca431 2011-02-23 kinaba: (xl < yl) ? return 1; 23dfcca431 2011-02-23 kinaba: (xl > yl) ? return 0; 23dfcca431 2011-02-23 kinaba: i = 0; 23dfcca431 2011-02-23 kinaba: return loop { 23dfcca431 2011-02-23 kinaba: (i == xl) ? return 0; 23dfcca431 2011-02-23 kinaba: (x[i] < y[i]) ? return 1; 23dfcca431 2011-02-23 kinaba: (x[i] > y[i]) ? return 0; 23dfcca431 2011-02-23 kinaba: i = i+1; 23dfcca431 2011-02-23 kinaba: }; 23dfcca431 2011-02-23 kinaba: }; 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: global SUB = func { 23dfcca431 2011-02-23 kinaba: arg x; 23dfcca431 2011-02-23 kinaba: arg y; 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: x.length() > y.length() ? (y = [].resize(x.length()-y.length(),0).merge(y)); 23dfcca431 2011-02-23 kinaba: z = [].resize(x.length(), 0); 23dfcca431 2011-02-23 kinaba: i = x.length()-1; 23dfcca431 2011-02-23 kinaba: carry = 0; 23dfcca431 2011-02-23 kinaba: loop { 23dfcca431 2011-02-23 kinaba: z[i] = x[i] - y[i] - carry; 23dfcca431 2011-02-23 kinaba: carry = z[i] < 0 ? (z[i]=z[i]+10; 1) : 0; 23dfcca431 2011-02-23 kinaba: (i=i-1) < 0 ? return; 23dfcca431 2011-02-23 kinaba: }; 23dfcca431 2011-02-23 kinaba: head = 0; 23dfcca431 2011-02-23 kinaba: loop { 23dfcca431 2011-02-23 kinaba: head == z.length()-1 ? return; 23dfcca431 2011-02-23 kinaba: z[head] != 0 ? return; 23dfcca431 2011-02-23 kinaba: head = head + 1; 23dfcca431 2011-02-23 kinaba: }; 23dfcca431 2011-02-23 kinaba: return z.slice(head, z.length()); 23dfcca431 2011-02-23 kinaba: }; 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: global DBL = func { 23dfcca431 2011-02-23 kinaba: arg x; 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: z = [].resize(x.length(), 0); 23dfcca431 2011-02-23 kinaba: i = x.length()-1; 23dfcca431 2011-02-23 kinaba: carry = 0; 23dfcca431 2011-02-23 kinaba: loop { 23dfcca431 2011-02-23 kinaba: z[i] = x[i] + x[i] + carry; 23dfcca431 2011-02-23 kinaba: carry = z[i] > 9 ? (z[i]=z[i]-10; 1) : 0; 23dfcca431 2011-02-23 kinaba: ((i=i-1) < 0) ? return; 23dfcca431 2011-02-23 kinaba: }; 23dfcca431 2011-02-23 kinaba: return (carry==1 ? [1].merge(z) : z); 23dfcca431 2011-02-23 kinaba: }; 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: global DIF = func { 23dfcca431 2011-02-23 kinaba: arg x; 23dfcca431 2011-02-23 kinaba: arg y; 23dfcca431 2011-02-23 kinaba: return LESS(x, y) ? SUB(y, x) : SUB(x, y); 23dfcca431 2011-02-23 kinaba: }; 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: global MOD = func { 23dfcca431 2011-02-23 kinaba: arg x; 23dfcca431 2011-02-23 kinaba: arg y; 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: LESS(x,y) ? return x; 23dfcca431 2011-02-23 kinaba: z = MOD(x, DBL(y)); 23dfcca431 2011-02-23 kinaba: LESS(z,y) ? return z; 23dfcca431 2011-02-23 kinaba: return SUB(z,y); 23dfcca431 2011-02-23 kinaba: }; 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: global ISZERO = func { 23dfcca431 2011-02-23 kinaba: arg x; 23dfcca431 2011-02-23 kinaba: return x[0] == 0; 23dfcca431 2011-02-23 kinaba: }; 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: global GCD = func { 23dfcca431 2011-02-23 kinaba: arg x; 23dfcca431 2011-02-23 kinaba: arg y; 23dfcca431 2011-02-23 kinaba: return ISZERO(x) ? y : GCD(MOD(y,x),x); 23dfcca431 2011-02-23 kinaba: }; 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: ############################################################################# 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: solve = func { 23dfcca431 2011-02-23 kinaba: arg N; 23dfcca431 2011-02-23 kinaba: arg t; 23dfcca431 2011-02-23 kinaba: t = t.map( func{return FROM_STRING(this);} ); 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: lexical t0 = t[0]; 23dfcca431 2011-02-23 kinaba: lexical g = DIF(t0, t[1]); 23dfcca431 2011-02-23 kinaba: t.slice(2, t.length()).map(func{ 23dfcca431 2011-02-23 kinaba: g = GCD(g, DIF(t0, this)); 23dfcca431 2011-02-23 kinaba: }); 23dfcca431 2011-02-23 kinaba: r = MOD(t[0], g); 23dfcca431 2011-02-23 kinaba: return TO_STRING( ISZERO(r) ? r : DIF(g,r) ); 23dfcca431 2011-02-23 kinaba: }; 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: input = sys.library("streams").stdio().read(1000000000).split("\n").map(func{return this.split(" ");}); 23dfcca431 2011-02-23 kinaba: C = input[0][0].num(); 23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: CaseID = (args.length() >= 2 ? args[1].num() : 1); 23dfcca431 2011-02-23 kinaba: loop { 23dfcca431 2011-02-23 kinaba: "Case #".print(); 23dfcca431 2011-02-23 kinaba: CaseID.print(); 23dfcca431 2011-02-23 kinaba: ": ".print(); 23dfcca431 2011-02-23 kinaba: solve( input[CaseID][0].num(), input[CaseID].slice(1,input[CaseID].length()) ).print(); 23dfcca431 2011-02-23 kinaba: "\n".print(); 23dfcca431 2011-02-23 kinaba: ((CaseID = CaseID+1) > C) ? return; 23dfcca431 2011-02-23 kinaba: };