Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Problem B:
Miscalculation

Bob is an elementary schoolboy, not so good at mathematics. He found Father's calculator and tried cheating on his homework using it. His homework was calculating given expressions containing multiplications and additions. Multiplications should be done prior to additions, of course, but the calculator evaluates the expression from left to right, neglecting the operator precedence. So his answers may be the result of either of the following two calculation rules.

Doing multiplication before addition

Doing calculation from left to right neglecting the operator precedence

Write a program that tells which of the rules is applied from an expression and his answer.

An expression consists of integers and operators. All the integers have only one digit, from 0 to 9. There are two kinds of operators + and *, which represent addition and multiplication, respectively.

The following is an example expression.

1+2*3+4

Calculating this expression with the multiplication-first rule, the answer is 11, as in Sample Input 1. With the left-to-right rule, however, the answer will be 13 as shown in Sample Input 2.

There may be cases in which both rules lead to the same result and you cannot tell which of the rules is applied. Moreover, Bob sometimes commits miscalculations. When neither rules would result in Bob’s answer, it is clear that he actually did.

Input

The input consists of a single test case specified with two lines. The first line contains the expression to be calculated. The number of characters of the expression is always odd and less than or equal to 17. Each of the odd-numbered characters in the expression is a digit from '0' to '9'. Each of the even-numbered characters is an operator '+' or '*'. The second line contains an integer which ranges from 0 to 999999999, inclusive. This integer represents Bob's answer for the expression given in the first line.

Output

Output one of the following four characters:

M     When only the multiplication-first rule results Bob's answer.

L     When only the left-to-right rule results Bob's answer.

U     When both of the rules result Bob's answer.

I     When neither of the rules results Bob's answer.

Sample Input 1

1+2*3+4
11

Sample Output 1

M

Sample Input 2

1+2*3+4
13

Sample Output 2

L

Sample Input 3

3
3

Sample Output 3

U

Sample Input 4

1+2*3+4
9

Sample Output 4

I

Solution in Scala (judge verdict: Accepted)

import scala.annotation.tailrec
import scala.io.StdIn._
object Main extends App {

  val input = readLine.trim.toList
  val bob = readLine.trim.toInt
  val multiplicationFirst = input match {
    case m MultiplicationFirst Nil ⇒ m
    case _ ⇒ None
  }
  val left = input match {
    case l Left Nil ⇒ l
    case _ ⇒ ???
  }
  println(
    if (bob == multiplicationFirst && bob != left) 'M'
    else if (bob != multiplicationFirst && bob == left) 'L'
    else if (bob == multiplicationFirst && bob == left) 'U'
    else 'I'
  )

  object MultiplicationFirst {
    def unapply(str: List[Char]): Option[(Long, List[Char])] = {
      str match {
        case l Factor t ⇒
          t match {
            case '+':: (r MultiplicationFirst rest) ⇒ Some(l + r, rest)
            case Nil ⇒ Some(l, t)
            case _ ⇒ ???
          }
        case _ ⇒ ???
      }
    }
    private object Factor {
      def unapply(str: List[Char]): Option[(Long, List[Char])] = {
        str match {
          case l Number t ⇒ Some(takeAll(t, l))
          case _ ⇒ None
        }
      }
    }
    @tailrec private[this] def takeAll(str: List[Char], left: Long): (Long, List[Char]) = {
      str match {
        case '*'::(r Number rest) ⇒ takeAll(rest, left * r)
        case _ ⇒ (left, str)
      }
    }
  }
  object Left {
    def unapply(str: List[Char]): Option[(Long, List[Char])] = {
      str match {
        case l Number t ⇒ Some(takeAll(t, l), Nil)
        case _ ⇒ ???
      }
    }
    @tailrec private[this] def takeAll(str: List[Char], left: Long): Long = {
      str match {
        case '+'::(r Number rest) ⇒ takeAll(rest, left + r)
        case '*'::(r Number rest) ⇒ takeAll(rest, left * r)
        case Nil ⇒ left
        case _ ⇒ ???
      }
    }
  }
  object Number {
    def unapply(str: List[Char]): Option[(Long, List[Char])] = {
      str.span(_.isDigit) match {
        case (Nil, _) ⇒ None
        case (n, t) ⇒ Some(n.foldLeft(0L){(a, b) ⇒ a * 10 + b.asDigit }, t)
      }
    }
  }
  implicit class Extension[T](val value: T) extends AnyVal {
    @inline def let[A](@inline func: T ⇒ A): A = func(value)
    @inline def also(@inline func: T ⇒ Unit): T = {func(value); value}
  }
}