#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int main(void){
  while( true ){
    int loopStart = -1;
    bool bContDividing = false;
    vector<int> mods, quots;
    unsigned int p, q;
  
    cin >> p >> q;
    if( cin.eof() ) break;

    mods.push_back( p % q );

    do{
      p = mods[mods.size()-1] * 10;
      vector<int>::iterator itSearchRes = find(mods.begin(), mods.end(), p%q);
      if( p % q == 0 ) bContDividing = false;
      else if( p % q != 0 && itSearchRes == mods.end() ) bContDividing = true;
      else{
	loopStart = distance(mods.begin(), itSearchRes);
	bContDividing = false;
      }
      quots.push_back(p / q);
      mods.push_back(p % q);
    }while( bContDividing );

    for(int i = 0; i < quots.size(); i++) cout << quots[i];
    cout << '\n';
    if( loopStart >= 0 ){
      for(int i = 0; i < loopStart; i++) cout << ' ';
      for(int i = loopStart; i < quots.size(); i++) cout << '^';
      cout << '\n';
    }
  }
  return 0;
}