#include<cstdio>
#include<cstring>

int p,q;
int data[85];
int encount[10000000];
int main(void){
	while(~scanf("%d%d",&p,&q)){
		memset(encount,0,sizeof(encount));
		memset(data,0,sizeof(data));
		p*=10;
		for(int i=1;i<=80;i++){
			data[i]=p/q;
			p=p%q;
			if(p==0){
				for(int j=1;j<=i;j++)printf("%d",data[j]);
				printf("\n");
				break;
			}
			else{
				if(encount[p]==0)encount[p]=i;
				else if(encount[p]!=0){
					for(int j=1;j<=i;j++)printf("%d",data[j]);
					printf("\n");
					for(int j=1;j<=i;j++)printf("%c",(encount[p]<j && j<=i)?'^':' ');
					printf("\n");
					break;
				}
			}
			p*=10;
		}
	}
	return 0;
}