CF912E Prime Gift 数学
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.
首先在1e18的范围内,那么质数的个数最多只有16个,你可以将这16个列出来然后相乘,看看多大;
那么我们分开算;
不妨将奇数位置的dfs和偶数位置的分别dfs;
分别算出包含这些因子的所有可能值,如果对数组开多大不确定,用 vector.push_back就好;
那么我们要求第k大,考虑二分答案;
那么对于我们之前求出的那两个集合,我们用 two-pointer 来扫一遍确定当前答案是第几个;
然后调整上下界即可;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 300005
#define inf 0x3f3f3f3f
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int n;
int p[30]; ll k;
vector<ll>vc[2];
int tmp[30]; int cnt; void dfs(int belong, ll val, int cur) {
vc[belong].push_back(val);
for (int i = cur; i <= cnt; i++) {
if (1e18 / tmp[i] >= val) {
dfs(belong, val*tmp[i], i);
}
}
} ll sol(ll x) {
ll res = 0;
int j = 0;
for (int i = vc[0].size() - 1; i >= 0; i--) {
while (j < vc[1].size() && vc[1][j] <= x / vc[0][i])j++;// 双指针扫一遍
res += j;
}
return res;
} int main()
{
//ios::sync_with_stdio(0);
rdint(n);
for (int i = 1; i <= n; i++)rdint(p[i]);
rdllt(k);
for (int i = 1; i <= n; i++)if (i & 1)tmp[++cnt] = p[i];
dfs(0, 1, 1);
cnt = 0;
for (int i = 1; i <= n; i++)if (i % 2 == 0)tmp[++cnt] = p[i];
dfs(1, 1, 1);
sort(vc[0].begin(), vc[0].end());
sort(vc[1].begin(), vc[1].end());
ll l = 1, r = 1e18;
while (l <= r) {
ll mid = (l + r) / 2;
if (sol(mid) >= k)r = mid - 1;
else l = mid + 1;
}
cout << l << endl;
return 0;
}
CF912E Prime Gift 数学的更多相关文章
- CF912E Prime Gift题解(搜索+二分答案)
CF912E Prime Gift题解(搜索+二分答案) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1314956 洛谷题目链接 $ $ CF题目 ...
- CF912E Prime Gift
传送门 看到\(n\)只有16,可以把这些质数分成两半,然后预处理出这些数相乘得出的小于\(10^{18}\)的所有数,排个序,然后二分最终答案,再用两个指针从前往后和从后往前扫,进行\(two-po ...
- $CF912E\ Prime\ Gift$ 二分+搜索
正解:二分+搜索 解题报告: 传送门$QwQ$ 因为翻译真的很$umm$所以还是写下题目大意$QwQ$,就说给定一个大小为$n$的素数集合,求出分解后只含这些质数因子的第$K$小整数 考虑先把质数分两 ...
- Codeforces 912E - Prime Gift
912E - Prime Gift 思路: 折半枚举+二分check 将素数分成两个集合(最好按奇偶位置来,保证两集合个数相近),这样每个集合枚举出来的小于1e18的积个数小于1e6. 然后二分答案, ...
- Codeforces 912 E.Prime Gift (折半枚举、二分)
题目链接:Prime Gift 题意: 给出了n(1<=n<=16)个互不相同的质数pi(2<=pi<=100),现在要求第k大个约数全在所给质数集的数.(保证这个数不超过1e ...
- Prime Gift(prime)
Prime Gift(prime) 题目描述 Jyt有nn个质数,分别为p1,p2,p3-,pnp1,p2,p3-,pn. 她认为一个数xx是优秀的,当且仅当xx的所有质因子都在这nn个质数中. 她想 ...
- Codeforces 912E Prime Gift(预处理 + 双指针 + 二分答案)
题目链接 Prime Gift 题意 给定一个素数集合,求第k小的数,满足这个数的所有质因子集合为给定的集合的子集. 保证答案不超过$10^{18}$ 考虑二分答案. 根据折半的思想,首先我们把这个 ...
- Codeforces H. Prime Gift(折半枚举二分)
题目描述: Prime Gift time limit per test 3.5 seconds memory limit per test 256 megabytes input standard ...
- 680C. Bear and Prime 100 数学
C. Bear and Prime 100 time limit per test:1 second memory limit per test:256 megabytes input:standar ...
随机推荐
- 使用svn进行本地代码管理
简述 这里讨论的是如何管理自己个人电脑的个人项目的代码.和SVN服务器配置无关. 可以先到TortoiseSVN官网下载安装包进行安装. 代码仓库管理 比如现在有一个工程需要进行管理,可以先将该工程放 ...
- 部署和调优 1.5 vsftp部署和优化-1
系统自带的ftp服务软件.vsftpd 安装vsftpd yum install -y vsftpd 启动vsftpd /etc/init.d/vsftpd start 如果启动失败,可能是端口被占用 ...
- 关于handler的再次讨论
主要有两个问题,post方法和sendmessage方法有什么不同? 同一个handler对象发送的message只能发送给自己吗? 问题1: post方法,对于Handler的Post方式来说,它会 ...
- ms project展开和折叠任务
1.视图——大纲——显示子任务 2.视图——大纲——隐藏子任务
- 4-4 zk特性 – 理解watcher机制
watcher是zk里面非常重要的特性.watcher一定要去好好地看一下,一定要去好好地理解一下它是如何去用的,包括触发的事件类型等等.监督者也可以理解为触发器,也就是说当我们的节点发生了一些变化的 ...
- Codeforces #505(div1+div2) D Recovering BST
题意:给你一个升序的数组,元素之间如果gcd不为1可以建边,让你判断是否可以建成一颗二叉搜索树. 解法:dp,首先建图,然后进行状态转移.因为如果点k左端与i相连,右端与k相连,则i和k可以相连,同时 ...
- 按钮控件JButton的使用
---------------siwuxie095 工程名:TestUI 包名:com.siwuxie095.ui 类名:TestButton. ...
- Linux 内核与模块调试
一.简介 内核开发比用户空间开发更难的一个因素就是内核调试艰难.内核错误往往会导致系统宕机,很难保留出错时的现场.调试内核的关键在于你的对内核的深刻理解. 二.方法总结 1)内核模块相关指令 ht ...
- 339E Three Swaps
传送门 题目大意 给出由1-n组成的序列,每次可将一个区间翻转.问如何从1-n的递增序列变成给出的序列,输出操作次数以及每次操作的区间.最多翻转3次,保证有解,输出任意方案即可. 分析 我们对于每一次 ...
- 《Effective Java》第8章 通用程序设计
第47条:了解和使用类库 Top 100 Java Libraries on Github 2016 Library Number of Projects Type % of projects jun ...