#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
  int p,q;
  while(cin >>p>>q){
    vector<int> ans,num;
    bool f = true;
    int r;
    num.push_back(p);
    for(;;){
      p*=10;
      vector<int>::iterator pos = find(num.begin(),num.end(),p%q);
      ans.push_back(p/q);
      if(pos != num.end()){r = p%q;break;}
      num.push_back(p%q);
      p%=q;
    }
    for(int i=0; i<ans.size(); i++){
      cout <<ans[i];
    }
    cout <<endl;
    if(r){
      bool ff = false;
      for(int i=0; i<num.size(); i++){
	if(num[i] == r){ff = true;}
	if(ff){cout <<"^";}
	else{cout <<" ";}
      }
      cout <<endl;
    }
  }
  return 0;
}