## ALWFACT Statement

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.

### Input

The input consists of several test cases. The first line of a test case contains two integers **N** (1 ≤ **N** ≤ 10^{2}) and **M **(1 ≤ **M** ≤ 10^{5}) representing respectively the number of numbers written by Professor Chico and the number written on the card. The second line contains **N **prime numbers **P**_{i }(2 ≤ **P**_{i} < 10^{6}), where **P**_{i} (1 ≤ **i** ≤ **N**) is a number written in the board. The entry ends when **N**=**M**=0.

### Output

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

## ALWFACT Solution

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include <iostream> #include <algorithm> #include <vector> #include <set> using namespace std; int N, M; void getCount(long long product, long long N, int p, vector<int> &data, int &skipped) { if (p >= (int)data.size()) { skipped ++; return; } else if (product > N) { return; } for (int i = 0; ; i ++) { if (product <= N) { getCount(product, N, p + 1, data, skipped); } else { break; } product *= data[p]; } } int main() { ios_base::sync_with_stdio(0); while (cin >> N >> M) { if (N == 0 && M == 0) { break; } vector<int> data(N); for (int i = 0; i < N; i ++) { cin >> data[i]; } sort(data.begin(), data.end(), greater<int>()); long long low = 2, high = 1LL << 40, middle, best = high; while (low <= high) { middle = (low + high + 1) >> 1; int ways = 0; getCount(1, middle, 0, data, ways); if (ways >= M + 1) { best = middle; high = middle - 1; } else { low = middle + 1; } } cout << best << endl; } //cout << "clock = " << 1. * clock() / CLOCKS_PER_SEC << endl; return 0; } |

**You also like to see:**