codeforces912E(折半搜索+双指针+二分答案)

E. Prime Gift

E. Prime Gift
time limit per test
3.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Opposite to Grisha's nice behavior, Oleg, though he has an entire year at his disposal, didn't manage to learn how to solve number theory problems in the past year. That's why instead of Ded Moroz he was visited by his teammate Andrew, who solemnly presented him with a set of n distinct prime numbers alongside with a simple task: Oleg is to find the k-th smallest integer, such that all its prime divisors are in this set.

Input

The first line contains a single integer n (1 ≤ n ≤ 16).

The next line lists n distinct prime numbers p1, p2, ..., pn (2 ≤ pi ≤ 100) in ascending order.

The last line gives a single integer k (1 ≤ k). It is guaranteed that the k-th smallest integer such that all its prime divisors are in this set does not exceed 1018.

Output

Print a single line featuring the k-th smallest integer. It's guaranteed that the answer doesn't exceed 1018.

Examples
input
Copy
3
2 3 5
7
output
Copy
8
input
Copy
5
3 7 11 13 31
17
output
Copy
93
Note

The list of numbers with all prime divisors inside {2, 3, 5} begins as follows:

(1, 2, 3, 4, 5, 6, 8, ...)

The seventh number in this list (1-indexed) is eight.

/*

  给定一个大小为n的素数集合

  求出分解后只含这些质数因子的第k小整数


直接枚举判断显然不可以。 考虑折半搜索。可以把这16个数字拆成2个子集,各自生成所有大小1e18及以下的积。 但也需要使两个乘积组成的集合尽量接近。可以预先造出极限数据试一试集合里能有多少数 对于最坏情况,即如下数据 16 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 分为2 3 5 7 11 13 和 17 19 23 29 31 37 41 43 47 53 两个集合时 这两个集合生成的1e18及以下的积的数量分别为 958460个 和 505756个,并不大 集合中数必须两两不等。 最后统计答案, 两个集合生成的积各自排一下序 然后二分答案,对于每个答案 u,可以O(|S|)双指针得到他是第几大。 具体做法是枚举从到小枚举第一个集合的积 t1,然后计算一下第二个集合的积中有多少积和 t1 相乘小于等于 u, 由于是从大到小枚举的,所以t1必然递增所以第二个集合的积中符合条件的积的数量也必然是递增的,所以只要扫一遍就行。 */ #include<bits/stdc++.h> #define ll long long #define inf 1e18 #define N 24 using namespace std; vector<ll> seg[2]; int p[N],n; ll ansid; void dfs(int L,int R,ll val,int id) { seg[id].push_back(val); for(int i=L;i<=R;i++) if(inf/p[i]>=val) dfs(i,R,val*p[i],id); } ll cnt(ll num) { int j=0; ll ret=0; for(int i=seg[0].size()-1;i>=0;i--) { while(j<seg[1].size() && seg[1][j]<=num/seg[0][i]) j++; ret+=j; } return ret; } void solve() { int i,j; dfs(1,min(6,n),1,0); dfs(min(6,n)+1,n,1,1); sort(seg[0].begin(),seg[0].end()); sort(seg[1].begin(),seg[1].end()); ll L=0,R=inf,mid; while(L<R-1) { mid=(L+R)>>1; if(cnt(mid)>=ansid) R=mid; else L=mid; } cout<<R<<endl; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&p[i]); cin>>ansid; solve(); return 0; }

 

posted @ 2018-11-04 20:33  安月冷  阅读(360)  评论(0编辑  收藏  举报