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