#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;

string toString(int n){
  ostringstream oss;
  oss << n;
  return oss.str();
}

vector<int> mod;

int boundary;

bool findMod(int M){
  for(int i = 0 ; i < mod.size() ; i++){
    if(mod[i] == M){
      boundary = mod.size()-i;
      return true;  
    }
  }
  return false;
}

int main(){
  int a, b;
  
  while(cin >> a >> b){
    bool flag = false;
    mod.clear();
    string ans = "";
    //a = a % b;
    mod.push_back(a);
    // a *= 10;
    for(; ;){
      a *= 10;
      int c = a / b;
      string s = toString(c);
      ans += s;
      if(a % b == 0){
	flag = true;
	goto END;
      }
      if(findMod(a % b)) goto END;
      mod.push_back(a % b);
      a -= b * c;

    }
  END:;
    if(flag) cout << ans << endl;
    else{
      cout << ans << endl;
      for(int i = 0 ; i < ans.size() - boundary ; i++) cout << ' ';
      for(int i = ans.size() - boundary ; i < ans.size() ; i++) cout << '^';
      cout << endl;
    }
    
  }
  return 0;
}