DMOPC '15 Contest 5 P3 - All Your Base

View as PDF

Submit solution


Points: 7 (partial)
Time limit: 1.0s
Memory limit: 64M

Author:
Problem type

Okabe likes writing notes for himself. Since he writes about strange things and does not want others to think he is weird, he encodes all his messages in his own super-secret format. Unfortunately for Okabe, you — being an amazing cryptanalyst — have figured out his scheme.

First, he maps a character to an integer D, and then converts it into another integer E by changing its base. He creates a random list A of N integers, where Ai1 is stored in base Ai, and E is stored in base A0. That is,

D=EA0 A1  AN1

You've gotten your hands on some of Okabe's notes, and would like to decrypt some E values, given A. Can you do it?

Reminder

The notation for representing a number X in base Y is XY.

Input Specification

The first line of input will contain the integers E and N (1N10).
The next and final line of input will contain N space-separated integers making up A, with the i-th integer representing Ai (1Ai 1010). AN1 is always given in base 10, and it is guaranteed that E can be converted (for every digit d in Ai1, d<Ai unless Ai=1, when d=1).

Output Specification

A single integer, D (0D109).

Sample Input 1

Copy
5 3
10 10 10

Sample Output 1

Copy
5

Sample Input 2

Copy
5000 4
7 1001 10 2

Sample Output 2

Copy
1715

Explanation

Working your way up from AN1,
5000 7 1001 10 2=5000 7 1001 2=5000 7 9=5000 7=1715

Step-by-step,
102=2.
10012=9.
79=7.
50007=1715.


Comments

There are no comments at the moment.