#include<deque>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
  int h,i,j;
  int p,q;
  while(cin>>p>>q){
    int a[90],b[90];
    set<pair<int,int> > c;
    h=-1;
    for(i=0;;i++){
      p*=10;
      a[i]=p/q;
      b[i]=p=p%q;
      if(b[i]){
	if(c.count(make_pair(a[i],b[i]))){
	  for(h=0;a[h]!=a[i]||b[h]!=b[i];h++);
	  break;
	}else{
	  c.insert(make_pair(a[i],b[i]));
	}
      }else{
	i++;
	break;
      }
    }
    for(j=0;j<i;j++)
      cout<<a[j];
    cout<<endl;
    if(h!=-1){
      for(j=0;j<h;j++)
	cout<<" ";
      for(;j<i;j++)
	cout<<"^";
      cout<<endl;
    }
  }
  return 0;
}