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

bool judgeExtra( int* r, int j )
{
    for( int i = 0; i < j; i++ )
        if( r[i] == r[j] )
            return true;
    return false;
}

int main()
{
    int a, b, r[100];
    string s, t;

    while( cin >> a >> b )
    {
        s = "";
        t = "";
        for( int i = 0; i < 100; i++ )
             r[i] = 0;
        
        int j = 0;
        bool judge = true;
        while( 1 )
        {
            s += ( a / b ) + '0';
            t += ' ';

            if( a % b != 0 )
            {
                r[j] = a % b;
                int cnt = 0;
                if( judgeExtra( r, j ) )
                {
                    t[j] = '^';
                    for( int i = j - 1; i >= 0; i-- )
                    {
                        if( r[i] == a % b )
                            break;
                        else
                            t[i] = '^';
                    }
                    break;
                }
                a = ( a % b ) * 10;
                j++;
            }
            else
            {
                judge = false;
                break;
            }
        }
        for( int i = 1; i < s.size(); i++ )
            cout << s[i];
        cout << endl;

        if( judge )
        {
            for( int i = 1; i < t.size(); i++ )
                cout << t[i];
            cout << endl;
        }

    }
    return 0;
}
