#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
	int a,b;
	while(cin>>a>>b) 
	{
		bool f[b];
		int tmp[b];
		fill(f,f+b,0);
		string ans="";
		int rest=a*10;
		int cnt=0;
		while(rest!=0) 
		{
			int div=rest/b;
			rest=rest%b;
			ans+=div+'0';
			if(f[rest]) break;
			f[rest]=true;
			tmp[rest]=cnt;
			cnt++;
			rest*=10;
		}
		cout<<ans<<endl;
		if(rest!=0)
		{
			for(int i = 0; i < ans.size()-1; ++i)
			{
				if(i<tmp[rest]) cout<<' ';
				else cout<<'^';
			}
			cout<<endl;
		}
	}
  return 0;
}