#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;
		while(1){
			p *= 10;
			str += '0' + p / q;
			p %= q;
			if(p == 0){cout << str << endl;break;}

			if( find(his.begin(),his.end(),p) != his.end() ){
				int pos = find(his.begin(),his.end(),p)-his.begin();
				if(!pos){
					cout << str.substr(0,str.length()-1) << endl;
					rep(i,his.size()-pos)cout << "^";cout << endl;
				}else{
					cout << str << endl;
					rep(i,pos+1)cout << " ";
					rep(i,his.size()-pos)cout << "^";
					cout << endl;
				}
				break;
			}
			his.push_back(p);
		}
	}
}