ADDED SRM/TCO8-3-U/1A.java Index: SRM/TCO8-3-U/1A.java ================================================================== --- SRM/TCO8-3-U/1A.java +++ SRM/TCO8-3-U/1A.java @@ -0,0 +1,39 @@ +import java.math.*; +import java.util.*; + +public class ZenoDivision { + static BigInteger ZERO = BigInteger.ZERO; + static BigInteger ONE = BigInteger.ONE; + static BigInteger TWO = BigInteger.valueOf(2); + static BigInteger TEN = BigInteger.TEN; + + public String cycle(String a_, String b_) + { + BigInteger a = new BigInteger(a_); + BigInteger b = new BigInteger(b_); + + if( b.remainder(TWO).equals(ZERO) ) + return "impossible"; + if( a.equals(ZERO) && b.equals(ONE) ) + return "-"; + if( a.equals(ONE) && b.equals(ONE) ) + return "*"; + + int x = 1; + while( !TWO.modPow(BigInteger.valueOf(x),b).equals(ONE) ) { + x++; + if( x >= 61 ) + return "impossible"; + } + + BigInteger z = TWO.pow(x).subtract(ONE).divide(b); + String str = a.multiply(z).toString(2); + while( str.length() < x ) + str = "0" + str; + + String answer = ""; + for(int i=0; i= 61 ) - return "impossible"; - } - - BigInteger z = TWO.pow(x).subtract(ONE).divide(b); - String str = a.multiply(z).toString(2); - while( str.length() < x ) - str = "0" + str; - - String answer = ""; - for(int i=0; i