#include <iostream>
#include <iomanip>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>

using namespace std;

typedef pair<int, int> P;

int main() {
	int p, q;
	while (cin >> p >> q) {
		vector<P> v;
		while (p % q) {
			p *= 10;
			P rq = P(p % q, p / q);
			if (find(v.begin(), v.end(), rq) != v.end())
				break;
			v.push_back(rq);
			p %= q;
		}
		int start = find(v.begin(), v.end(), P(p % q, p / q)) - v.begin();
		for (int i = 0; i < v.size(); i++)
			cout << v[i].second;
		cout << endl;
		if (p % q) {
			for (int i = 0; i < v.size(); i++)
				cout << (i >= start ? "^" : " " );
			cout << endl;
		}
	}
	return 0;
}