#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int p,q;
	int a[80],n;
	int *s,*t;
	while(cin>>p>>q){
		n=0;
		while(p){
			p=(10*p);
			cout<<p/q;
			p%=q;
			t=find(a,a+n,p);
			if(t!=a+n){
				cout<<endl;
				for(s=a;s<=t;s++)cout<<' ';
				for(;s<=a+n;s++)cout<<'^';
				cout<<endl;
				break;
			}
			a[n++]=p;
		}
	}
}