diff options
author | Jack Sangdahl <[email protected]> | 2025-02-20 09:40:31 -0700 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-02-20 09:40:31 -0700 |
commit | 91491fa92ae64f36e8277849d0943d20b82093bc (patch) | |
tree | a10a045d910f43413cf32f2ce253c35cd93652a0 /introductory_problems/digit_queries.cpp | |
parent | 5b765ce70d76f1c3e56b24694c832bd938d33816 (diff) | |
download | cses-91491fa92ae64f36e8277849d0943d20b82093bc.tar.gz cses-91491fa92ae64f36e8277849d0943d20b82093bc.zip |
2431. digit queries
Diffstat (limited to 'introductory_problems/digit_queries.cpp')
-rw-r--r-- | introductory_problems/digit_queries.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/introductory_problems/digit_queries.cpp b/introductory_problems/digit_queries.cpp new file mode 100644 index 0000000..008d3f5 --- /dev/null +++ b/introductory_problems/digit_queries.cpp @@ -0,0 +1,32 @@ +// 2431. Digit Queries +#include <bits/stdc++.h> +using namespace std; + +long long pow10(int x) { + long long res = 1; + for (int i = 0; i < x; i++) + res *= 10; + return res; +} + +int solve(long long n) { + if (n < 9) + return (int)n + 1; + int len = 1; + while (pow10(len - 1) * len * 9 < n) { + n -= 9 * pow10(len - 1) * len; + len++; + } + string s = to_string(pow10(len - 1) + n / len); + return (int)(s[n % len] - '0'); +} + +int main() { + static int q; + static long long k; + scanf("%d", &q); + for (int i = 0; i < q; i++) { + scanf("%lld", &k); + printf("%d\n", solve(k - 1)); + } +} |