《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 .

隐藏翻译

题意翻译

给你 n 个互不相同的素数 p1​,p2​,⋯,pn​,它们组成一个集合 P。

请你求出第 k 小的正整数,满足:

  • 该数字的所有素因子 ∈P

1≤n≤16,2≤pi​≤100, 保证答案不超过 1018。

输入输出样例

输入 #1复制

复制代码
3
2 3 5
7

输出 #1复制

复制代码
8

输入 #2复制

复制代码
5
3 7 11 13 31
17

输出 #2复制

复制代码
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.

代码实现:

#include <iostream>

#include <queue>

#include <set>

#include <vector>

using namespace std;

int main() {

int n, k;

cin >> n;

vector<long long> primes(n);

for (int i = 0; i < n; i++) {

cin >> primes[i];

}

cin >> k;

priority_queue<long long, vector<long long>, greater<long long> > pq;

set<long long> generated;

pq.push(1);

generated.insert(1);

for (int i = 0; i < k; i++) {

long long current = pq.top();

pq.pop();

if (i == k - 1) {

cout << current << endl;

return 0;

}

// 使用迭代器遍历primes

for (vector<long long>::iterator it = primes.begin(); it != primes.end(); ++it) {

long long p = *it;

long long nextNum = current * p;

if (nextNum / p == current && generated.find(nextNum) == generated.end()) {

pq.push(nextNum);

generated.insert(nextNum);

}

}

}

return 0;

}

相关推荐
No0d1es6 小时前
电子学会青少年软件编程(C/C++)5级等级考试真题试卷(2024年6月)
c语言·c++·算法·青少年编程·电子学会·五级
大阳1238 小时前
线程(基本概念和相关命令)
开发语言·数据结构·经验分享·算法·线程·学习经验
weixin_307779139 小时前
VS Code配置MinGW64编译GNU 科学库 (GSL)
开发语言·c++·vscode·算法
学行库小秘9 小时前
ANN神经网络回归预测模型
人工智能·python·深度学习·神经网络·算法·机器学习·回归
没落之殇10 小时前
基于C语言实现的HRV分析方法 —— 与Kubios和MATLAB对比
算法
秋难降10 小时前
线段树的深度解析(最长递增子序列类解题步骤)
数据结构·python·算法
楚韵天工10 小时前
基于GIS的无人机模拟飞行控制系统设计与实现
深度学习·算法·深度优先·无人机·广度优先·迭代加深·图搜索算法
你也向往长安城吗11 小时前
推荐一个三维导航库:three-pathfinding-3d
javascript·算法
百度智能云12 小时前
VectorDB+FastGPT一站式构建:智能知识库与企业级对话系统实战
算法
AI小白的Python之路12 小时前
数据结构与算法-排序
数据结构·算法·排序算法