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

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
  int a,b,c;
  while(cin >> a >> b >> c && a!=-1) {
    bool f=0;
    for(int i=2; i<17; i++) {
      int x=0,z=a;
      string s="";
      while(z) {
        s+=(char)(z%i+'0');
        z/=i;
      }
      reverse(s.begin(),s.end());
      bool ff=1;
      for(int j=0; j<s.size(); j++) {
        if(!isdigit(s[j])) ff=0;
        else if(s[i]-'0'>=i) ff=0;
      }
      if(!ff) continue;
      stringstream ss;
      ss << s;
      ss >> x;
      if(x==b) f=1;
    }
    if(!f) {
      cout << -1 << endl;
      continue;
    }
    for(int k=a+1; k<=c; k++) {
      int r=1<<29;
      for(int i=2; i<17; i++) {
        int x=0,z=k;
        string s="";
        while(z) {
          s+=(char)(z%i+'0');
          z/=i;
        }
        reverse(s.begin(),s.end());
        bool ff=1;
        for(int j=0; j<s.size(); j++) {
          if(!isdigit(s[j])) ff=0;
          else if(s[i]-'0'>=i) ff=0;
        }
        if(!ff) continue;
        stringstream ss;
        ss << s;
        ss >> x;
        if(b<=x) r=min(r,x);
      }
      b=r;
    }
    cout << b << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3228
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
 
int main() {
  int a,b,c;
  while(cin>>a>>b>>c&&a!=-1) {
    bool f=0;
    for(int i=2; i<17; i++) {
      int x=0,z=a;
      string s="";
      while(z) {
        s+=(char)(z%i+'0');
        z/=i;
      }
      reverse(s.begin(),s.end());
      bool ff=1;
      for(int j=0; j<s.size(); j++) {
        if(!isdigit(s[j])) ff=0;
        else if(s[i]-'0'>=i) ff=0;
      }
      if(!ff) continue;
      stringstream ss;
      ss<<s;ss>>x;
      if(x==b) f=1;
    }
    if(!f) {
      cout<<-1<<endl;
      continue;
    }
    for(int k=a+1; k<=c; k++) {
      int r=1<<29;
      for(int i=2; i<17; i++) {
        int x=0,z=k;
        string s="";
        while(z) {
          s+=(char)(z%i+'0');
          z/=i;
        }
        reverse(s.begin(),s.end());
        bool ff=1;
        for(int j=0; j<s.size(); j++) {
          if(!isdigit(s[j])) ff=0;
          else if(s[i]-'0'>=i) ff=0;
        }
        if(!ff) continue;
        stringstream ss;
        ss<<s;ss>>x;
        if(b<=x) r=min(r,x);
      }
      b=r;
    }
    cout<<b<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3152
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
int m(int a,int k){
  if(!a) return 0;
  string s;
  while(a){
    int tmp=a%k;
    if(tmp>9) return -1;
    s+='0'+tmp;
    a/=k;
  }
  reverse(s.begin(),s.end());
  return stoll(s);
}
signed main(){
  int a,b,c;
  while(cin>>a>>b>>c,~a){
    bool f=1;
    for(int i=2;i<=16;i++) f&=m(a,i)!=b;
    int res=b;
    for(int j=a;j<=c;j++){
      int tmp=-1;
      for(int i=2;i<=16;i++)
	if(m(j,i)>=res)
	  if(tmp<0||m(j,i)<tmp) tmp=m(j,i);
      if(tmp<0){
	f=1;
	break;
      }
      res=tmp;
    }
    cout<<(f?-1:res)<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3132
final_rank: 6

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
char str[20];
char s[20];
int main(){
	int a,b,c;
	while(scanf("%d%d%d",&a,&b,&c),~a){
		bool ok=false;
		sprintf(str,"%d",b);
		for(int i=2;i<17;i++){
			long long tmp=0;
			for(int j=0;str[j];j++){
				tmp*=i;
				tmp+=str[j]-'0';
			}
			bool OK=true;
			for(int j=0;str[j];j++){
				if(str[j]-'0'>=i)OK=false;
			}
			if(tmp==a&&OK)ok=true;
		}
		if(!ok){
			printf("-1\n");continue;
		}
		for(int i=a+1;i<=c;i++){
			int to=999999999;
			for(int j=2;j<17;j++){
				int tmp=i;
				int at=0;
				while(tmp){
					s[at++]='0'+tmp%j;
					tmp/=j;
				}
				ok=true;
				for(int k=0;k<at;k++){
					if(s[k]>'9')ok=false;
					str[at-1-k]=s[k];
				}
				if(ok){
					str[at]=0;
					int val;
					sscanf(str,"%d",&val);
					if(b<=val)to=min(to,val);
				}
			}
			b=to;
		}
		printf("%d\n",b);
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1048
final_rank: 2

code_snippet: #include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;

const int IINF = INT_MAX;

bool converter(int value,int base,string &s){
  string ret = "";
  while( value >= base ){
    int tmp = value % base;
    value /= base;
    if( 0 <= tmp && tmp <= 9 ) ret += string(1,(char)('0'+tmp));
    else                       return false;
  }
  if( value != 0 ) {
    int tmp = value;
    if( 0 <= tmp && tmp <= 9 ) ret += string(1,(char)('0'+tmp));
    else                       return false;
  }
  if( ret.empty() ) ret = "0";
  reverse(ret.begin(),ret.end());
  s = ret;
  return true;
}

string itos(int i) { stringstream ss; ss << i; return ss.str(); }

int main(){
  int A,C;
  string B;
  while( cin >> A >> B >> C ){
    if( A == -1 && B == "-1" && C == -1 ) break;
    string boarder = B;
    bool failed = false;
    REP(age,A,C+1){
      //cout << age << " " << boarder << endl;
      int nebear = IINF;
      REP(base,2,17){
        string tmp = "";
        if( !converter(age,base,tmp) ) continue;
        if( age == A && tmp != boarder ) continue;
        else {
          int boar = (atoi)(boarder.c_str());
          int curr = (atoi)(tmp.c_str());
          if( boar > curr ) continue;
          nebear = min(nebear,curr);
        }
      }
      if( nebear == IINF ) failed = true;
      if( failed ) break;
      boarder = itos(nebear);
    }
    if( failed ) puts("-1");
    else cout << boarder << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 1244
final_rank: 5

code_snippet: #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;
  }
}
accuracy: 1
time_ms: 30
memory_kb: 1216
final_rank: 3

code_snippet: //41
#include<iostream>
#include<cctype>
#include<string>
#include<cstdlib>

using namespace std;

string itos(int i,int b){
  if(i){
    string s;
    while(i){
      int d=i%b;
      s+=(d<10)?'0'+d:'a'+d-10;
      i/=b;
    }
    return string(s.rbegin(),s.rend());
  }else{
    return "0";
  }
} 

int main(){
  int a,c;
  string b;
  while(cin>>a>>b>>c,a!=-1){
    int i;
    for(i=16;i>=2;i--){
      if(itos(a,i)==b)break;
    }
    if(i<2){
      cout<<-1<<endl;
    }else{
      for(;a<=c;a++){
	for(int i=16;i>=2;i--){
	  string s=itos(a,i);
	  int j;
	  for(j=0;j<s.size();j++){
	    if(isalpha(s[j]))break;
	  }
	  if(j==s.size()&&atoi(b.c_str())<=atoi(s.c_str())){
	    b=s;
	    break;
	  }
	}
      }
      cout<<b<<endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 1228
final_rank: 4

code_snippet: #include <iostream>

using namespace std;
typedef long long ll;
const ll INF = 0x7fffffffffffffffll;

ll radix_convert(ll x, int base){
	ll a = x, b = 0, c = 1;
	while(a > 0){
		if(a % base >= 10){ return -1; }
		b = b + a % base * c;
		a /= base;
		c *= 10;
	}
	return b;
}

int main(){
	while(true){
		ll a, b, c;
		cin >> a >> b >> c;
		if(a < 0 && b < 0 && c < 0){ break; }
		bool accept = false;
		for(int base = 2; base <= 16; ++base){
			if(radix_convert(a, base) == b){ accept = true; break; }
		}
		if(!accept){
			cout << -1 << endl;
			continue;
		}
		for(++a; a <= c; ++a){
			ll d = INF;
			for(int base = 2; base <= 16; ++base){
				ll e = a, f = 0, g = 1;
				while(e > 0){
					if(e % base >= 10){ break; }
					f = f + e % base * g;
					e /= base;
					g *= 10;
				}
				if(e == 0 && f >= b){
					d = min(d, f);
				}
			}
			b = d;
			if(b == INF){ break; }
		}
		cout << b << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1

code_snippet: #include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <sstream>
#include <cstdlib>

using namespace std;

typedef long long ll;
const ll INF=(1LL<<50);

ll DecToBaseNum(ll num,int base){
  string s;
  ll res=0;
  while(num){
    stringstream ss;
    ss<<(num%base);
    if(num%base>9)return -1;
    s=ss.str()+s;
    num/=base;
  }
  for(int i=0;i<s.size();i++)res=res*10+(s[i]-'0');
  return res;
}

ll A,B,C;
int main(){
  while(cin>>A>>B>>C&&!(A==-1&&B==-1&&C==-1)){
    bool ok=false;
    for(int i=2;i<=16;i++){
      ll a=DecToBaseNum(A,i);
      if(a==B){
	ok=true;
	break;
      }
    }
    if(!ok)cout<<-1<<endl;
    else{
      ll cur=B;
      ok=true;
      for(int i=A+1;i<=C;i++){
	ll nxt=INF;
	for(int j=2;j<=16;j++){
	  ll a=DecToBaseNum(i,j);
	  if(a>=cur&&nxt>a)nxt=a;
	}
	if(nxt==INF){
	  ok=false;
	  break;
	}
	cur=nxt;
      }
      if(!ok)cout<<-1<<endl;
      else cout<<cur<<endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 400
memory_kb: 944
final_rank: 9