Problem:
Problem F: Lying about Your Age

You have moved to a new town. As starting a new life, you have made up your mind to do
one thing: lying about your age. Since no person in this town knows your history, you don’t
have to worry about immediate exposure. Also, in order to ease your conscience somewhat,
you have decided to claim ages that represent your real ages when interpreted as the base-M
numbers (2 ≤ M ≤ 16). People will misunderstand your age as they interpret the claimed age
as a decimal number (i.e. of the base 10).

Needless to say, you don’t want to have your real age revealed to the people in the future. So
you should claim only one age each year, it should contain digits of decimal numbers (i.e. ‘0’
through ‘9’), and it should always be equal to or greater than that of the previous year (when
interpreted as decimal).

How old can you claim you are, after some years?

Input

The input consists of multiple datasets. Each dataset is a single line that contains three integers
A, B, and C, where A is the present real age (in decimal), B is the age you presently claim
(which can be a non-decimal number), and C is the real age at which you are to find the age
you will claim (in decimal). It is guaranteed that 0 ≤ A < C ≤ 200, while B may contain up to
eight digits. No digits other than ‘0’ through ‘9’ appear in those numbers.

The end of input is indicated by A = B = C = -1, which should not be processed.

Output

For each dataset, print in a line the minimum age you can claim when you become C years old.
In case the age you presently claim cannot be interpreted as your real age with the base from 2
through 16, print -1 instead.

Sample Input

23 18 53
46 30 47
-1 -1 -1

Output for the Sample Input

49
-1

Solution in C++ (judge verdict: Accepted)

#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

ll int2int(ll src, int base) {
  string res;
  while(src != 0) {
    char c = src%base;
    if (c < 10) c += '0';
    else c += 'a'-10;
    res = string(1,c) + res;
    src /= base;
  }
  FOR(it, res) if (!isdigit(*it)) return -1;
  return atoi(res.c_str());
}

int main() {
  ll A,B,C;
  while(cin>>A>>B>>C,A!=-1) {
    bool f = 0;
    for (int i=2; i<=16; ++i) {
      if (B == int2int(A,i)) {
        f = 1;
        break;
      }
    }
    if (!f) {
      puts("-1");
      continue;
    }
    for (int i=A+1; i<=C; ++i) {
      ll next = -1;
      for (int j=2; j<=16; ++j) {
        ll k = int2int(i,j);
        if (k >= B) {
          if (next == -1 || next > k) {
            next = k;
          }
        }
      }
      B = next;
    }
    cout << B << endl;
  }
}