#include <cstdio>
#include <map>
#include <iostream>

using namespace std;

int main()
{
	int p, q;

	while (cin >> p >> q){
		map<int, int> mod;
		int index = 0;
		do {
			p *= 10;
			cout << p / q;
			if (mod.find(p % q) != mod.end()) break;
			mod[p %= q] = index++;
		} while (p);
		cout << endl;

		if (p){
			int blank = mod[p % q];
			for (int i = 0; i < blank + 1; i++){
				cout << ' ';
			}
			for (int i = 0; i < index - blank; i++){
				cout << '^';
			}
			cout << endl;
		}
	}
	return (0);
}