#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;

    set<int> app;
    app.insert(p);
    bool loop=false;
    int sg;
    while(true){
      ans[pos]=p*10/q;
      mod[pos]=p=(p*10)%q;
      if(p==0){
	++pos;
	break;
      }
      
      if(app.count(p)){
	++pos;
	ans[pos]=p*10/q;

	loop=true;
	sg=p;
	break;
      }
      app.insert(p);
      ++pos;
    }
    rep(i,pos)cout<<ans[i];
    cout<<endl;
    if(loop){
      int i;
      for(i=0;i<pos-1;i++){
	if(i && mod[i-1]==sg)break;
	cout<<' ';
      }

      for(;i<pos;i++)cout<<'^';
      cout<<endl;
    }
  }
}