Poly-polygonal Numbers
Time limit1sMemory limit128 MB
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 -gonal numbers are those whose points form a regular -gon (so triangular numbers are 3-gonal, square numbers are 4-gonal, and so on). We call the index of the polygonal number. The -th -gonal number is given by
In this problem you must find numbers that are -gonal for two or more values of . 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 (), the number of kinds of polygonal numbers of interest in this instance.
- The second line contains integers, the indices of those polygonal numbers. They are all distinct and given in increasing order, and each index satisfies . (This line may be longer than 80 characters.)
- The third line contains a single positive integer (), the starting point for the search for poly-polygonal numbers.
A value of terminates the input.
Output
For each problem instance, output the next five poly-polygonal numbers that are greater than or equal to . 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 -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.