Hint
Try to characterize even-length palindrome numbers.
Solution
For simplifications, in the following solution we define lovely integer as an even-length positive palindrome number.
An even-length positive integer is lovely if and only if the first half of its digits is equal to the reverse of the second half.
So if a and b are two different 2k-digit lovely numbers, then the first k digits of a and b differ in at least one position.
So a is smaller than b if and only if the first half of a is smaller than the the first half of b.
Another useful fact: The first half of a a lovely number can be any arbitrary positive integer.
Using the above facts, it's easy to find the first half of the n-th lovely number — it exactly equals to integer n. When we know the first half of a lovely number, we can concatenate it with its reverse to restore the lovely integer. To sum up, the answer can be made by concatenating n and it's reverse together.
The complexity of this solution is O(log n).
C++ code
// . .. ... .... ..... be name khoda ..... .... ... .. . \\ #include <bits/stdc++.h> using namespace std; inline int in() { int x; scanf("%d", &x); return x; } const long long N = 2002; int main() { string s; cin >> s; cout << s; reverse(s.begin(), s.end()); cout << s << endl; }
Python code
s = raw_input() print s + ''.join(reversed(s))