#include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int p,q,d[80],n;

int main(){
	while(cin>>p>>q){
		map<int,int> r;
		map<int,int>::iterator ite;
		p%=q;
		r[p]=-1;
		int n=0,s,f=0;
		while(1){
			d[n]=p*10/q;
			if(!((p*10)%q)){f=1;break;}
			ite=r.find((p*10)%q);
			if(ite!=r.end()){s=r[(p*10)%q];break;}
			r[(p*10)%q]=n++;
			p=(p*10)%q;
		}
		if(f){
			for(int i=0;i<=n;i++)cout<<d[i];cout<<endl;
		}
		else {
			for(int i=0;i<=n;i++)cout<<d[i];cout<<endl;
			for(int i=0;i<=n;i++){
				if(i<=s)cout<<" ";
				else cout<<"^";
			}
			cout<<endl;
		}
	}
}