Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define V vector
  4. using LL = long long;
  5. using ULL = unsigned long long;
  6. #define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
  7. #define REP(i, n) FOR(i, 0, (n) - 1)
  8. template<class T> int size(T &&x) {
  9. return int(x.size());
  10. }
  11. template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
  12. return out << '(' << p.first << ", " << p.second << ')';
  13. }
  14. template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
  15. out << '{';
  16. for(auto it = x.begin(); it != x.end(); ++it)
  17. out << *it << (it == prev(x.end()) ? "" : ", ");
  18. return out << '}';
  19. }
  20. void dump() {}
  21. template<class T, class... Args> void dump(T &&x, Args... args) {
  22. cerr << x << "; ";
  23. dump(args...);
  24. }
  25. #ifdef DEBUG
  26. struct Nl{~Nl(){cerr << '\n';}};
  27. # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << ""
  28. #else
  29. # define debug(x...) 0 && cerr
  30. #endif
  31. mt19937_64 rng(0);
  32. int rd(int l, int r) {
  33. return uniform_int_distribution<int>(l, r)(rng);
  34. }
  35.  
  36. int main() {
  37. ios_base::sync_with_stdio(0);
  38. cin.tie(0);
  39.  
  40. int t; cin >> t;
  41.  
  42. V<ULL> power = {1ll};
  43. REP(i, 18)
  44. power.emplace_back(power.back() * 10ull);
  45.  
  46. auto dig = [&](ULL n) {
  47. REP(i, 19)
  48. if(power[i] > n)
  49. return i;
  50. };
  51.  
  52. auto rev = [&](ULL n) {
  53. int d = dig(n);
  54. V<ULL> digits;
  55. while(n) {
  56. digits.emplace_back(n % 10);
  57. n /= 10;
  58. }
  59. //reverse(digits.begin(), digits.end());
  60. ULL x = 0;
  61. REP(i, d)
  62. x += power[d - i - 1] * digits[i];
  63. return make_pair(x, digits);
  64. };
  65.  
  66. REP(test, t) {
  67. ULL n; cin >> n;
  68. int d = dig(n) / 2;
  69. ULL x = n / power[dig(n) - d];
  70. ULL y = n % power[d];
  71.  
  72. if(n + 1 == power[dig(n)]) {
  73. cout << "1";
  74. REP(i, dig(n) - 1)
  75. cout << "0";
  76. cout << "1\n";
  77. }
  78.  
  79. else if(y < rev(x).first) {
  80. // jest dobrze
  81. cout << x;
  82. if(dig(n) & 1)
  83. cout << ((n / power[d]) % 10);
  84. V<ULL> v = rev(x).second;
  85. for(auto &q : v)
  86. cout << q;
  87. cout << '\n';
  88. }
  89. else {
  90. if(dig(n) & 1) {
  91. int mid = ((n / power[d]) % 10);
  92. if(mid < 9) {
  93. mid++;
  94. cout << x << mid;
  95. V<ULL> v = rev(x).second;
  96. for(auto &q : v)
  97. cout << q;
  98. cout << '\n';
  99. }
  100. else {
  101. cout << x + 1 << "0";
  102. V<ULL> v = rev(x + 1).second;
  103. for(auto &q : v)
  104. cout << q;
  105. cout << '\n';
  106. }
  107. }
  108. else{
  109. cout << x + 1;
  110. V<ULL> v = rev(x + 1).second;
  111. for(auto &q : v)
  112. cout << q;
  113. cout << '\n';
  114. }
  115. }
  116. }
  117.  
  118. return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement