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

int main()
{
	int p, q;
	while (std::cin >> p >> q) {
		
		int digits_count = 0;
		std::vector<int> remainders;
		int recurring_pos = -1;
		while (true) {
			if (p==0) break;
			
			std::vector<int>::iterator it = std::find(remainders.begin(), remainders.end(), p);
			if ( it != remainders.end() ) {
				recurring_pos = std::distance(remainders.begin(), it);
				break;
			}
			
			remainders.push_back(p);
			p *= 10;
			std::cout << p/q;
			p = p % q;
			digits_count++;
		}
		std::cout << std::endl;
		
		if (recurring_pos >= 0) {
			for (int d=0; d<recurring_pos; d++) std::cout << " ";
			for (int d=recurring_pos; d<digits_count; d++) std::cout << "^";
			std::cout << std::endl;
		}
		
	}
	
	return 0;
}


