#include <iostream>
using namespace std;

int main(void){
	int i,j,x,y;
	int log[128];
	bool flg;

	while(cin>>x>>y){
		flg = false;
		for(i=0;;i++){
			if(i)cout << x/y;
			x = x - y * (x / y);
			if(x == 0) break;
			for(j=i-1;j>=0;j--){
				if(x == log[j]){
					flg = true;
					break;
				}
			}
			if(flg){
				break;
			}
			log[i] = x;
			x *= 10;
		}
		cout<<endl;
		if(flg){
			int k;
			for(k=0;k<j;k++){
				cout<<' ';
			}
			for(;k<i;k++){
				cout<<'^';
			}
			cout<<endl;
		}
	}

  return 0;
}