Champernown constant is an irrational number represented in decimal by "0." followed by concatenation of all positive integers in the increasing order. The first few digits of this constant are: 0.123456789101112...
Your task is to write a program that outputs the K digits of Chapnernown constant starting at the N-th place for given two natural numbers K and N.
Input
The input has multiple lines. Each line has two positive integers N and K (N ≤ 109, K ≤ 100) separated by a space.
The end of input is indicated by a line with two zeros. This line should not be processed.
Output
For each line, output a line that contains the K digits.
Example
Input
4 5
6 7
0 0
Output
45678
6789101
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to read lines with N and K, where N ≤ 1e9, K ≤ 100. For each, output K digits from the Champernowne constant starting at position N (presumably 1-indexed positions after the decimal point). That is, the constant is 0.123456789101112131415..., and positions: position 1 = 1, position 2 = 2, ..., position 9 = 9, position 10 = 1 (of 10), position 11 = 0, etc. So we need to extract a substring of length K from the infinite Champernowne string.
N up to 1e9, but K is small (≤100). So we need to compute the digits efficiently without generating huge string. Standard approach: find which number the Nth digit belongs to, then extract digits from numbers accordingly.
Let’s define:
For each group of numbers with d digits (d=1 for 1-9, d=2 for 10-99, d=3 for 100-999, etc.):
Count of numbers with d digits: 9 * 10^{d-1}
Total digits contributed: d * 9 * 10^{d-1}
We need to locate the number containing the Nth digit.
Procedure for a given start position N and length K (K <= 100):
Find the digit index within Champernowne: we need to output digits from position N to N+K-1 inclusive.
Given that K is at most 100, we can maybe just simulate building the substring by moving forward digit by digit, but N can be up to 1e9, so we need a way to jump.
Simpler: Use a function to get the digit at a given position i (1-indexed). Then for each position from N to N+K-1, compute digit individually. Since K ≤ 100 and each digit computation is O(log N) or O(d) (number of digits), that's fine. But we need to be careful: N up to 1e9, computing each digit individually might involve finding which number it's in, which can be done in O(d) by scanning digit groups. Since d is at most 10 (since numbers up to around 1e9: the maximum number to consider maybe up to around 1e9/1? Actually the numbers go beyond 1e9? Let's see: position N up to 1e9, the numbers might be up to maybe 1e9? Actually the total digits up to numbers with d digits: sum_{d=1 to m} d * 9 * 10^{d-1}. For m=9 (numbers up to 9 digits, i.e., up to 999,999,999), total digits: sum_{d=1}^{9} d910^{d-1}. Compute:
d=1:9
2:180 -> 189
3:2700 -> 2889
4:36000 -> 38889
5:450000 -> 488889
6:5400000 -> 5,888,889
7:63,000,000? Wait compute: d=7: 7910^6 = 79,000,000=63,000,000? Actually 910^{6}=9,000,000; times 7 =63,000,000. total: 63,000,000 + previous 5,888,889 = 68,888,889.
d=8: 8910^{7}=890,000,000=720,000,000; total = 720M+68,888,889 = 788,888,889.
d=9: 9910^{8}=9900,000,000=8,100,000,000; total = 8,100,000,000+788,888,889 = 8,888,888,889.
So position N=1e9 is within 9-digit numbers (since up to 8 digits gives 788,888,889 digits, and adding 9-digit numbers). So numbers up to 9 digits (i.e., up to 999,999,999) cover positions up to about 8.888e9, which is > 1e9. So we don't need to go beyond 9-digit numbers. So digit groups up to d=9 suffice. So we can compute in O(number of digit lengths) per query, which is constant (<10). So per digit we can compute in O(10) = O(1). For K up to 100, total operations per line are ~1000, which is fine.
But we must also consider that we might need to extract multiple digits from the same number; but computing each digit separately might be repeated work. However it's fine because K is small.
Alternatively, we can implement a function that, given N, returns the number and offset and then we can stream digits from that number onward. But because K is small, it's easier to compute each digit individually.
Thus plan: For each line (N,K) not (0,0):
Initialize an empty string result.
For i from N to N+K-1:
Find digit at position i.
Append to result.
Output result.
We need a function get_digit(pos) that returns the character (digit) at that 1-indexed position in Champernowne.
Algorithm get_digit(pos):
Set d = 1 (digit length of current number group)
While pos > d * 9 * 10^{d-1}:
pos -= d * 9 * 10^{d-1}
d += 1
Now we are within the d-digit numbers.
Let offset = pos - 1 (0-indexed within this group).
The index of the number within this group: num_index = offset // d (0-indexed). So the actual number = first_digit_start + num_index, where first_digit_start = 10^{d-1} (the smallest d-digit number: for d=1 it's 1, but 10^{0}=1, yes).
The digit position within that number: digit_offset = offset % d. (0-indexed from leftmost digit)
So number = 10^{d-1} + num_index.
Extract the digit at position digit_offset (0-indexed) of the decimal representation of number. Convert number to string (or compute digit via division) and get that digit as char.
But careful: For d=1, first_digit_start = 1, count = 9. Works.
Now about pos: pos is integer up to 1e9. The while loop subtracts values for each d. Since d max is 9, loop runs at most 9 times.
Edge Cases: When pos exactly at boundary? The while condition >? Let's test: Suppose pos=9 (last digit of 1-digit numbers). For d=1, total digits=9. While pos > 9? No, 9 > 9 false, so stay in d=1. offset = pos-1=8. num_index = 8//1=8, number = 1+8=9, digit_offset=0? Actually 8%1=0, so the digit at offset 0 of "9" is '9'. Works.
For pos=10: d=1, pos>9? yes (10>9). So subtract 9 => pos=1, d=2. Now pos=1 <= d910^{d-1}=2910=180. offset = pos-1=0, num_index=0//2=0, number=10^{1}+0=10, digit_offset=0 -> first digit of 10 is '1'. So position 10 is '1'. Good.
Now for get_digit, careful: after while loop, pos is the position within the d-digit block, starting from 1. So pos is from 1 to countd where count = 910^{d-1}. So offset = pos - 1 (0-indexed). Yes.
Now implement extracting digit: we can convert number to string via to_string and then take char at digit_offset. But since K <=100, but we may call get_digit up to 100 times per line, converting number to string each time is fine (number up to 10^9). Alternatively, compute digit by integer math: we can get the digit from leftmost? Actually digit_offset from leftmost: For a d-digit number, the leftmost digit is the most significant. To extract the digit at position digit_offset (0-index from left), we can do: divide by 10^{d-1-digit_offset} then mod 10. Specifically, let divisor = 10^{d-1-digit_offset}; digit = (number / divisor) % 10. But need to compute powers of 10. That's also fine. Using string is simpler.
But note: if we repeatedly convert the same number to string for multiple positions, it may be inefficient but K small, it's okay.
Potential optimization: compute get_digit for consecutive positions might reuse info. But not needed.
Now we need to be careful about N up to 10^9. In while loop we compute d * 9 * 10^{d-1}. For d=9, this value is 9910^8 = 81*10^8? Actually compute: 9 * 9 * 10^{8} = 81 * 10^8 = 8,100,000,000, which fits in 64-bit (less than 2^63). For d=10, if we ever need it? But pos max is 1e9, so d will not exceed 9 because total digits up to 9-digit numbers is about 8.9e9 > 1e9, so we won't go to d=10. But for safety, we can loop until d=10 maybe but it's fine.
Now handling K up to 100: we just output K digits as a continuous string.
Potential issue: N+K-1 could exceed the maximum digits we consider? But Champernowne is infinite, so there is always digits. So no problem.
Now implement reading until "0 0". Output each result line.
Check example: Input: 4 5 -> positions 4,5,6,7,8? Wait K=5 means positions N to N+K-1 inclusive: N=4, K=5 => positions 4,5,6,7,8? Actually K=5 means 5 digits: positions 4,5,6,7,8? That would be 5 digits: indices 4-8 inclusive. But output shown: "45678". That corresponds to positions 4-8: digits at pos4=4, pos5=5, pos6=6, pos7=7, pos8=8. Yes.
Use 64-bit integers (long long) for intermediate calculations because numbers can exceed 2^31 (up to 8.1e9). So use long long.
In while condition: while (pos > (long long)d * 9 * pow10[d-1]) where pow10[d-1] is 10^(d-1). Compute pow10 as an array.
Be careful with overflow when computing d * 9 * pow10[d-1] if using int.
Also careful: after subtracting, pos becomes a smaller number, but still up to around 180 million for d=9? Actually after subtracting, pos will be between 1 and 180 million? Let's compute: for d=9, block size is 9910^8=8.1e9. But initial pos <=1e9, so after subtracting up to d=8 total=788,888,889, pos may be up to about 211,111,111? Actually 1e9 - 788,888,889 = 211,111,111, which is < 8.1e9. So safe for 32-bit int? 211 million fits in int. But for safety use long long.
Now writing get_digit(pos):
Pseudo:
long long pow10[10]; // precompute pow10[i] = 10^i for i=0..9
char get_digit(long long pos) {
int d = 1;
long long block_len;
while ( (block_len = d * 9LL * pow10[d-1]) < pos ) {
pos -= block_len;
d++;
}
// now pos is 1-indexed within d-digit numbers
long long offset = pos - 1; // 0-indexed
long long num_index = offset / d; // which number (0-indexed)
long long number = pow10[d-1] + num_index;
int digit_offset = offset % d; // 0-indexed from left
// extract digit
// convert to string:
string s = to_string(number);
return s[digit_offset];
}
But we need to be careful: to_string(number) returns a string of length d. That's fine.
Alternative: compute digit mathematically:
int divisor = pow10[d-1-digit_offset];
int digit = (number / divisor) % 10;
return '0' + digit;
This avoids string conversion, which is simpler? But need to compute pow10 array up to maybe 10.
We'll precompute pow10 as long long array of size 10 or 11.
Let's implement with math to avoid string overhead. But careful: number can be up to 10^9, pow10 up to 10^9, fits in long long.
Implementation:
long long pow10[10];
pow10[0] = 1;
for (int i = 1; i < 10; i++) pow10[i] = pow10[i-1] * 10;
Then in get_digit:
int d = 1;
while (pos > d * 9LL * pow10[d-1]) {
pos -= d * 9LL * pow10[d-1];
d++;
}
long long offset = pos - 1;
long long num_index = offset / d;
long long number = pow10[d-1] + num_index;
int digit_offset = offset % d;
int digit = (int)(number / pow10[d-1-digit_offset]) % 10;
return '0' + digit;
But wait: For d=1, pow10[0] = 1, d-1-digit_offset = 0-digit_offset: offset is 0 to 8, digit_offset=0? Actually for d=1, pow10[0] =1. d-1-digit_offset = 0 - digit_offset? That's negative if digit_offset>0? Because d-1-digit_offset = 0 - digit_offset, which is negative if digit_offset>0. That's wrong! For d=1, the only digit is the number itself (number from 1 to 9). We want to extract the digit at offset 0 always because there's only one digit. Indeed, for d=1, offset % d always 0 (since any integer mod 1 = 0). So digit_offset is always 0. So we can treat d=1 as a special case? Actually the formula (number / pow10[d-1-digit_offset]) % 10 requires that exponent is non-negative. For d=1, d-1-digit_offset = 0 - 0 = 0, which is fine. So it works because digit_offset always 0 for d=1. For d>1, digit_offset ranges from 0 to d-1, so d-1-digit_offset ranges from d-1 to 0, all non-negative. So the formula works as long as we ensure that digit_offset is in [0, d-1] and we compute pow10[d-1-digit_offset]. For d=1, d-1-digit_offset = 0 - 0 = 0, fine. If by some reason digit_offset could be non-zero when d=1? Since d=1, offset % d is always 0. So correct.
But careful: pow10 array indexing: pow10[d-1-digit_offset] where d-1-digit_offset may be up to 9. For d=9, digit_offset can be 0, then exponent = 8, which is fine (pow10[8]=10^8). For digit_offset=8, exponent=0, pow10[0]=1. So need pow10 up to index 9? Actually max exponent is d-1, which for d=9 is 8, so pow10[8] needed. But d can be 9 max; but if we had d=10 (though not used), exponent up to 9. But we can compute up to 10 to be safe.
We'll precompute up to 10 or 12.
Now implement main loop:
while (true) {
read N, K (int).
if (N==0 && K==0) break;
string ans;
for (int i = 0; i < K; i++) {
ans += get_digit(N + i);
}
cout << ans << endl;
}
Complexities: O(K * num_groups) per query. Fine.
But careful: get_digit uses while loop that subtracts block lengths based on pos. Since we call get_digit for each consecutive pos, we could reuse state to avoid recomputation, but not necessary.
One nuance: K up to 100, but N up to 1e9, and we may call get_digit for pos up to N+99, which may cross block boundaries. That's okay.
Double-check the computation for positions near boundaries.
Test manually for pos=188? Let's compute:
d=1:9 digits
d=2:180 digits
Total up to 2-digit numbers: 189. So pos=190 would be in 3-digit numbers.
Our while condition: while (pos > d * 9 * 10^{d-1}) . At d=2, 2910=180. For pos=180? If pos=180, then condition: pos > 180? false, so stays d=2. offset=179, num_index=179//2=89, digit_offset=179%2=1. first 2-digit number=10, so number=10+89=99, digit_offset=1 (0-index from left) -> digits: 9 (since '99' digits: index0='9', index1='9') -> returns '9'. But what is the actual 180th digit? Let's check: positions:
1-9: 1-9
10-189: numbers 10-99, each 2 digits. So total digits from 10-99: 90 numbers 2 =180 digits. Starting position 10 corresponds to first digit of 10. So positions:
10:'1',11:'0',
12:'1',13:'1' (of 11? Actually 11: digits: '1','1'),
...
So the last two digits of 99 would be at positions 188 and 189? Let's compute: number 10 starts at pos10, each number 2 digits. For number n from 10 to 99, its position start = 10 + 2(n-10). So number 99: start = 10 + 2*(89) = 10+178 = 188. So digits: pos188='9', pos189='9'. So the 180th digit (pos190?) Actually 180th digit is pos180, not 180. Wait careful: total digits from 10-99 is 180, covering positions 10 to 189. So pos180 is within that. Compute: pos10 is first, pos11 second, so pos180 = start of number? Let's compute offset: pos180 is offset = pos - 10 = 170 (if pos10 is index 0). Actually easier: The block of 2-digit numbers: positions 10 to 189 inclusive. For a given position p in [10,189], let local = p - 10 (0-index). Then num_index = local // 2, digit_offset = local % 2. For p=180: local=170, num_index=85, digit_offset=0. So number = 10+85=95, first digit of 95 = '9'. So indeed pos180 = '9'. For p=181: local=171, num_index=85, digit_offset=1 -> second digit of 95 = '5'. So our algorithm: For p=180, while: d=1, pos=180>9 -> pos=171; d=2, now pos=171 <= 180? Actually after subtract d=1, pos becomes 180-9=171. Then d=2, check while pos > 180? 171>180 false. Then offset = pos-1 = 170? Wait careful: In our algorithm after while, we have d=2 and pos is the position within the d-digit block? But our while subtracts d=1 block and sets pos=171. Then we treat pos as 1-indexed within 2-digit numbers? The block of 2-digit numbers has length 180 (positions 10-189). But after subtracting 9, we get pos=171, meaning the 171st digit within the 2-digit block? Actually if we treat the 2-digit block as starting at position 1 (i.e., relative), then the absolute position is 10 + (relative -1). So we need to be consistent.
Let's run through the algorithm for p=180:
Start d=1, block_len = 9. Since p=180 > 9, subtract 9 => p=171, d=2.
Now p=171, check while p > block_len for d=2: block_len = 2910^{1}=180. Is 171 > 180? No. So exit.
Now we have d=2, p=171 (relative position within d-digit numbers, starting from 1). So offset = p - 1 = 170.
num_index = offset/d = 170/2 = 85.
number = 10^{d-1} + num_index = 10^{1} + 85 = 10+85=95.
digit_offset = offset % d = 170 % 2 = 0.
digit = (number / 10^{d-1-digit_offset}) % 10 = (95 / 10^{1-0}) % 10? Actually d-1-digit_offset = 1-0 =1, 10^1=10, 95/10=9, %10=9 => '9'. Good.
So correct.
Now test for p=189: absolute last digit of 2-digit block. Subtract 9 => p=180? Wait 189-9=180. Then d=2, p=180, block_len=180, condition p > 180? false. So p=180 relative. offset=179, num_index=179//2=89, number=10+89=99, digit_offset=179%2=1 => second digit of 99 = '9'. But absolute pos 189 should be second digit of 99 = '9'. Good. But note that p=180 relative corresponds to offset=179. That gives number=99, digit_offset=1, ok.
Now for p=190: Subtract 9 -> 181, d=2. While? 181 > 180 true, so subtract 180 => p=1, d=3. Then offset=0, num_index=0, number=100, digit_offset=0 => '1'. Indeed pos190 is first digit of 100. Good.
Thus algorithm works.
Now edge case: N=1, K=1 -> output "1". Fine.
Now we need to be mindful of integer overflow when computing d * 9 * pow10[d-1]. For d up to 9, it's 9910^8=8.1e9, which fits in 64-bit signed (max 9e18). For d=10, it would be 10910^9 = 90e9 = 9e10, still fits. But we won't need d=10 because pos up to 1e9. However if N=1e9, then after subtracting up to d=8 total 788,888,889, pos remaining = 211,111,111, which is less than block_len for d=9 (8.1e9), so d=9. So we will not go to d=10. So safe.
Now one more detail: In while condition, we compare pos > block_len. That's correct for moving to next digit length only when pos is strictly greater than block_len. If pos is exactly block_len, we stay in current d. That's correct because if pos equals the total digits of current group, that position is the last digit of the group. For example, pos=9 -> block_len=9, pos>9 false, stays d=1, correct. For pos=189? Let's test: at d=1, pos=189 >9 => pos=180; d=2. Now pos=180, block_len=180, pos>180 false, stay d=2. That's correct because pos=180 relative corresponds to last digit? Actually absolute pos=189 corresponds to relative pos=180, which is the last position of the 2-digit block? Since block length is 180, relative positions 1..180, 180 is last. So yes, we want to stay. So condition should be > not >=.
Now we need to output exactly K digits, no spaces.
Now implement.
Potential issue: The loop inside get_digit uses variable pos (passed by value). It modifies pos. That's fine.
Now I'd like to test with some random small values by writing a small generator for the constant up to certain length and compare. But in analysis, we can reason.
Now consider a case crossing boundaries: N=9, K=3 -> positions 9,10,11 => "910". Our algorithm: get_digit(9) => '9', get_digit(10) => '1', get_digit(11) => '0' => "910". Good.
Now consider N=188, K=4 -> positions 188,189,190,191 => should be "9 9 1 0"? Actually from earlier: pos188='9', pos189='9', pos190='1', pos191='0' => "9910". Our algorithm should produce that.
Now test for N large: e.g., N=1000000000, K=1. Need to compute digit at position 1e9. Let's compute roughly: total digits up to 8-digit numbers: 788,888,889. So pos 1e9 - 788,888,889 = 211,111,111 within 9-digit numbers. First 9-digit number is 100,000,000. The 9-digit numbers each have 9 digits. So the number index = (211,111,111 - 1) // 9 = 211,111,110 // 9 = 23,456,790? Let's compute: 9 * 23,456,790 = 211,111,110, remainder 0, so number = 100,000,000 + 23,456,790 = 123,456,790? Actually 100,000,000 + 23,456,790 = 123,456,790. And offset 0 means first digit of that number, which is '1'. So digit at position 1e9 is '1'. But we can trust algorithm.
Now implement.
But careful: when using long long for pos, and we pass N + i which may be up to 1e9+99, which fits in 32-bit signed (2.147e9) but safer as long long.
Now code skeleton:
#include
#include
#include
using namespace std;
long long pow10[10];
void init_pow10() {
pow10[0] = 1;
for (int i = 1; i < 10; i++) {
pow10[i] = pow10[i-1] * 10;
}
}
char get_digit(long long pos) {
int d = 1;
while (pos > d * 9LL * pow10[d-1]) {
pos -= d * 9LL * pow10[d-1];
d++;
}
// pos is 1-indexed within d-digit numbers
long long offset = pos - 1;
long long num_index = offset / d;
long long number = pow10[d-1] + num_index;
int digit_offset = offset % d; // 0-index from left
int digit = (number / pow10[d-1-digit_offset]) % 10;
return '0' + digit;
}
int main() {
init_pow10();
int N, K;
while (cin >> N >> K && (N != 0 || K != 0)) {
string ans;
ans.reserve(K);
for (int i = 0; i < K; ++i) {
ans += get_digit(N + i);
}
cout << ans << endl;
}
return 0;
}
Test with sample.
Potential pitfalls: The pow10 array for d-1-digit_offset when d=1 and digit_offset=0 gives exponent 0; okay.
One more nuance: pow10[d-1-digit_offset] uses index that may be negative? For d=1, d-1-digit_offset = 0, okay. For d>1, the exponent is between 0 and d-1 inclusive. All indices are within 0..9, safe.
Now let's test some edge cases.
Case: N=1, K=100: Should output first 100 digits: "1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950..." Let's compute small: after 9 digits: "123456789", then "10" gives digits 10,11, so total 11 digits. Continue to 100 digits. Our algorithm should produce correctly. It may be slow? 100 calls, each while loop up to 9 iterations, so 900 iterations, fine.
Case: N=1000000000, K=100: That's okay.
Now verify for large N crossing into 10-digit numbers? Since N max 1e9, we might get close to 10-digit numbers? Actually as computed, the 9-digit block extends up to position ~8,888,888,889, so 1e9 is within 9-digit numbers. So no need to handle 10-digit. However if K=100 and N=1e9, then the maximum position is 1,000,000,099, still well within 9-digit numbers. So safe.
But what if N is given as 10^9? It's within range. So no need for more digits.
Now potential bug: For d=9, pow10[8] = 100,000,000. That's fine. However, note that number = pow10[d-1] + num_index. For d=9, pow10[8]=100,000,000, num_index could be up to 9*10^8 - 1 = 899,999,999? Actually count of 9-digit numbers = 9 * 10^8 = 900,000,000. So num_index max = 899,999,999, then number = 999,999,999. That fits in 32-bit? 999,999,999 < 2^31-1=2,147,483,647, so int is fine. But we used long long for safety.
Now digit extraction: number / pow10[d-1-digit_offset] could be large but within long long.
Everything seems fine.
One more nuance: The while condition uses block_len = d * 9 * pow10[d-1]; but pow10[d-1] for d=1 is pow10[0]=1. So block_len = 191=9. Good.
Now consider if N=0? The problem says positive integers N and K, and line with 0 0 stops. So N>=1. So we don't need to handle 0 as start.
Now handle multiple lines.
Now output exactly K digits without newline except at end of line.
Now code in C++.
But before finalizing, consider potential issues with integer types: In the while condition, we compare pos (long long) > d * 9LL * pow10[d-1]. Since pow10[d-1] is long long, d is int, 9LL ensures long long multiplication. So all good.
Now confirm for N=999,999,999? Actually N max is 1e9, so it's okay.
Now ensure we handle K up to 100 and output string.
Now consider the possibility that N+K-1 might exceed the range we handle with 32-bit, but 1e9+100 < 2,147,483,647, so safe.
Now final code.
One more nuance: If we use get_digit for each i, we could also implement a more efficient method that finds the starting number and then extracts sequential digits, but K small.
Now test with some random values using a brute force generator for small positions.
Let's simulate mentally a few:
N=10, K=5 => positions 10,11,12,13,14 => digits: 10->1,11->0,12->1,13->1,14->1? Actually 12 corresponds to first digit of 11, which is 1, 13 is second digit of 11 = 1, 14 is first digit of 12 = 1? Let's list: 10: '1', 11: '0', 12: '1' (of 11), 13: '1' (of 11), 14: '1' (of 12), 15: '2' (of 12). So 10-14 gives "10111". Let's test with our algorithm: get_digit(10) -> '1', 11->'0', 12->'1', 13->'1', 14->'1'. So "10111". Good.
Now ensure that for N large and crossing number boundaries, extraction works.
Now consider if we need to handle cases where K=0? The problem says K positive, so K>=1.
Now consider reading until 0 0. Should stop.
Now done.
But wait, the problem statement says "Champernown constant" and "Chapnernown constant"? It's Champernowne. But the constant is as described. The example output seems correct.
Now finalize.
However, I'd like to check if there is any off-by-one in the while condition when pos is exactly at the total digits of all groups before d? Actually our algorithm as described works. Let's test with pos=1: d=1, block_len=9, pos>9 false, offset=0, number=1, digit='1'. Good.
Now test pos=9: offset=8, number=9, digit='9'.
pos=10: as above, works.
Now test pos=189: as above, works.
pos=190: as above, works.
Now test pos=1000: Let's compute manually? But trust.
Now implement.
One more thought: The input may have multiple lines and large N up to 1e9, but K ≤ 100. So total input size unknown but not huge.
Now about performance: The while loop inside get_digit loops at most 9 times per call, so total operations per query ≤ 900, which is fine.
Now final code.
But I want to double-check the pow10 indexing for digit extraction: digit = (number / pow10[d-1-digit_offset]) % 10. This extracts the digit from the left, with digit_offset=0 being the most significant. However, the offset within the number from the left is indeed digit_offset. For example, number=95, d=2. For digit_offset=0, we want the tens digit: 9. pow10[d-1-digit_offset]=10^{1}=10, number/10=9, %10=9. For digit_offset=1, we want ones digit: pow10[0]=1, number/1=95, %10=5. Works.
Now confirm for d=3, number=123, digit_offset=1 (the middle): pow10[3-1-1]=pow10[1]=10, number/10=12, %10=2, correct.
Now for d=1, number=5, digit_offset=0: pow10[1-1-0]=pow10[0]=1, number/1=5, %10=5, correct.
So formula works.
Now final answer: Provide code in C++.
But careful: In the while loop, we compute block_len = d * 9 * pow10[d-1]; For d=1, pow10[0]=1. Good. For d=9, block_len = 9910^8 = 8.1e9, fits in long long.
Now finalize.
But I just realize: The problem says N ≤ 10^9, but the position indexing could be considered after the decimal point, which is what we used. So fine.
Now I'll produce final code.
But note: The example input: "4 5" and "6 7" output "45678" and "6789101". Our code yields same.
Now deliver final answer. ```cpp
#include
#include
using namespace std;
long long pow10[10]; // pow10[i] = 10^i
void init_pow10() {
pow10[0] = 1;
for (int i = 1; i < 10; ++i) {
pow10[i] = pow10[i-1] * 10;
}
}
// Returns the digit at position pos (1‑based) of Champernowne's constant
char get_digit(long long pos) {
int d = 1; // number of digits of the current block
while (pos > d * 9LL * pow10[d-1]) {
pos -= d * 9LL * pow10[d-1];
++d;
}
// now pos is 1‑based inside the block of d‑digit numbers
long long offset = pos - 1; // 0‑based inside the block
long long num_index = offset / d; // which d‑digit number (0‑based)
long long number = pow10[d-1] + num_index; // the actual integer
int digit_offset = offset % d; // 0‑based from the left
code
// extract the desired digit
int digit = (number / pow10[d-1-digit_offset]) % 10;
return '0' + digit;
}
int main() {
init_pow10();
code
int N, K;
while (cin >> N >> K && (N != 0 || K != 0)) {
string ans;
ans.reserve(K);
for (int i = 0; i < K; ++i) {
ans += get_digit(N + i);
}
cout << ans << endl;
}
return 0;