This page is still under construction.

Parts of this page are still being built. What you see may change.

Methodic Multiplication

Time limit2sMemory limit1024 MB

Summary
Read two Peano-encoded natural numbers and print their product, also in Peano form.
Level

Easy2 of 10

Topics
String, Math, Implementation, Simulation
Solved
No attempts yet

Problem

After one computer crash too many, Alonso has had enough of all this shoddy software and poorly written code. He decides that in order for this situation to improve, the glass house that is modern programming needs to be torn down and rebuilt from scratch using only completely formal axiomatic reasoning. As one of the first steps, he decides to implement arithmetic with natural numbers using the Peano axioms.

The Peano axioms (named after Italian mathematician Giuseppe Peano) are an axiomatic formalization of the arithmetic properties of the natural numbers. We have two symbols: the constant 00, and a unary successor function SS. The natural numbers, starting at 00, are then 00, S(0)S(0), S(S(0))S(S(0)), S(S(S(0)))S(S(S(0))), and so on. With these two symbols, the operations of addition and multiplication are defined inductively by the following axioms: for any natural numbers xx and yy, we have [ \begin{align*} x + 0 &= x & x \cdot 0 &= 0 \ x + S(y) &= S(x + y) & x \cdot S(y) &= x \cdot y + x \end{align*} ] The two axioms on the left define addition, and the two on the right define multiplication.

For instance, given x=S(S(0))x = S(S(0)) and y=S(0)y = S(0) we can repeatedly apply these axioms to derive [ \begin{align*} x \cdot y &= S(S(0)) \cdot S(0) = S(S(0)) \cdot 0 + S(S(0))\ &= 0 + S(S(0)) = S(0 + S(0)) = S(S(0 + 0)) = S(S(0)) \end{align*} ] Write a program which given two natural numbers xx and yy, defined in Peano arithmetic, computes the product x⋅yx \cdot y.

Input

The input consists of two lines. Each line contains a natural number defined in Peano arithmetic, using at most 1 0001\,000 characters.

Output

Output the product of the two input numbers.

Examples2

  1. Example 1

    Input
    S(S(0))
    S(S(S(0)))
    
    Expected output
    S(S(S(S(S(S(0))))))
    
  2. Example 2

    Input
    S(S(S(S(S(0)))))
    0
    
    Expected output
    0