import scala.io.StdIn.{readInt,readLine}

object Main {

  def scan(p:Long,q:Long,rList:List[Long],l:List[Long]):(Int,List[Long]) = {
    val n = p/q
    val r = if(p>=q) p%q else p
    if(r==0) (0, (n::l).reverse)
    else if(rList.contains(r)) {
      val i = rList.reverse.indexWhere(_==r)
      (i+1,(n::l).reverse)
    }
    else scan(10*r, q, r::rList, n::l)
  }

  def main(args:Array[String]) = {
    Iterator.continually(readLine).takeWhile(_!=null).foreach { s =>
      val n = s.split(" ").map(_.toInt)
      val (repeat, value) = scan(n(0),n(1),Nil,Nil)
      println(value.drop(1).mkString)
      if(repeat>0) {
        (0 until repeat-1).foreach(_=>print(' '))
        (repeat-1 until value.length-1).foreach(_=>print('^'))
        println()
      }
    }
  }
}