import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;


string[] calc(int p, int q, RedBlackTree!int s) {
	auto r = p%q;
	if(r == 0){
		s.clear();
		return [to!string(p/q), ""];
	}
	if(r in s) {
		s.clear();
		s.insert(r);
		return [to!string(p/q), "^"];
	}
	s.insert(r);
	auto ret = calc(r*10, q, s);

	ret[0] = to!string(p/q) ~ ret[0];
	if(r in s)
		s.clear();
	if(s.empty())
		ret[1] = " " ~ ret[1];
	else
		ret[1] = "^" ~ ret[1];
	return ret;
}

void main() {
	auto s = readln().strip().split();
	while(!stdin.eof()) {
		int p = to!int(s[0]);
		int q = to!int(s[1]);
		auto ans = calc(p*10, q, redBlackTree!int());
		writeln(ans[0]);
		if(ans[1].back == '^')
			writeln(ans[1]);

		s = readln().strip().split();
	}
}