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

int main(){
	int p, q;
	map<int, bool> used;
	vector<int> nums;
	while(cin >> p >> q){
		used.clear();
		nums.clear();
		p %= q;
		p *= 10;
		int i = 0;
		while(p != 0){
			used[p] = true;
			nums.push_back(p);
			cout << p / q;
			p %= q;
			p *= 10;
			if(used[p]){
				while(i < nums.size() && nums[i] != p) i++;
				break;
			}
		}
		cout << endl;
		if(p != 0){
			for(int j = 0; j < i; j++) cout << " ";
			for(int j = i; j < nums.size(); j++) cout << "^";
			cout << endl;
		}
	}
	return 0;
}