fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. // 將字元轉換為對應的數值
  8. int charToValue(char c) {
  9. if (isdigit(c)) return c - '0';
  10. if (isupper(c)) return c - 'A' + 10;
  11. if (islower(c)) return c - 'a' + 36;
  12. return -1;
  13. }
  14.  
  15. int main() {
  16. string s;
  17. while (cin >> s) {
  18. long long sum = 0;
  19. int max_val = 1; // 進位制最小從 2 開始 (即最大位數值為 1)
  20.  
  21. for (char c : s) {
  22. int v = charToValue(c);
  23. if (v == -1) continue; // 跳過無效字元(如正負號)
  24.  
  25. sum += v;
  26. if (v > max_val) max_val = v;
  27. }
  28.  
  29. bool found = false;
  30. // 進位制 R 必須大於數字中出現過的最大值 max_val
  31. // 因此 R-1 至少要從 max_val 開始
  32. for (int r = max_val + 1; r <= 62; ++r) {
  33. if (sum % (r - 1) == 0) {
  34. cout << r << endl;
  35. found = true;
  36. break;
  37. }
  38. }
  39.  
  40. if (!found) {
  41. cout << "such number is impossible!" << endl;
  42. }
  43. }
  44. return 0;
  45. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty