#include <iostream>
#include <string>
using namespace std;

int main() {
	int p,q,a,b,c,d,i,j,t,*m;
	string s;

	while(cin>>p>>q){
		m=new int[q];
		for(i=0;i<q;++i)m[i]=0;
		s="";
		t=0;	
		a=p/q;
		b=a*q;
		p=(p-b)*10;

		while(1){
			t++;
			d=p%q;
			a=p/q;
			b=a*q;
			p=(p-b)*10;
			s+=(char)(a+'0');
			if(!d){
				cout<<s<<endl;
				break;
			}
			if(m[d]){cout<<s<<endl;s="";for(j=0;j<m[d];++j)s+=" ";for(j=0;j<t-m[d];++j)s+="^";cout<<s<<endl;break;}
			else{m[d]=t;}
		}
		delete[]m;
	}
	return 0;
}