Given cycle lengths that already coincide within L, choose the smallest extra cycle maximizing the next common multiple not exceeding L.
Hard8Number theoryMathBinary searchNo attempts yetTime limit1sMemory limit1024 MBThe North American periodical cicadas have the longest life cycle among known insects. Every 17 years they mature, mate, lay eggs, and die. The newly hatched young burrow 20 centimeters underground, feed on root sap for 17 years, and come out when their turn arrives.
The number 17 is believed not to be an accident. Another cicada species in the same region runs on a 13 year cycle, so the two species emerge in the same year only once every 221 years. The chance that the two species mix drops sharply, and the traits of one population do not enter the other.
A variant of an evolutionary algorithm borrows this idea. In its last stage the best candidate solutions are split into populations, and population i gets a life cycle Ci. One extra population is then added, and its period is chosen so that the number of iterations until every life cycle coincides again is as large as possible. The populations are evaluated until all life cycles coincide, and the best solution at that moment is taken. Waiting too long for an answer is useless, so the number of iterations also has an upper bound L.
Given the life cycles of the populations and the iteration limit L, compute the optimal period for the extra population.
The first line contains two integers N and L. N is the number of populations produced by the earlier stages and L is the limit on the number of iterations (2≤N≤104, 1≤L≤106).
The second line contains the N values Ci, the length in iterations of each population's life cycle (1≤Ci). The life cycles of the current populations coincide within L iterations.
Print on one line the period of the extra population that maximizes the number of iterations T until the life cycles of all populations coincide, under the condition T≤L. If several periods reach that maximum, print the smallest one.