#include <iostream>
#include <vector>

using namespace std;
typedef pair<int, int> P;

void solve()
{
	int p, q;
	while(cin >> p)
	{
		cin >> q;
		vector<P> Vec;
		int pos = 0;
		p *= 10;
		bool circulation_flag = true;
		while(true)
		{
			if(p % q == 0)
			{
				circulation_flag = false;
				Vec.push_back(P(p / q, p % q));
				break;
			}
			else
			{
				P Pair(p / q, p % q);
				int n = Vec.size();
				for(int i = 0; i < n; ++i)
				{
					if(Vec[i].first == Pair.first && Vec[i].second == Pair.second)
					{
						pos = i;
						goto NEXT;
					}
				}
				Vec.push_back(Pair);
				p = p - p / q * q;
				p *= 10;
			}
		}
NEXT:
		if(circulation_flag)
		{
			int n = Vec.size();
			for(int i = 0; i < n; ++i)
			{
				cout << Vec[i].first;
			}
			cout << endl;
			for(int i = 0; i < pos; ++i)
			{
				cout << " ";
			}
			for(int i = pos; i < n; ++i)
			{
				cout << "^";
			}
			cout << endl;
		}
		else
		{
			int n = Vec.size();
			for(int i = 0; i < n; ++i)
			{
				cout << Vec[i].first;
			}
			cout << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}