#include <iostream>
#include <string>
#include <map>

using namespace std;

int main(void){
  int p,q;
  int remain;
  while(cin >> p >> q){
	map<int, bool> m;
	map<int, int> digit;
	string str="";
	int i=0;
	bool loop=false;
	char tmp;
	remain = p%q;
	m[remain] = true;
	digit[0] = remain;
	remain *= 10;
	while(1){
	  tmp = remain / q + '0';
	  remain = remain % q;
	  //	  cout << "remain=" << remain << endl;
	  str += tmp;
	  if(remain == 0 || m[remain] == true) break;
	  digit[i+1] = remain;
	  m[remain] = true;
	  remain *= 10;
	  i++;
	}
	cout << str <<endl;
	if(remain == 0){
	  //do nothing
	}else{
	  for(int i=0;i<str.size();i++){
		if(digit[i] == remain) loop = true;
		if(!loop){
		  cout << " ";
		}else{
		  cout << "^";
		}
	  }
	  cout << endl;
	}
  }
}