#include <iostream>
#include <map>
#include <vector>

using namespace std;

const int N = 80;
map<int, int> amari;

int main(){
	int p, q, r, c, i;
	int loop;
	while(cin >> p >> q){
		vector<int> ans;
		loop = 0;
		p *= 10;
		for(i = 0;i < N;++i){
			ans.push_back(int(p / q));
			p = p % q;

			if(amari[p]){
				loop = amari[p];
				break;
			}
			else{
				amari[p] = i;
			}

			if(p == 0) break;

			p *= 10;
		}
		for(int j = 0;j < ans.size();++j){
			cout << ans[j];
		}
		cout << endl;
		if(loop){
			for(int j = 0;j <= loop;++j){
				cout << " ";
			}
			for(int j = loop + 1;j < ans.size();++j){
				cout << "^";
			}
			cout << endl;
		}
		amari.clear();
		ans.clear();
	}
	return 0;
}