Poly-polygonal Numbers

Time limit1sMemory limit128 MB

Summary
Given a set of polygonal indices and a start s, print the next five numbers that are polygonal for at least two of those indices. Input continues until n = 0.
Level

Medium7 of 10

Topics
Math, Number theory, Brute force, Implementation
Solved
No attempts yet

Problem

A polygonal number is a number that can be represented by a regular arrangement of equally spaced points forming a regular polygon. Some examples are shown below.

The first figure shows the first four triangular numbers 1, 3, 6, 10. The next three show the first four square, pentagonal, and hexagonal numbers, respectively. In general, the kk-gonal numbers are those whose points form a regular kk-gon (so triangular numbers are 3-gonal, square numbers are 4-gonal, and so on). We call kk the index of the polygonal number. The mm-th kk-gonal number is given by

P(k,m)=(k−2)m2−(k−4)m2P(k, m) = \frac{(k-2)m^2 - (k-4)m}{2}

In this problem you must find numbers that are kk-gonal for two or more values of kk. We call such numbers poly-polygonal.

Input

The input consists of several problem instances. Each instance consists of three lines.

  • The first line contains a non-negative integer nn (n≤50n \le 50), the number of kinds of polygonal numbers of interest in this instance.
  • The second line contains nn integers, the indices of those polygonal numbers. They are all distinct and given in increasing order, and each index kk satisfies 3≤k≤10003 \le k \le 1000. (This line may be longer than 80 characters.)
  • The third line contains a single positive integer ss (s≤10000s \le 10000), the starting point for the search for poly-polygonal numbers.

A value of n=0n = 0 terminates the input.

Output

For each problem instance, output the next five poly-polygonal numbers that are greater than or equal to ss. Print each number on its own line in the following format:

num:k1 k2 k3 ...

where num is the poly-polygonal number and k1, k2, k3, ... are the indices (among the given ones), in increasing order, for which num is that kk-gonal number. Separate consecutive indices with a single space, and separate consecutive problem instances with a single blank line. The input guarantees that the maximum value of any poly-polygonal number fits in a 64-bit signed (long) integer.

Examples1

  1. Example 1

    Input
    10
    6 7 8 9 10 11 12 13 14 15
    1000
    5
    3 4 13 36 124
    1
    0
    
    Expected output
    1216:9 12
    1540:6 10
    1701:10 13
    2300:11 14
    3025:12 15
    
    1:3 4 13 36 124
    36:3 4 13 36
    105:3 36
    171:3 13
    1225:3 4 124