fork download
  1. #include<bits/stdc++.h>
  2. #define f1(i, n) for(int i=1;i<=n;++i)
  3. #define f0(i, n) for(int i=0;i<n;++i)
  4. #define ull unsigned long long
  5. #define ll long long
  6. #define rev(a) reverse(a.begin(),a.end())
  7. #define all(x) x.begin(),x.end()
  8. #define so(A, n) sort(A+1, A+n+1)
  9. using namespace std;
  10. const int maxn = 1e6 + 1;
  11. const int N = 5e5 + 1;
  12. const ll mod = 1e18;
  13. string minNum1(ll n) {
  14. string s = to_string(n);
  15. int num = s[0] - 48;
  16. bool check = true;
  17. int cnt = 0;
  18. for (int i = 1; i < s.size(); ++i) {
  19. if (s[i] > s[i - 1]) {
  20. check = false;
  21. break;
  22. }
  23. else if (s[i] < s[i - 1]) {
  24. break;
  25. }
  26. }
  27. if (cnt == s.size() - 1) return s;
  28. string res = "";
  29. if (num == 10) {
  30. for (int i = 0; i <= s.size(); ++i) {
  31. res += '1';
  32. }
  33. }
  34. else {
  35. for (int i = 0; i < s.size(); ++i) {
  36. res += char(s[0] + (check ? 0 : 1));
  37. }
  38. }
  39. return res;
  40. }
  41. // string minNum2(ll n){
  42.  
  43. // }
  44. bool checkL1(ll n) {
  45. string s = to_string(n);
  46. for (int i = 1; i < s.size(); ++i) {
  47. if (s[i] != s[i - 1]) return false;
  48. }
  49. return true;
  50. }
  51. bool checkL2_2(ll n) {
  52. string s = to_string(n);
  53. sort(s.begin(), s.end());
  54. if (s[0] == s[1]) {
  55. for (int i = 1; i < s.size() - 1; ++i) {
  56. if (s[i] != s[i - 1]) return false;
  57. }
  58. return true;
  59. }
  60. else {
  61. for (int i = 2; i < s.size(); ++i) {
  62. if (s[i] != s[i - 1]) return false;
  63. }
  64. return true;
  65. }
  66. return false;
  67. }
  68. bool checkL2(ll n) {
  69. if (checkL1(n)) return true;
  70. if (checkL2_2(n)) return true;
  71. return false;
  72. }
  73. ll resL2(ll n) {
  74. while (1) {
  75. if (checkL2(n)) {
  76. break;
  77. }
  78. ++n;
  79. }
  80. return n;
  81. }
  82. vector<ll> v;
  83. ll taoso(int sz) {
  84. ll res = 0;
  85. for (int i = 0; i < sz; ++i) {
  86. res = res * 10 + v[i];
  87. }
  88. return res;
  89. }
  90. vector<ll> numbers;
  91. void gen(int so_chu_so) {
  92. int n = so_chu_so;
  93. for (int i = 0; i < n; ++i) {
  94. v.push_back(0);
  95. }
  96.  
  97. for (int j = 0; j <= 9; ++j) { // iter 0->9
  98. for (int i = 0; i < n; ++i) {
  99. for (int k = 0; k < n; ++k) v[k] = j;
  100. for (int k = 1; k <= 9; ++k) {
  101. v[i] = k;
  102. ll motcaigiday = taoso(n);
  103. numbers.push_back(motcaigiday);
  104. }
  105. }
  106. }
  107.  
  108. }
  109. int main()
  110. {
  111. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  112. ll x;
  113. cin >> x;
  114. int k;
  115. cin >> k;
  116. string resL1 = minNum1(x);
  117. if (k == 0) {
  118. cout << resL1;
  119. }
  120. else {
  121. if (x <= 1e5) {
  122. while (1) {
  123. if (checkL2(x)) {
  124. break;
  125. }
  126. ++x;
  127. }
  128. ll a = stoll(resL1);
  129. cout << min(a, x);
  130. }
  131. else
  132. {
  133. gen(resL1.size());
  134. sort(numbers.begin(), numbers.end());
  135. // for(int i=0;i<numbers.size();++i) cout<<numbers[i]<<endl;
  136. vector<ll>::iterator res = lower_bound(numbers.begin(), numbers.end(), x);
  137. cout << *res;
  138. }
  139. }
  140.  
  141. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
93999999999999