#include <iostream>

#include <vector>

#include <map>

#include <algorithm>

#include <string>

using namespace std;



int main() {

    int p,q;

    while(cin>>p>>q) {

        string ans = "";

        int cnt = 0;

        map<int,int> rems;

        while(true) {

            p *= 10;

            ans += string(1, '0'+p/q);

            p %= q;

            if(p == 0) {

                cout<<ans<<endl;

                break;

            }

            if(rems.find(p) != rems.end()) {

                int st = rems[p];

                cout<<ans<<endl;

                for(int i=0; i<=st; ++i) cout<<" ";

                for(int i=st; i<cnt; ++i) cout<<"^";

                cout<<endl;

                break;

            }

            rems[p] = cnt++;

        }



    }

}