ALWFACT - Allowed Factors

Chico told his students that now they would be referenced by numerical codes of up to 12 digits in official communications (e-mails and tasks). And then he gave to each one of his students a card containing an unique number written on it. Quickly the students assumed that this would be their code, but to the surprise of the students and Levi's despair, Professor Chico explained that these were not their code.

A student's code was the term of an ordered sequence S that was in the position (indexed from 1) specified by the number on the card of each student. This sequence has a special feature: each term, when decomposed into prime factors, can only have numbers contained in a set of N elements written on the board by the teacher. And to make life even harder for Levi, those numbers would change every week in such a way that he will always have to recalculate his code if he doesn't want to delay his tasks.

Your task is to make a program to help Levi, so that given the prime numbers written on the board during the week by Professor Chico and number on the card, tell his weekly code.


The input consists of several test cases. The first line of a test case contains two integers N (1 ≤ N ≤ 102) and M (1 ≤ M ≤ 105) representing respectively the number of numbers written by Professor Chico and the number written on the card. The second line contains N prime numbers Pi (2 ≤ Pi < 106), where Pi (1 ≤ iN) is a number written in the board. The entry ends when N=M=0.


The output consists of one line per test case containing the Levi's weekly code.



2 1

2 3

2 10

2 3

3 10

2 3 5

3 10

3 7 13

0 0






hide comments
julkas: 2018-06-03 11:46:46

Good problem and interesting results.

absolute_coder: 2018-05-28 15:03:20

could you tell ..No of test case ?

Added by:Francisco Elio Parente Arcos Filho [UEA]
Time limit:0s-1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: SQLITE