Artifact Content
Not logged in

Artifact 49868c43512ca5f3c70daaf9633fe345ed185123


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<str.length(); ++i)
			answer += (str.charAt(i)=='1' ? "*" : "-");
		return answer;
	}
};