cppAcademy
FROM LEARNING TO COMPETING

Intermediate.
Put your skills to the test.

Bulgarian Informatics Group D · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group D also includes challenging national-level problems. The six 2017 National tasks shared by Groups C and D use their existing permanent URLs. Years follow Arena’s competition-season labels. Source clarifications and special grader requirements are noted on affected tasks.
GROUP D2014 · MediumBig integers

Digit of Concatenated Powers

Autumn Tournament (2014)

Submission name: seq.cpp

Problem summary

Write 1, x, x², x³, ... consecutively without spaces. Find the n-th decimal digit of the resulting infinite sequence, counting from 1.

Input

Two integers x and n.

Output

The digit in position n.

Constraints

1 < x < 1000; 1 < n < 1000000.

Worked example

Original statement sample.

Input

3 9

Output

4

Solution analysis

  1. The powers quickly exceed long long, so store the current power as decimal digits in reverse order. Initially it is the one-digit number 1. Multiplying by x uses ordinary school multiplication with a carry.
  2. Keep remaining=n. If the current power has fewer than remaining digits, subtract its length and multiply it by x. Once remaining falls inside the current power, its digit is at reversed-array index size−remaining. No giant concatenated string is needed.
  3. For x=3 the blocks are 1, 3, 9, 27, 81, 243. Their cumulative lengths are 1, 2, 3, 5, 7, 10. Position 9 is the second digit of 243, namely 4. During multiplication, each digit stores product modulo 10 while the carry stores the remaining higher digits.

Why it works

Inductively the digit array represents the next exact power of x because the multiplication is standard base-10 arithmetic. Subtracting complete block lengths preserves the target position among unprocessed digits. The selected array entry therefore equals the required digit of the correct power.

Complexity: O(n) time and O(D) auxiliary space, where D is the number of digits in the final power. Each skipped power is processed once, and their digit lengths total less than n; multiplication by x<1000 adds only a constant number of carry digits.

Watch out. Do not use floating-point powers or fixed-width integers. The digit array is reversed, while positions in the statement are counted from the left. Finish propagating the carry after processing the existing digits.

Commented C++17 solution

An independently written complete program with standard headers.

#include<iostream>
#include<vector>
using namespace std;
int main(){
    int multiplier;
    int remaining;
    cin>>multiplier>>remaining;
    vector<int>digits(1,1);
    while(remaining>static_cast<int>(digits.size())){
        remaining-=static_cast<int>(digits.size());
        int carry=0;
        // Least-significant digit first makes carrying straightforward.
        for(int&digit:digits){
            int product=digit*multiplier+carry;
            digit=product%10;
            carry=product/10;
        }
        while(carry>0){
            digits.push_back(carry%10);
            carry/=10;
        }
    }
    cout<<digits[digits.size()-remaining]<<'\n';
}

Source: Arena Olimpiici ↗