Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define fixed(n) fixed << setprecision(n)
- #define ceil(n, m) (((n) + (m) - 1) / (m))
- #define add_mod(a, b, m) (((a % m) + (b % m)) % m)
- #define sub_mod(a, b, m) (((a % m) - (b % m) + m) % m)
- #define mul_mod(a, b, m) (((a % m) * (b % m)) % m)
- #define all(vec) vec.begin(), vec.end()
- #define rall(vec) vec.rbegin(), vec.rend()
- #define sz(x) int(x.size())
- #define debug(x) cout << #x << ": " << (x) << "\n";
- #define fi first
- #define se second
- #define ll long long
- #define ull unsigned long long
- #define EPS 1e-9
- constexpr int INF = 1 << 30, Mod = 1e9 + 7;
- constexpr ll LINF = 1LL << 62;
- #define PI acos(-1)
- template < typename T = int > using Pair = pair < T, T >;
- vector < string > RET = {"NO", "YES"};
- template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
- for (auto &x : v) in >> x;
- return in;
- }
- template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
- for (const T &x : v) out << x << ' ';
- return out;
- }
- bool is_all_nine(vector < int >& num, int n){
- for(int i = 0; i < sz(num); ++i)
- if (num[i] != 9)
- return false;
- return true;
- }
- void get_next_palindrome (vector < int >& num){
- int n = sz(num), mid = n / 2;
- int i = mid - 1, j = (n % 2) ? mid + 1 : mid;
- bool leftsmaller = false;
- while (i >= 0 && num[i] == num[j])
- i--, j++;
- if (i < 0 || num[i] < num[j])
- leftsmaller = true;
- while (i >= 0){
- num[j++] = num[i--];
- }
- if (leftsmaller == true){
- int carry = 1;
- i = mid - 1;
- if (n % 2 == 1){
- num[mid] += carry;
- carry = num[mid] / 10;
- num[mid] %= 10;
- j = mid + 1;
- }
- else
- j = mid;
- while (i >= 0){
- num[i] += carry;
- carry = num[i] / 10;
- num[i] %= 10;
- num[j++] = num[i--];
- }
- }
- }
- void Solve(){
- string s;
- cin >> s;
- vector < int > num;
- for(auto &c : s)
- num.push_back(c - '0');
- if (is_all_nine(num, sz(num))){
- cout << 1;
- for(int i = 1; i < sz(num); ++i)
- cout << 0;
- cout << 1 << '\n';
- }
- else{
- get_next_palindrome(num);
- for(auto &x : num)
- cout << x;
- cout << '\n';
- }
- }
- int main(){
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- int test_cases = 1;
- // cin >> test_cases;
- for(int tc = 1; tc <= test_cases; tc++){
- // cout << "Case #" << tc << ": ";
- Solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement