fork(2) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. long long lowerBound,actualAge;
  4. long long powah(long long a,long long b){
  5. long long ans = 1;
  6. for(int i=1;i<=b;i++)
  7. ans = ans * a;
  8. return ans;
  9. }
  10. long long convert(long long base){
  11. if(actualAge < base)
  12. return actualAge;
  13. vector<int> d;
  14. long long cpy = actualAge, ans=0;
  15. while(cpy > 0){
  16. long long t = cpy % base;
  17. if(t > 9)
  18. return -1;
  19. d.push_back(t);
  20. cpy = cpy / base;
  21. }
  22. for(int i=d.size()-1;i>=0;i--){
  23. ans = ans * 10 + d[i];
  24. }
  25. return ans;
  26. }
  27.  
  28. long long conv(long long base, long long num){
  29. long long ans = 0;
  30. long long exp = 1;
  31. while(num){
  32. int last = num%10;
  33. ans+=last*exp;
  34. num/=10;
  35. exp*=base;
  36. }
  37. return ans;
  38. }
  39.  
  40. int main() {
  41.  
  42. cin>>actualAge>>lowerBound;
  43. long long ans = -1;
  44. long long ansAge = 5000000000000000000;
  45. for(long long curAge = lowerBound; curAge<=9999LL; curAge++){
  46. long long lowBase = 10;
  47. long long num_digits;
  48. if(curAge > 999)
  49. num_digits = 3;
  50. else if(curAge > 99)
  51. num_digits = 2;
  52. else if(curAge > 9)
  53. num_digits = 1;
  54. else
  55. num_digits = 0;
  56. long long high_base_order = 1;
  57. if(num_digits!=0)
  58. high_base_order = 18/num_digits;
  59. long long highBase = powah(10,high_base_order);
  60. while(lowBase<=highBase){
  61. long long midBase = (lowBase+highBase)/2;
  62. long long num = conv(midBase, curAge);
  63. if(num<0) highBase = midBase-1;
  64. if(num>actualAge) highBase = midBase-1;
  65. if(num<actualAge) lowBase = midBase+1;
  66. if(num==actualAge){
  67. if(ansAge>curAge){
  68. ansAge=curAge;
  69. ans = midBase;
  70. }
  71. break;
  72. }
  73. }
  74. }
  75. for(long long base = 10; base<=100000; base++){
  76. long long num = convert(base);
  77. if(num>=lowerBound){
  78. if(ansAge>num){
  79. ansAge=num;
  80. ans = base;
  81. }
  82. }
  83. }
  84. cout<<ans;
  85. return 0;
  86. }
Success #stdin #stdout 0.04s 3464KB
stdin
2016 100
stdout
42