#include <bits/stdc++.h>

using namespace std;

int p, q;
map<int, int> RestExist, LastPos;
string AnsNumber;

bool solve() {
	int rest;

	RestExist[p%q] = 0;
	p *= 10;
	for (int i = 1; i < 82; i++) {
		rest = p%q;
		AnsNumber += p / q + '0';
		//cout << "i=" << i << "   Ans=" << AnsNumber << "   rest=" << rest << endl;
		if (rest == 0) {
			return false;
		}

		if (RestExist.find(rest) != RestExist.end()) {
			LastPos[rest] = i;
			//LastPos.insert(pair<int, int>(rest, i));
			return true;
		}
		RestExist[rest] = i;
		//RestExist.insert(pair<int, int>(rest, i));
		p = rest * 10;
	}
	return false;
}

int main() {
	int  startPos, endPos, index;

	while (cin >> p >> q) {
		AnsNumber = "";
		RestExist.clear();
		LastPos.clear();
		if (solve()) {
			cout << AnsNumber << endl;
			startPos = RestExist[LastPos.begin()->first];
			endPos = LastPos.begin()->second;
			for (index = 0; index < startPos; index++) {
				cout << " ";
			}
			for (; index < endPos; index++) {
				cout << "^";
			}
			cout << endl;
		}
		else {
			cout << AnsNumber << endl;
		}

	}

	return 0;
}
