Prime Gift(prime)

题目描述

Jyt有nn个质数,分别为p1,p2,p3…,pnp1,p2,p3…,pn。

她认为一个数xx是优秀的,当且仅当xx的所有质因子都在这nn个质数中。

她想知道第kk大的优秀的数是多少。保证答案不超过10181018。

输入

第一行一个整数nn,表示质数的个数。

第二行nn个升序排列的质数,第ii个数表示pipi。

第三行一个整数kk,表示要求的是第kk大的数。

输出

一行,表示第kk大的优秀的数。

样例输入

<span style="color:#333333"><span style="color:#333333">#### 样例输入1
3
2 3 5
7
#### 样例输入2
5
3 7 11 13 31
17</span></span>

样例输出

<span style="color:#333333"><span style="color:#333333"> 样例输出1
8
样例输出2
93</span></span>

提示

数据规模及约定

对于10%的数据,n≤2n≤2。

对于30%的数据,n≤5n≤5。

对于60%的数据,n≤10n≤10。

对于100%的数据,n≤16,2≤pi≤100n≤16,2≤pi≤100。

来源

Codeforces912E


solution

神奇题

考虑爆搜,显然数目太多了。

把数字分为两组,可以奇数下标一组,偶数下标一组

有点像meet-in-middle

但这样我们没法一次算出答案。

再二分一下即可。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#define ll long long
#define Top 1e18
using namespace std;
int k,n,s[20];
ll q1[5000005],q2[5000005],l,r,t1,t2;
ll pd(ll num)
{
ll cnt=0,p=t2;
for(int i=1;i<=t1;i++){
while(q2[p]>num/q1[i])p--;
cnt+=p;
}
return cnt;
}
void dfs(ll x,int las){
q1[++t1]=x;
for(int i=las;i<=k;i+=2){
if(x<=Top/(ll)s[i])dfs(s[i]*x,i);
}
}
void DFS(ll x,int las){
q2[++t2]=x;
for(int i=las;i<=k;i+=2){
if(x<=Top/(ll)s[i])DFS(s[i]*x,i);
}
}
int main()
{
cin>>k;
for(int i=1;i<=k;i++){
scanf("%d",&s[i]);
}
cin>>n;
dfs(1,1);DFS(1,2); sort(q1+1,q1+t1+1);
sort(q2+1,q2+t2+1);
l=1,r=1e18; while(l<r){
//cout<<l<<' '<<r<<endl;
ll mid=l+r>>1;
ll t=pd(mid);
if(t<n)l=mid+1;
if(t==n)r=mid;
if(t>n) r=mid-1;
}
cout<<l<<endl;
return 0;
}

Prime Gift(prime)的更多相关文章

  1. Codeforces 912E - Prime Gift

    912E - Prime Gift 思路: 折半枚举+二分check 将素数分成两个集合(最好按奇偶位置来,保证两集合个数相近),这样每个集合枚举出来的小于1e18的积个数小于1e6. 然后二分答案, ...

  2. Codeforces 912 E.Prime Gift (折半枚举、二分)

    题目链接:Prime Gift 题意: 给出了n(1<=n<=16)个互不相同的质数pi(2<=pi<=100),现在要求第k大个约数全在所给质数集的数.(保证这个数不超过1e ...

  3. Codeforces 912E Prime Gift(预处理 + 双指针 + 二分答案)

    题目链接 Prime Gift 题意  给定一个素数集合,求第k小的数,满足这个数的所有质因子集合为给定的集合的子集. 保证答案不超过$10^{18}$ 考虑二分答案. 根据折半的思想,首先我们把这个 ...

  4. Codeforces H. Prime Gift(折半枚举二分)

    题目描述: Prime Gift time limit per test 3.5 seconds memory limit per test 256 megabytes input standard ...

  5. CF912E Prime Gift题解(搜索+二分答案)

    CF912E Prime Gift题解(搜索+二分答案) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1314956 洛谷题目链接 $     $ CF题目 ...

  6. CF912E Prime Gift 数学

    Opposite to Grisha's nice behavior, Oleg, though he has an entire year at his disposal, didn't manag ...

  7. 【Codeforces 912E】Prime Gift

    Codeforces 912 E 题意:给\(n\leq16\)个素数\(p_1..p_n\),求第\(k\)个所有质因数都在\(n\)个数中的数. 思路:折半搜索...我原来胡搞毛搞怎么也搞不动\( ...

  8. CF912E Prime Gift

    传送门 看到\(n\)只有16,可以把这些质数分成两半,然后预处理出这些数相乘得出的小于\(10^{18}\)的所有数,排个序,然后二分最终答案,再用两个指针从前往后和从后往前扫,进行\(two-po ...

  9. Prime Gift CodeForces - 912E (中途相遇)

    链接 大意:求素因子只含给定素数的第k大数 先二分答案转为判定x是第几大, 然后分两块合并即可, 按奇偶分块可以优化一下常数 #include <iostream> #include &l ...

随机推荐

  1. python_67_生成器3

    import time def consumer(name): print("%s 准备吃包子啦!"%name) while True: baozi = yield print(& ...

  2. Linux内核参数min_free_kbytes

    1. min_free_kbytes 先看官方解释: This is used to force the Linux VM to keep a minimum number of kilobytes ...

  3. Symfony相关网站参考

    http://www.doctrine-project.org/projects.html 数据库相关知识 http://firehare.blog.51cto.com/809276/703599整合 ...

  4. "segmentation fault " when "import tensorflow as tf"

    https://github.com/tensorflow/tensorflow/issues/2034

  5. IPython安装过程 @win7 64bit

    http://www.360doc.com/content/14/0902/11/16740871_406476389.shtml 为了测验测验一下IPython的应用,今天折腾了好久的从安装包msi ...

  6. c++ question 003 求两数大者?

    #include <iostream>using namespace std; int main(){ //求两数中的大者? int a,b; cin>>a>>b; ...

  7. HTML <input> 标签如何屏蔽浏览器的自动填写?

    autocomplete = "off",实测无效. <input type="text" autocomplete = "off"/ ...

  8. JZOJ 5775. 【NOIP2008模拟】农夫约的假期

    5775. [NOIP2008模拟]农夫约的假期 (File IO): input:shuru.in output:shuru.out Time Limits: 1000 ms  Memory Lim ...

  9. python3.7 time模块

    #!/usr/bin/env python __author__ = "lrtao2010" #python3.7 time模块 #time模块没有time.py文件,是内置到解释 ...

  10. 权限组件(11):基于formset实现批量增加

    效果图: 增加页面: 编辑页面: 因为后面要对权限进行批量操作,所以先用这个示例演示下如何实现批量操作 数据库 from django.db import models class Menu(mode ...