#include <iostream>
#include <vector>
#include <algorithm>
int main(){
	int a = 0, b = 0;
	while(std::cin >> a >> b, !std::cin.eof()){
		std::vector<int> r, q;
		while(1){
			a *= 10;
			if(std::find(r.begin(), r.end(), a%b) != r.end() && std::find(q.begin(), q.end(), a/b) != q.end())break;
			q.push_back(a/b);
			r.push_back(a%b);
			a %= b;
		}
		int first = std::find(r.begin(), r.end(), a%b) - r.begin();

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

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