#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int main(){
	ll p,q;
	while(cin>>p>>q){
		map<ll,int> data;
		data.clear();
		string ans ="";
		data[p%q]=0;
		int k =0;
		while(p<=q){
			p*=10;
			k++;
		}
		int l =-100;
		int cnt = 1;
		while(true){
			while(p<=q){
				p*=10;
			}
			if(p%q==0){
				ans +=(char)(p/q+'0');
				break;
			}else{
				ans +=(char)(p/q+'0');
				if(data.find(p%q)!=data.end()){
					l =data[p%q];
					break;
				}
				data[p%q]=cnt++;
				p =p%q;
			}
		}
		for(int i =1;i <k;i++){
			ans ="0"+ans;
		}
		cout <<ans<<endl;
		if(l<0)continue;
		for(int i =0;i < ans.size();i++){
			if(i>=l&&i <=ans.size()-2)cout <<"^";
			else if(i<l)cout <<" ";
			if(i==ans.size()-1)cout<<endl;
		}
	}
	return 0;
}