Skip to content

Instantly share code, notes, and snippets.

@separation
Last active August 4, 2018 12:11
Show Gist options
  • Save separation/c85d1d0391765a358c2c1f3a1da57575 to your computer and use it in GitHub Desktop.
Save separation/c85d1d0391765a358c2c1f3a1da57575 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
using namespace std;
int to_decimal(char c)
{
if (isdigit(c))
{
return c - '0';
}
else if (isupper(c))
{
return c - 'A' + to_decimal('9') + 1;
}
else if (islower(c))
{
return c - 'a' + to_decimal('Z') + 1;
}
return 0;
}
bool verify(int base, const string& r)
{
if (r.empty()) return true;
bool is_signed = r.front() == '+' || r.front() == '-';
auto remainder = 0;
for (size_t i = is_signed ? 1 : 0; i < r.size(); i++)
{
auto num = to_decimal(r[i]);
if (num >= base) return false;
remainder = (remainder + num) % (base - 1);
}
return remainder == 0;
}
void test_case(const string& r)
{
for (int base = 2; base <= 62; base++)
{
if (verify(base, r))
{
cout << base << endl;
return;
}
}
cout << "such number is impossible!" << endl;
}
int main()
{
string r;
while (cin >> r) test_case(r);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment