TopcoderArchive
Archive
Algorithm problem

AbsSequence

Problem statement, definition, constraints, and public examples.

Problem Statement

Let's consider an infinite sequence S of non-negative integers defined as follows:

S0 = first;
S1 = second;
Si = |Si-2 - Si-1| for all i >= 2.

You will be given Strings first and second, representing the 0-th and the 1-st elements of the sequence S, and a String[] indices, each element of which represents a non-negative integer without extra leading zeros. Return a String[] containing as many elements as indices, where the i-th element is equal to the indices[i]-th element of the sequence S (index is 0-based). No element of the return should contain extra leading zeros.

Definition

Class:
AbsSequence
Method:
getElements
Parameters:
String, String, String[]
Returns:
String[]
Method signature:
String[] getElements(String first, String second, String[] indices)
(be sure your method is public)

Constraints

  • first will represent an integer between 0 and 10^18, inclusive, with no extra leading zeros.
  • second will represent an integer between 0 and 10^18, inclusive, with no extra leading zeros.
  • indices will contain between 1 and 50 elements, inclusive.
  • Each element of indices will represent an integer between 0 and 10^18, inclusive, with no extra leading zeros.

Examples

  1. "21"
    "12"
    {"0", "1", "2", "3", "4"}
    Returns: {"21", "12", "9", "3", "6" }
    Here S0=21 and S1=12. The next three sequence elements are S2 = |21 - 12| = 9, S3 = |12 - 9| = 3 and S4 = |9 - 3| = 6.
  2. "0"
    "0"
    {"1000000000000000000"}
    Returns: {"0" }
    Here we get the sequence consisting of only zeros.
  3. "823"
    "470"
    {"3","1","31","0","8","29","57","75","8","77"}
    Returns: {"117", "470", "2", "823", "115", "87", "49", "25", "115", "23" }
  4. "710370"
    "177300"
    {"5","95","164721","418","3387","710","0","1197","19507","5848"}
    Returns: {"178470", "108270", "90", "0", "90", "90", "710370", "90", "0", "0" }
Back to all problems