#include <iostream>
#include <vector>
#include <algorithm>

typedef std::pair<int, int> P;

int main(){
	int a = 0, b = 0;
	while(std::cin >> a >> b, !std::cin.eof()){
		std::vector<P> v;
		while(a%b){
			a *= 10;

			if(std::find(v.begin(), v.end(), P(a%b, a/b)) != v.end())break;
			v.push_back(P(a%b, a/b));
			a %= b;
		}
		int first = std::find(v.begin(), v.end(), P(a%b, a/b)) - v.begin();

		for(int i=0;i<v.size();i++){
			std::cout << v[i].second;
		}std::cout << std::endl;

		if(a%b == 0)continue;
		for(int i=0;i<v.size();i++){
			if(i >= first)
				std::cout << "^";
			else
				std::cout << " ";
		}std::cout << std::endl;
	}
}