#include <bits/stdc++.h>

using namespace std;

int main()
{
  int p, q;
  while(cin >> p >> q){
    static int used[1000010];
    memset(used, -1, sizeof(used));
    string ans = "";
    for(int i = 0; ; i++){
      p *= 10;
      int r = p / q;
      int l = p % q;
      ans += to_string(r);
      if(l == 0){
	cout << ans << endl;
	break;
      } else if(used[l] != -1){
	cout << ans << endl;
	for(int j = 0; j <= i; j++){
	  if(j <= used[l]) cout << " ";
	  else cout << "^";
	}
	cout << endl;
	break;
      } else {
	p = l;
	used[l] = i;
	int k = p * 10;
	if(i == 0 && k % q == l) {
	  cout << ans << endl;
	  cout << "^" << endl;
	  break;
	}
      }
    }
  }
  return 0;
}