#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	int p, q;
	while( cin >> p >> q ){
		string answer;
		vector< int > covered;
		int loop = -1;

		while( p != 0 ){
			for( int i = 0; i < (int)covered.size(); ++i ){
				if( p * 10 == covered[ i ] ){
					loop = i;
					break;
				}
			}
			if( loop != -1 )break;

			while( ( p *= 10 ) < q ){
				answer += '0';
				covered.push_back( p );
			}

			covered.push_back( p );

			int div = p / q;
			answer += (char)( div + '0' );
			p %= q;
		}

		cout << answer << endl;
		if( loop != -1 ){
			for( int i = 0; i < loop; ++i ){
				cout << ' ';
			}
			for( int i = loop; i < (int)answer.size(); ++i ){
				cout << '^';
			}
			cout << endl;
		}
	}
	return 0;
}