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

using namespace std;

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