#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; ++i)
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define P pair<int, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())

int check(vector<double> &n, double d){
  REP(i, n.size()) if(n[i] == d) return i;
  n.push_back(d);
  return -1;
}

int main() {
  int p, q;
  while(cin >>p >>q){
    vector<double> n;
    stringstream ss;
    int last = -1;
    while(true){
      double tmp = (double)(p) / q;
      if(tmp == 0.0 || (last = check(n, tmp)) != -1) break;
      p *= 10;
      while(p < q){
        n.push_back( (double)(p) / q );
        p *= 10;
        ss <<"0";
      }
      ss <<p / q;
      p %= q;
    }
    cout <<ss.str() <<endl;
    if(last != -1){
      REP(i, ss.str().length()) cout <<(i < last ? " " : "^");
      cout <<endl;
    }
  }
  return 0;
}