#include <iostream>

#include <vector>

#include <set>

#include <algorithm>

using namespace std;



int main() {

    int p,q;

    while(cin>>p>>q) {

        vector<int> fac,ap;

        bool flag;

        while(true) {

            p *= 10;

            fac.push_back(p/q);

            if(find(ap.begin(),ap.end(),p%q) != ap.end()) break;

            if(p%q == 0) {

                for(int i=0; i<fac.size(); ++i) cout<<fac[i];

                cout<<endl;

                goto NEXT;

            }



            ap.push_back(p%q);

            p %= q;

        }



        for(int i=0; i<fac.size(); ++i) cout<<fac[i];

        cout<<endl;



        flag = true;

        cout<<" ";

        for(int i=0; i<ap.size(); ++i) {

            if(ap[i] == p%q) flag = false;

            cout<<(flag?" ":"^");

        }

        cout<<endl;

    NEXT:;

    }

}