#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <string.h>
#include <cassert>
 
#ifdef _DEBUG
#define typeof(X) std::identity<decltype(X)>::type //C++0x (for vs2010)
#else
#define typeof(X) __typeof__(X) // for gcc
#endif
 
#define sz(a)  int((a).size())
#define FOREACH(it, c) for (typeof((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define FOR(i,count) for (int i = 0; i < (count); i++)
 
using namespace std;
typedef long long ll;
const int MODULO = 1000000007;
 
typedef pair<int,int> Pii;
// begin
 
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

int main()
{
	int p,q;
	while(cin>>p>>q){
		map<int,int> mp;
		int circle_pt = -1;
		int cnt = 0;
		int div;
		while(true){
			p *= 10;
			div = p/q;
			p -= q*div;
			if(p == 0){
				putchar(div+'0');
				break;
			} else if(mp.find(p) != mp.end()){
				circle_pt = mp[p];
				break;
			}
			mp[p] = cnt++;
			putchar(div+'0');
		}
		int n = sz(mp);
		if(p != 0){
			if(gcd(10,q) == 1){
				circle_pt--;
				n--;
			} else
				putchar(div+'0');
			puts("");
			for (int i = 0; i <= n; i++){
				putchar(i < circle_pt + 1 ? ' ' : '^');
			}
		}
		puts("");
	}
    return 0;
}