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

int main(){
    int p, q;
	while(cin >> p >> q){
	    map<int,int> used;
        vector<int> dig;
        
		int n = p * 10;
		bool fin = true;
        int start, size;
        
        size = 0;
		for(;;){
			dig.push_back(n / q);

			int m = n % q;
            if(m == 0)break;
			if(used.count(m)){
				fin = false;
				
                start = used[m];
                size = size - used[m];
                
                break;
			}

			used[m] = size++;
            
            n %= q;
            n *= 10;
		}

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

		if(fin)continue;

		for(int i=0;i<dig.size();i++){
			if(i <= start || i > start + size)cout << " ";
			else cout << "^";
		}

		cout << endl;
	}
	return 0;
}