GCJ1C09A - All Your Base

no tags 

In A.D. 2100, aliens came to Earth. They wrote a message in a cryptic language, and next to it they wrote a series of symbols. We've come to the conclusion that the symbols indicate a number: the number of seconds before war begins!

Unfortunately we have no idea what each symbol means. We've decided that each symbol indicates one digit, but we aren't sure what each digit means or what base the aliens are using. For example, if they wrote "ab2ac999", they could have meant "31536000" in base 10 -- exactly one year -- or they could have meant "12314555" in base 6 -- 398951 seconds, or about four and a half days. We are sure of three things: the number is positive; like us, the aliens will never start a number with a zero; and they aren't using unary (base 1).

Your job is to determine the minimum possible number of seconds before war begins.


The first line of input contains a single integer, T. T test cases follow. Each test case is a string on a line by itself. The line will contain only characters in the 'a' to 'z' and '0' to '9' ranges (with no spaces and no punctuation), representing the message the aliens left us. The test cases are independent, and can be in different bases with the symbols meaning different things.


For each test case, output a line in the following format:

Case #X: V

Where X is the case number (starting from 1) and V is the minimum number of seconds before war begins.


1 ≤ T ≤ 100

1 ≤ the length of each line < 61

The answer will never exceed 1018



Case #1: 201
Case #2: 75
Case #3: 11

hide comments
well_endowed14: 2021-09-30 13:55:42

powl could be used as the power function if you set the sum as long long.

imprakarsh: 2021-02-12 14:43:36

1.No unary base is allowed(1111 != 4 its 15)
2.Make your own power fn rather than using inbuilt.

div_25: 2019-05-28 16:00:50

We've got three things sure. What's the third thing again?
No need to use power, multiply with base as you go.

vishwanath_26: 2018-06-30 20:10:00

Easy One! Take care of not using power function!

Gowtham.R: 2015-07-09 22:15:43

Finally After 5 WAs

devil: 2015-07-02 00:26:41

brilliant question.....felt great after solving this one....!!! :)

Anchrondite: 2015-03-31 20:25:59

Do not even think to use pow function here got two wrong answers for me due to conversion between double and unsigned long long int .

Bharath Reddy: 2014-09-30 18:10:01

Read the "We are sure of three things.." part very very carefully

Siya: 2014-08-23 18:10:18

Very good problem ,Missing single point can leads to WA..

Flago: 2014-02-27 16:34:41

I have no idea why my PHP give NZEC while it output the same as my C++, and run fine on ideone or in local. Maybe SPOJ doesn't support GMP functions ? Too bad, Limits could get much much higher :

Input : (a to z) 100 times (2600 letters)
Output : Int with 3678 digits :D

Last edit: 2014-02-27 16:38:40

Added by:Shafaet
Time limit:20s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Google Codejam 2009, Round 1C, Problem A