cses

solution to cses exercise problems
git clone git://git.superpozycja.net/cses
Log | Files | Refs | README

digit_queries.cpp (796B)


      1 #include <bits/stdc++.h>
      2 
      3 using namespace std;
      4 
      5 using ui = unsigned int;
      6 using l = long;
      7 using ul = unsigned long;
      8 using ll = long long;
      9 using ull = unsigned long long;
     10 
     11 using vi = vector<int>;
     12 using vui = vector<ui>;
     13 using vl = vector<l>;
     14 using vul = vector<ul>;
     15 using vll = vector<ll>;
     16 using vull = vector<ull>;
     17 
     18 void solve()
     19 {
     20 	ll k;
     21 	cin >> k;
     22 	if (k < 10) {
     23 		cout << k << "\n";
     24 		return;
     25 	}
     26 
     27 	ll den = 90;
     28 	ll n = 2;
     29 	ll base = 10;
     30 	ll range = 9;
     31 	while (k > range) {
     32 		ll new_range = range + den * n;
     33 		if (k <= new_range) 
     34 			break;
     35 		den *= 10;
     36 		n++;
     37 		base *= 10;
     38 		range = new_range;
     39 	}
     40 
     41 	k -= range + 1;
     42 	ll dig = k % n;
     43 	k /= n;
     44 	ll res = base + k;
     45 	for (ll i = n-1; i > dig; i--)
     46 		res /= 10;
     47 	cout << res % 10 << "\n";
     48 }
     49 
     50 int main()
     51 {
     52 	int t;
     53 	cin >> t;
     54 	while (t--)
     55 		solve();
     56 }