Advertisement
7oSkaaa

F

Feb 24th, 2023
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define fixed(n) fixed << setprecision(n)
  6. #define ceil(n, m) (((n) + (m) - 1) / (m))
  7. #define add_mod(a, b, m) (((a % m) + (b % m)) % m)
  8. #define sub_mod(a, b, m) (((a % m) - (b % m) + m) % m)
  9. #define mul_mod(a, b, m) (((a % m) * (b % m)) % m)
  10. #define all(vec) vec.begin(), vec.end()
  11. #define rall(vec) vec.rbegin(), vec.rend()
  12. #define sz(x) int(x.size())
  13. #define debug(x) cout << #x << ": " << (x) << "\n";
  14. #define fi first
  15. #define se second
  16. #define ll long long
  17. #define ull unsigned long long
  18. #define EPS 1e-9
  19. constexpr int INF = 1 << 30, Mod = 1e9 + 7;
  20. constexpr ll LINF = 1LL << 62;
  21. #define PI acos(-1)
  22. template < typename T = int > using Pair = pair < T, T >;
  23. vector < string > RET = {"NO", "YES"};
  24.  
  25. template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
  26.     for (auto &x : v) in >> x;
  27.     return in;
  28. }
  29.  
  30. template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
  31.     for (const T &x : v) out << x << ' ';
  32.     return out;
  33. }
  34.  
  35. bool is_all_nine(vector < int >& num, int n){
  36.     for(int i = 0; i < sz(num); ++i)
  37.         if (num[i] != 9)
  38.             return false;
  39.     return true;
  40. }
  41.  
  42. void get_next_palindrome (vector < int >& num){
  43.     int n = sz(num), mid = n / 2;
  44.     int i = mid - 1, j = (n % 2) ? mid + 1 : mid;
  45.     bool leftsmaller = false;
  46.     while (i >= 0 && num[i] == num[j])
  47.         i--, j++;
  48.     if (i < 0 || num[i] < num[j])
  49.         leftsmaller = true;
  50.     while (i >= 0){
  51.         num[j++] = num[i--];
  52.     }
  53.     if (leftsmaller == true){
  54.         int carry = 1;
  55.         i = mid - 1;
  56.         if (n % 2 == 1){
  57.             num[mid] += carry;
  58.             carry = num[mid] / 10;
  59.             num[mid] %= 10;
  60.             j = mid + 1;
  61.         }
  62.         else
  63.             j = mid;
  64.         while (i >= 0){
  65.             num[i] += carry;
  66.             carry = num[i] / 10;
  67.             num[i] %= 10;            
  68.             num[j++] = num[i--];
  69.         }
  70.     }
  71. }
  72.  
  73. void Solve(){
  74.     string s;
  75.     cin >> s;
  76.     vector < int > num;
  77.     for(auto &c : s)
  78.         num.push_back(c - '0');
  79.     if (is_all_nine(num, sz(num))){
  80.         cout << 1;
  81.         for(int i = 1; i < sz(num); ++i)
  82.             cout << 0;
  83.         cout << 1 << '\n';
  84.     }
  85.     else{
  86.         get_next_palindrome(num);
  87.         for(auto &x : num)
  88.             cout << x;
  89.         cout << '\n';
  90.     }
  91. }
  92.  
  93. int main(){
  94.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  95.     int test_cases = 1;
  96.     // cin >> test_cases;
  97.     for(int tc = 1; tc <= test_cases; tc++){
  98.         // cout << "Case #" << tc << ": ";
  99.         Solve();
  100.     }
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement