/*****include*****/
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <vector>
/*****デバッグ定義*****/
//#define DEBUG
/*****マクロ定義*****/

/*****名前空間*****/
using namespace std;

/*****グローバル変数置き場*****/

/*****その他関数置き場*****/

/*****main関数*****/
int main(){
	/*****ファイルオープン*****/
#ifdef DEBUG
	ofstream fout("output.txt");
	ifstream fin("input.txt");
	if(!fout || !fin){
		cout << "Can't open the file.\n";
		return 0;
	}
#endif
	/*****変数置き場*****/
	int p,q;
	map<int,int> remain;
	vector<int> answer;
	int counter;
	/*****処理部*****/
	while(1){
		counter = 0;
		answer.clear();
		remain.clear();
		cin >> p >> q;
		if(cin.eof())	break;
		while(p>q)
			p = p%q;
		remain[0] = p;
		while(1){
			p *= 10;
			answer.push_back(p/q);
			p=p%q;
			int i;
			for(i=0;i<counter;i++)
				if(remain[i] == p)	break;
			if(remain[i] == p)	break;
			if(p == 0)	break;
			remain[counter+1] = p;
			counter++;
		}
		for(int j=0;j<=counter;j++){
			cout << answer[j];
		}
		cout << endl;
		if(p!=0){
			for(int i=0;i<=counter;i++)
				if(remain[i] == p){
					if(remain[i+1] == p)	break;
					remain[i+1] = p;
				}
			for(int i=0;i<=counter;i++){
				if(remain[i] == p){
					cout << '^';
				}else
					cout << " ";
			}
			cout << endl;
		}
	}
	/*****処理終了後*****/
#ifdef DEBUG
	fout.close();
	fin.close();
#endif
	return 0;
}