#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <numeric>
#include <ctime>
#include <algorithm>
#include <set>
#include <stdio.h>
#include <map>

using namespace std;
typedef long long int lint;

int GCD(int a, int b)
{
	int na, nb, buf;
	na = a;
	nb = b;
	while (a % b != 0){
		a = a % b;
		buf = a;
		a = b;
		b = buf;
	}
	return b;
}

class pairInt {
public:
	int rem; //余り
	int num; //何桁目に出現したか
	int q;

	pairInt(int r, int n, int x){
		rem = r;
		num = n;
		q = x;
	}
};

void solve(int p, int q)
{
	int g = GCD(p, q);
	p /= g; q /= g;

	set<int> s;
	vector<pairInt> vec;
	int begin, end;	//循環開始と終了
	
	begin = end = 0;
	int digit = 0;
	int x = p / q;
	int r = p - q * x;
	while (1){
		digit++;
		p = r * 10;
		x = p / q;
		r = p - q * x;
		if (binary_search(s.begin(), s.end(), r)){
			end = digit;
			for (int i = 0; i < vec.size(); i++){
				if (vec[i].rem == r){
					begin = vec[i].num;
					break;
				}
			}
			vec.push_back(pairInt(r, digit, x));
			break;
		}
		s.insert(r);
		vec.push_back(pairInt(r, digit, x));
		if (r == 0) break;
	}
	for (int i = 0; i < vec.size(); i++){
		cout << vec[i].q;
	}
	cout << endl;
	if (begin != 0){
		for (int i = 0; i < vec.size(); i++){
			if (begin <= i && i <= end){
				cout << '^';
			}
			else cout << ' ';
		}
		cout << endl;
	}
}

int main()
{
	int p, q;
	while (cin >> p >> q){
		solve(p, q);
	}

	return 0;
}