#include<bits/stdc++.h>
using namespace std;

map<int,int> flg;

void solve(int p,int q,int now){
  int r = p % q;
  int w = p / q;
  if(flg[r] != 0){
    cout << w << endl;
    for(int i=0;i<flg[r];i++) cout << " ";
    for(int i=flg[r];i<now+1;i++) cout << "^";
    cout << endl;
  } else {
    flg[r] = now + 1;
    cout << w;
    if(r != 0) solve(r*10,q,now+1);
    else cout << endl;
  }
  return;
}

int main(){
  int p,q;
  while(cin >> p >> q){
    flg[p%q] = 1;
    solve(p*10,q,0);
    flg.clear();
  }
}