#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;


template <class T>
ostream& operator<<(ostream& os, vector<T> v) {
    os << "{";
    for (int i = 0; i < v.size(); ++i) {
        if(i) os << ", ";
        os << v[i];
    }
    return os << "}";
}

template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> p) {
    return os << "{" << p.first << ", " << p.second << "}";
}


int main() {
    int a, b;
    while(cin >> a >> b){
        string ans;
        map<int, int> m;
        int cur = 0;
        while(!m.count(a) && a){
            m[a] = cur++;
            a *= 10;
            ans += a/b+'0';
            a %= b;
        }
        cout << ans << "\n";
        if(a){
            for (int i = 0; i < ans.size(); ++i) {
                if(i < m[a]) putchar(' ');
                else putchar('^');
            }
            puts("");
        }
    }
    return 0;
}
