codeforces912E(折半搜索+双指针+二分答案)
E. Prime Gift
3.5 seconds
256 megabytes
standard input
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.
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.
Print a single line featuring the k-th smallest integer. It's guaranteed that the answer doesn't exceed 1018.
3
2 3 5
7
8
5
3 7 11 13 31
17
93
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[];
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=;
ll ret=;
for(int i=seg[].size()-;i>=;i--)
{
while(j<seg[].size() && seg[][j]<=num/seg[][i])
j++;
ret+=j;
}
return ret;
} void solve()
{
int i,j;
dfs(,min(,n),,);
dfs(min(,n)+,n,,);
sort(seg[].begin(),seg[].end());
sort(seg[].begin(),seg[].end());
ll L=,R=inf,mid;
while(L<R-)
{
mid=(L+R)>>;
if(cnt(mid)>=ansid) R=mid;
else L=mid;
}
cout<<R<<endl;
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&p[i]);
cin>>ansid;
solve();
return ;
}
codeforces912E(折半搜索+双指针+二分答案)的更多相关文章
- 【BZOJ 2679】[Usaco2012 Open]Balanced Cow Subsets(折半搜索+双指针)
[Usaco2012 Open]Balanced Cow Subsets 题目描述 给出\(N(1≤N≤20)\)个数\(M(i) (1 <= M(i) <= 100,000,000)\) ...
- Codeforces 912E Prime Gift(预处理 + 双指针 + 二分答案)
题目链接 Prime Gift 题意 给定一个素数集合,求第k小的数,满足这个数的所有质因子集合为给定的集合的子集. 保证答案不超过$10^{18}$ 考虑二分答案. 根据折半的思想,首先我们把这个 ...
- codeforces 880E. Maximum Subsequence(折半搜索+双指针)
E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 洛谷P1528 切蛋糕 [搜索,二分答案]
题目传送门 切蛋糕 题目描述 Facer今天买了n块蛋糕,不料被信息组中球球等好吃懒做的家伙发现了,没办法,只好浪费一点来填他们的嘴巴.他答应给每个人留一口,然后量了量每个人口的大小.Facer有把刀 ...
- Codeforces 912E Prime Gift ( 二分 && 折半枚举 && 双指针技巧)
题意 : 给你 N ( 1 ≤ N ≤ 16 ) 个质数,然后问你由这些质数作为因子的数 ( 此数不超 10^18 ) & ( 不一定需要其因子包含所给的所有质数 ) 的第 k 个是什么 分析 ...
- E. Santa Claus and Tangerines 二分答案 + 记忆化搜索
http://codeforces.com/contest/752/problem/E 首先有一个东西就是,如果我要检测5,那么14我们认为它能产生2个5. 14 = 7 + 7.但是按照平均分的话, ...
- CF912E Prime Gift题解(搜索+二分答案)
CF912E Prime Gift题解(搜索+二分答案) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1314956 洛谷题目链接 $ $ CF题目 ...
- Luogu1084 NOIP2012D2T3 疫情控制 二分答案、搜索、贪心、倍增
题目传送门 题意太长就不给了 发现答案具有单调性(额外的时间不会对答案造成影响),故考虑二分答案. 贪心地想,在二分了一个时间之后,军队尽量往上走更好.所以我们预处理倍增数组,在二分时间之后通过倍增看 ...
- [题解](折半搜索)luogu_P4799_BZOJ_4800世界冰球锦标赛
抄的题解 以及参考:https://www.cnblogs.com/ZAGER/p/9827160.html 2^40爆搜过不了,考虑折半搜索,难点在于合并左右的答案,因为有可能答案同时载左右两边,我 ...
随机推荐
- Cooking Schedule Problem Code: SCHEDULE(优先队列)
Cooking Schedule Problem Code: SCHEDULE Chef is a well-known chef, and everyone wishes to taste his ...
- <项目><day12>通讯录(视频)
1 需求分析(需求分析师) 功能分析: 1)添加联系人 2)修改联系人 3)删除联系人 4)查询所有联系人 2 需求设计(系统分析师/架构师/资深开发人员) 2.1设计实体(抽象实体) 联系人实体: ...
- mybatis几种开发方式
mybatis是比较轻巧的半自动化的CRM框架,它有几种开发方式,现今张列于此: 一.注解方式:在接口方法上面写SQL语句,有点类似springdataJPA 的query sql 语句 范例 @se ...
- cef3的各个接口你知道几个
CEF3基本的框架包含C/C++程 序接口,通过本地库的接口来实现,而这个库则会隔离宿主程序和 Chromium&Webkit的操作细节.它在浏览器控件和宿主程序之间提供紧密的整合,它支持用户 ...
- uva live 12846 A Daisy Puzzle Game
假设下一个状态有必败.那么此时状态一定是必胜,否则此时状态一定是必败 状压DP #include<iostream> #include<map> #include<str ...
- powerdesign导出SQL时自己主动生成凝视
1.使用脚本的方式 在里面执行 Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl ' ...
- pat(A)1041. Be Unique(哈希)
1.链接:点击打开链接 2.代码: #include<cstdio> #include<iostream> #include<cstring> using name ...
- 在CentOS上把MySQL从5.5升级到5.6
在CentOS上把MySQL从5.5升级到5.6 摘要:本文记录了在CentOS 6.3上,把MySQL从5.5.28升级到5.6.19的过程. 1. 概述 在我做的一个项目中,最近我对生产服务器上的 ...
- 【iOS系列】-程序开启后台运行
[iOS系列]-程序开启后台运行 iOS程序是伪后台的运行,可是有时候我们需要让其在后台也要进行一些操作,我们可以让其伪装成音乐的APP,这样就可以让程序后台进行相关操作了,具体做法如下: 1:在Ap ...
- 【iOS系列】-autorelease的作用
内存管理原则(配对原则):只要出现了new,alloc,retain方法,就要配对出现release,autorelease 1:对象存入到自动释放池中,当这个池子被销毁的时候他会对池子中所有的对 ...