Iamtui1010

kthnum.cpp

Jan 2nd, 2022
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. //#include<bits/stdc++.h>
  2. #include<iostream>
  3. #include<string>
  4. #include<algorithm>
  5.  
  6. #define long long long
  7. #define nln '\n'
  8.  
  9. using namespace std;
  10.  
  11. string num2string(long num)
  12. {
  13.     string str = "";
  14.     while (num > 0)
  15.     {
  16.         str += (num%10)+'0';
  17.         num /= 10;
  18.     }
  19.     reverse(str.begin(), str.end());
  20.     return str;
  21. }
  22.  
  23. int main()
  24. {
  25.     cin.tie(0)->sync_with_stdio(0);
  26.     cout.tie(0)->sync_with_stdio(0);
  27.  
  28.     long n, k, len = 0;
  29.     cin >> n >> k;
  30.     string st1 = "", st2 = ""; 
  31.     for (long i = 1; i <= n; ++i)
  32.     {
  33.         st2 = num2string(i) + st1 + st1;
  34.         len += abs((long)st1.size()-(long)st2.size());
  35.         st1 = st2;
  36.         if ((long)st1.size() > k)
  37.             st1.erase(st1.begin()+k+1, st1.end());
  38.     }
  39.  
  40.     if (k > len)
  41.         cout << "-1" << nln;
  42.     else
  43.         cout << st2[k-1] << nln;
  44.  
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment