TopcoderARCHIVE
Archive/Problems/SpecialSquare
Algorithm · Problem 17501

SpecialSquare

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given a positive integer N and a very small positive integer C. (C is at most 8.)

Let X be the following number: X = C * (10^N - 1)^2 / 81.

It can be shown that X is always a positive integer too.


We are interested in the (standard base-10) digits of X. You are given two indices A and B. Return the string formed by the A-th to B-th digit of X, inclusive.

(Digits of X are numbered from the left to the right, starting with 1.)

Definition

Class:
SpecialSquare
Method:
substring
Parameters:
int, int, int, int
Returns:
String
Method signature:
String substring(int N, int C, int A, int B)
(be sure your method is public)

Constraints

  • N will be between 1 and 10^9, inclusive.
  • C will be between 1 and 8, inclusive.
  • A will be between 1 and the number of digits in X, inclusive.
  • B will be between A and the number of digits in X, inclusive.
  • (B-A) will not exceed 2,500.

Examples

  1. 3
    1
    1
    5
    Returns: "12321"
    For N=3 and C=1 we have X=12321. The request is to print the digits of X starting with the first (A=1) and ending with the fifth (B=5), so we print the whole number.
  2. 3
    1
    2
    5
    Returns: "2321"
    Same number as Example 0, but now we only return digits at positions 2-5.
  3. 3
    1
    1
    4
    Returns: "1232"
    Same number as Example 0, but now we only return digits at positions 1-4.
  4. 100
    2
    7
    7
    Returns: "5"
    For N=100 and C=2 the 7-th most significant digit of X is 5.
  5. 10
    1
    9
    12
    Returns: "0098"
    Note that sometimes the digit in position A can be a zero, so the string you return may have one or more leading zeros.
  6. 5
    6
    2
    7
    Returns: "407259"
    The full X is 740,725,926. We return its second to seventh digit.
  7. 18
    3
    12
    32
    Returns: "037036962962962962962"
← All problems