#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)

int main(){
	int p,q;
	
	while(cin >> p >> q){
		string str = "";
		vector<int> his;
		for(int i=0;i<100;i++){
			p *= 10;
			str += '0' + p / q;
			p %= q;
			if( find(his.begin(),his.end(),p) != his.end() ){
				int pos = find(his.begin(),his.end(),p)-his.begin();
				cout << str << endl;
				rep(i,pos+1)cout << " ";
				rep(i,his.size()-pos)cout << "^";
				cout << endl;
				break;
			}
			his.push_back(p);
			if(p == 0){cout << str << endl;break;}
		}
	}
}