7ea790dbb6 2011-07-09 kinaba: import java.math.*; 7ea790dbb6 2011-07-09 kinaba: import java.util.*; 7ea790dbb6 2011-07-09 kinaba: 7ea790dbb6 2011-07-09 kinaba: public class ZenoDivision { 7ea790dbb6 2011-07-09 kinaba: static BigInteger ZERO = BigInteger.ZERO; 7ea790dbb6 2011-07-09 kinaba: static BigInteger ONE = BigInteger.ONE; 7ea790dbb6 2011-07-09 kinaba: static BigInteger TWO = BigInteger.valueOf(2); 7ea790dbb6 2011-07-09 kinaba: static BigInteger TEN = BigInteger.TEN; 7ea790dbb6 2011-07-09 kinaba: 7ea790dbb6 2011-07-09 kinaba: public String cycle(String a_, String b_) 7ea790dbb6 2011-07-09 kinaba: { 7ea790dbb6 2011-07-09 kinaba: BigInteger a = new BigInteger(a_); 7ea790dbb6 2011-07-09 kinaba: BigInteger b = new BigInteger(b_); 7ea790dbb6 2011-07-09 kinaba: 7ea790dbb6 2011-07-09 kinaba: if( b.remainder(TWO).equals(ZERO) ) 7ea790dbb6 2011-07-09 kinaba: return "impossible"; 7ea790dbb6 2011-07-09 kinaba: if( a.equals(ZERO) && b.equals(ONE) ) 7ea790dbb6 2011-07-09 kinaba: return "-"; 7ea790dbb6 2011-07-09 kinaba: if( a.equals(ONE) && b.equals(ONE) ) 7ea790dbb6 2011-07-09 kinaba: return "*"; 7ea790dbb6 2011-07-09 kinaba: 7ea790dbb6 2011-07-09 kinaba: int x = 1; 7ea790dbb6 2011-07-09 kinaba: while( !TWO.modPow(BigInteger.valueOf(x),b).equals(ONE) ) { 7ea790dbb6 2011-07-09 kinaba: x++; 7ea790dbb6 2011-07-09 kinaba: if( x >= 61 ) 7ea790dbb6 2011-07-09 kinaba: return "impossible"; 7ea790dbb6 2011-07-09 kinaba: } 7ea790dbb6 2011-07-09 kinaba: 7ea790dbb6 2011-07-09 kinaba: BigInteger z = TWO.pow(x).subtract(ONE).divide(b); 7ea790dbb6 2011-07-09 kinaba: String str = a.multiply(z).toString(2); 7ea790dbb6 2011-07-09 kinaba: while( str.length() < x ) 7ea790dbb6 2011-07-09 kinaba: str = "0" + str; 7ea790dbb6 2011-07-09 kinaba: 7ea790dbb6 2011-07-09 kinaba: String answer = ""; 7ea790dbb6 2011-07-09 kinaba: for(int i=0; i<str.length(); ++i) 7ea790dbb6 2011-07-09 kinaba: answer += (str.charAt(i)=='1' ? "*" : "-"); 7ea790dbb6 2011-07-09 kinaba: return answer; 7ea790dbb6 2011-07-09 kinaba: } 7ea790dbb6 2011-07-09 kinaba: };