#include <vector>
#include <iostream>
#include <cassert>

int f(std::vector<int> &digit, int p, int q) {
	int double_step = (p % q) * 10;
	while (p != 0 && p != double_step) {
		digit.push_back(p / q);
		p = (p % q) * 10;
		double_step = (double_step * 10 % q) * 10;
	}
	digit.push_back(p / q);
	int count;
	if (p != 0) {
		count = 1;
		p = (p % q) * 10;
		while (p != double_step) {
			digit.push_back(p / q);
			p = (p % q) * 10;
			++count;
		}
	}
	else {
		count = 0;
	}
	return count;
}
int main() {
	int p, q;
	while (std::cin >> p >> q) {
		std::vector<int> digit;
		int count = f(digit, p, q);
		if (count != 0) {
			auto end = digit.size() - 1;
			auto start = end - count;
			assert(0 <= start && start < digit.size());
			assert(0 <= end && end < digit.size());
			while (digit.at(start) == digit.at(end)) {
				assert(0 <= start && start < digit.size());
				assert(0 <= end && end < digit.size());
				--start; --end;
			}
			++start; ++end;
			for (auto i = 1; i < end; ++i) {
				assert(0 <= i && i < digit.size());
				std::cout << digit.at(i);
			}
			std::cout << "\n";
			for (auto i = 1; i < start; ++i) {
				std::cout << " ";
			}
			for (auto i = start; i < end; ++i) {
				std::cout << "^";
			}
			std::cout << "\n";
		}
		else {
			for (auto i = 1; i < digit.size() - 1; ++i) {
				std::cout << digit.at(i);
			}
			std::cout << "\n";
		}
		std::cout << std::flush;
	}
	return 0;
}