#include <iostream>

using namespace std;
bool flag;
int p,q,j;
int r[10000];
int x[10000];
int main(){
	while(cin >> p){
		cin >> q;
		int i=1;
		flag=false;
		x[0]=0;
		r[0]=p;
		while(1){
			p*=10;
			x[i]=p/q;
			r[i]=p%q;
			p=r[i];
			for(j=0;j<i;j++){
				if(r[i]==r[j]||r[i]==0){
					if(r[i]==0)flag=true;
					goto FIN;
				}
			}
			i++;
		}
		FIN:
		for(int k=1;k<i+1;k++){
			cout << x[k];
		}
		cout << endl;
		while(!flag){
			for(int k=1;k<i+1;k++){
				if(k>j)cout << "^";
				if(k<=j)cout << " ";
			}
			cout << endl;
			flag=true;
		}
	}
	return 0;
}