#include<bits/stdc++.h>
using namespace std;
int main(){
  int p,q,used[1000005];
  while(cin>>p>>q){
    for(int i=0;i<q;i++)used[i]=-1;
    vector<int>ans;
    int i=0,idx=-1;
    while(p){
      if(used[p]!=-1){idx=used[p];break;}
      used[p]=i;
      p*=10;
      ans.push_back(p/q);
      p%=q;
      i++;
    }
    for(int j=0;j<ans.size();j++)
      cout<<ans[j];
    cout<<endl;
    if(idx!=-1){
      for(int j=0;j<ans.size();j++){
	if(j<idx)cout<<" ";
	else cout<<"^";
      }
      cout<<endl;
    }
  }
  return 0;
}