Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define V vector
- using LL = long long;
- using ULL = unsigned long long;
- #define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
- #define REP(i, n) FOR(i, 0, (n) - 1)
- template<class T> int size(T &&x) {
- return int(x.size());
- }
- template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
- return out << '(' << p.first << ", " << p.second << ')';
- }
- template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
- out << '{';
- for(auto it = x.begin(); it != x.end(); ++it)
- out << *it << (it == prev(x.end()) ? "" : ", ");
- return out << '}';
- }
- void dump() {}
- template<class T, class... Args> void dump(T &&x, Args... args) {
- cerr << x << "; ";
- dump(args...);
- }
- #ifdef DEBUG
- struct Nl{~Nl(){cerr << '\n';}};
- # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << ""
- #else
- # define debug(x...) 0 && cerr
- #endif
- mt19937_64 rng(0);
- int rd(int l, int r) {
- return uniform_int_distribution<int>(l, r)(rng);
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t; cin >> t;
- V<ULL> power = {1ll};
- REP(i, 18)
- power.emplace_back(power.back() * 10ull);
- auto dig = [&](ULL n) {
- REP(i, 19)
- if(power[i] > n)
- return i;
- };
- auto rev = [&](ULL n) {
- int d = dig(n);
- V<ULL> digits;
- while(n) {
- digits.emplace_back(n % 10);
- n /= 10;
- }
- //reverse(digits.begin(), digits.end());
- ULL x = 0;
- REP(i, d)
- x += power[d - i - 1] * digits[i];
- return make_pair(x, digits);
- };
- REP(test, t) {
- ULL n; cin >> n;
- int d = dig(n) / 2;
- ULL x = n / power[dig(n) - d];
- ULL y = n % power[d];
- if(n + 1 == power[dig(n)]) {
- cout << "1";
- REP(i, dig(n) - 1)
- cout << "0";
- cout << "1\n";
- }
- else if(y < rev(x).first) {
- // jest dobrze
- cout << x;
- if(dig(n) & 1)
- cout << ((n / power[d]) % 10);
- V<ULL> v = rev(x).second;
- for(auto &q : v)
- cout << q;
- cout << '\n';
- }
- else {
- if(dig(n) & 1) {
- int mid = ((n / power[d]) % 10);
- if(mid < 9) {
- mid++;
- cout << x << mid;
- V<ULL> v = rev(x).second;
- for(auto &q : v)
- cout << q;
- cout << '\n';
- }
- else {
- cout << x + 1 << "0";
- V<ULL> v = rev(x + 1).second;
- for(auto &q : v)
- cout << q;
- cout << '\n';
- }
- }
- else{
- cout << x + 1;
- V<ULL> v = rev(x + 1).second;
- for(auto &q : v)
- cout << q;
- cout << '\n';
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement