TopcoderArchiveVisit Topcoder
SRM25 Jun 2013
SRM 591

PyramidSequences

Problem statement, definition, constraints, and public examples.

Problem Statement

A pyramid sequence of height X > 1 is an infinite sequence of positive integers with period 2X-2. Its first 2X-2 terms are 1, 2, ..., X-1, X, X-1, ..., 2.

You are given ints N and M. Consider two pyramid sequences A and B, A of height N and B of height M. Return the number of distinct pairs of integers (x,y) such that for some i > 0 we have x=A[i] and y=B[i].

Definition

Class:
PyramidSequences
Method:
distinctPairs
Parameters:
int, int
Returns:
long
Method signature:
long distinctPairs(int N, int M)
(be sure your method is public)

Constraints

  • N will be between 2 and 1,000,000,000, inclusive.
  • M will be between 2 and 1,000,000,000, inclusive.

Examples

  1. 3
    4
    Returns: 6
    These are the first several terms of pyramid sequences of height 3 and 4: {1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1} {1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1} We can see the following pairs: (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (3, 1). No other pairs will appear later in the sequences.
  2. 3
    5
    Returns: 5
    The sequences begin with: {1, 2, 3, 2, 1, 2, 3, 2, 1} {1, 2, 3, 4, 5, 4, 3, 2, 1}
  3. 43
    76
    Returns: 895
  4. 2
    1000000000
    Returns: 1000000000
  5. 100000
    95555
    Returns: 4777750000
Back to all problems