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爆搜过不了,考虑折半搜索,难点在于合并左右的答案,因为有可能答案同时载左右两边,我 ...
随机推荐
- Python 列表的复制操作
2013-10-18 10:07:03| import copy a = [1,2,3,['a','b']] b = a c = a[:] d = copy.copy(a) e = copy.de ...
- POJ-2240 -Arbitrage(Bellman)
题目链接:Arbitrage 让这题坑了,精度损失的厉害.用赋值的话.直接所有变成0.00了,无奈下,我仅仅好往里输了,和POJ1860一样找正环,代码也差点儿相同,略微改改就能够了,可是这个题精度损 ...
- Windows 上通过本地搭建 Jekyll环境
一 准备Ruby环境 1 我们首先须要安装Ruby.从站点下载Ruby 上下载Ruby最新版和对应的DevKit. 我下载的是Ruby 2.1.4 (x64)和DevKit-mingw64-6 .注意 ...
- webservice0基础
在学习webservice的时候,常常将ns和url花了好久时间才理解过来,这里备份下. 首先定义接口: @WebService public interface IService { @WebRes ...
- ZOJ 2706 Thermal Death of the Universe (线段树)
题目链接:ZOJ 2706 Thermal Death of the Universe (线段树) 题意:n个数.m个操作. 每一个操作(a,b)表示(a,b)全部值更新为这个区间的平均数:1.当前的 ...
- Android 自己定义View须要重写ondraw()等方法
Android 自己定义View须要重写ondraw()等方法.这篇博客给大家说说自己定义View的写法,须要我们继承View,然后重写一些 方法,方法多多,看你须要什么方法 首先写一个自己定义的V ...
- leetcode笔记:Majority Element
一. 题目描写叙述 Given an array of size n, find the majority element. The majority element is the element t ...
- [Android]自己定义带删除输入框
在项目开发中,带删除button输入框也是人们经常常使用到的,该文章便介绍一下怎样创建一个带删除输入框.当中,须要解决的问题例如以下: a)创建自己定义editText类 b)在自己定义editTex ...
- 数据结构-二叉树的遍历(类C语言描写叙述)
遍历概念 所谓遍历(Traversal)是指沿着某条搜索路线.依次对树中每一个结点均做一次且仅做一次訪问.訪问结点所做的操作依赖于详细的应用问题. 遍历是二叉树上最重要的运算之中的一个,是二叉 ...
- 嵌入式开发之dmva---基于DMVA2平台720P高清智能分析
http://wenku.baidu.com/view/2190746ba45177232f60a2b8.html