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

typedef vector<int> V;

bool inv(int key, V v){
    for(V::iterator it = v.begin(); it != v.end(); it++){
        if( *it == key ) return true;
    }
    return false;
}

int main(){
    int p,q;
    while( cin >> p >> q ){
        V ldiv;
        V lrem;
        int d, r;
        lrem.push_back( p );
        while( true ){
            d = p*10/q;
            r = p*10%q;
            ldiv.push_back( d );
            if( !r || inv( r, lrem ) ) break;
            lrem.push_back( r );
            p = r;
        }
        for(V::iterator it = ldiv.begin(); it != ldiv.end(); it++)
            cout << *it;
        cout << '\n';
        if( r ){
            int f = 0;
            for(V::iterator it=lrem.begin(); it!=lrem.end() && f<2; it++){
                if( *it == r ) f++;
                if( f ) cout << '^';
                else cout << ' ';
            }
            cout << '\n';
        }
    }
    return 0;
}