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

using namespace std;

int main()
{
  int p,q;
  for(;cin>>p>>q;)
    {
      bool loop =false;
      vector<int> mod;
      int count;
      for(;!(p==0||loop);)
	{
	  p*=10;
	  cout<<p/q;
	  p=p%q;
	  if(find(mod.begin(),mod.end(),p)==mod.end())
	    {
	      mod.push_back(p);
	    }
	  else
	    {
	      count= find(mod.begin(),mod.end(),p)-mod.begin();
	      loop=true;
	    }
	}
      cout<<endl;
      if(loop)
	{
	  for(int i=0;i<count+1;i++)
	    cout<<" ";
	  for(int i=count+1;i<=mod.size();i++)
	    cout<<"^";
	  cout<<endl;
	}
    }
}