#include<iostream>
#include<vector>
main(){for(int p,q,i;std::cin>>p>>q;puts("")){std::vector<int>r,u(q,1);for(;(p%=q)*u[p];u[p]=i=0,printf("%d",(p*=10)/q))r.push_back(p);if(p)for(puts("");i<r.size();printf(q?" ":"^"))q=r[i++]^p?q:0;}}