#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <queue>
#include <set>
#include <map>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <bitset>
using namespace std;
  
  
const double EPS = 1e-9;
  
int main(){
 
	int p, q;
	while(cin >> p >> q){
		vector<int> rest;
		vector<int>::iterator l;
		bool loop = false;
		for(int i = 0; p != 0; i++){
			if((l = find(rest.begin(), rest.end(), p)) != rest.end()){
				loop = true;
				break;
			}
			rest.push_back(p);
			p*=10;
			cout << p/q;
			p%=q;
		}
		cout << endl;
		if(loop){
			vector<int>::iterator it;
			for(it = rest.begin(); it != rest.end(); it++){
				cout << ((it>=l)?'^':' ');
			}
			cout << endl;
		}
	}
  
    return 0;
}