#include<iostream>
#include<map>
#include<vector>
#include<string>

int p, q;
std::map<int, int> m;
std::vector<int> v;

int main()
{
	while( std::cin >> p >> q )
	{
		m.clear();
		v.clear();
		
		p *= 10;

		int cnt = 0;
		int s = -1;
		while( true )
		{
			v.push_back( p / q );
			p %= q;
			
			if( !p )
				break;

			if( m.count( p ) )
			{
				s = m.find( p )->second + 1;
				break;
			}
			
			m.insert( std::make_pair( p, cnt++ ) );
			p *= 10;
		}

		for( int i = 0; i != v.size(); ++i )
			std::cout << v[i];

		std::cout << std::endl;

		if( s != -1 )
			std::cout << std::string( s, ' ' ) << std::string( v.size() - s, '^' ) << std::endl;
	}

	return 0;
}