#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define F first
#define S second

int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};

int ans[100];
int mod[100];

main(){
  int p,q;
  while(cin>>p>>q){
    int pos=0;

    p*=10;
    while(pos<100){
      ans[pos]=p/q;
      mod[pos]=p;
      p%=q;
      p*=10;
      ++pos;
    }


    if(mod[99]){
      int ms=0,mt;
      for(;;ms++){
	for(mt=ms+1;mt<100;mt++){
	  if(mod[ms]==mod[mt])break;
	}
	if(mt<100)break;
      }

      rep(i,mt)cout<<ans[i];
      cout<<endl;
      rep(i,ms)cout<<' ';
      rep(i,mt-ms)cout<<'^';
      cout<<endl;
    }else{
      for(int i=0;mod[i];i++)cout<<ans[i];
      cout<<endl;
    }
  }
}