zoukankan      html  css  js  c++  java
  • 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; }

     

  • 相关阅读:
    asp.net调用mysql 存储过程 带 out 返回值,返回刚插入数据库中的自增的ID,LAST_INSERT_ID() 的使用
    如何俘获一个 IT 男的心,让他成为男友然后变成老公?
    MySqlHelper.cs mysql数据库助手类
    mysql 按年度、季度、月度、周、日SQL统计查询,mysql 存储过程 中 in 和 FIND_IN_SET 传递多个参数的使用
    奇怪的母版页里面的 form 表单里面的 enctype="multipart/formdata" html控件上传 FileUpload控件上传 一次多图片上传
    asp.net 连接 Mysql 代码生成器(下载地址)
    Convert.ToInt32、(int)和int.Parse,int.TryParse四者之间的区别:
    在web项目中 使用 WebService 根据IP地址来源搜索实际物理地址,常用的WebServices
    vc6控制台程序利用SoapToolkit3.0调用WebService
    浅议C++/CLI的gcnew关键字
  • 原文地址:https://www.cnblogs.com/L-Memory/p/9905612.html
Copyright © 2011-2022 走看看