Codeforces Round #565 (Div. 3)--D. Recover it!--思维+欧拉筛
D. Recover it!
Authors guessed an array aa consisting of nn integers; each integer is
not less than 22 and not greater than 2⋅1052⋅105. You don’t know the
array aa, but you know the array bb which is formed from it with the
following sequence of operations:Firstly, let the array bb be equal to the array aa; Secondly, for each
ii from 11 to nn: if aiai is a prime number, then one integer paipai
is appended to array bb, where pp is an infinite sequence of prime
numbers (2,3,5,…2,3,5,…); otherwise (if aiai is not a prime number),
the greatest divisor of aiai which is not equal to aiai is appended to
bb; Then the obtained array of length 2n2n is shuffled and given to
you in the input. Here paipai means the aiai-th prime number. The
first prime p1=2p1=2, the second one is p2=3p2=3, and so on.Your task is to recover any suitable array aa that forms the given
array bb. It is guaranteed that the answer exists (so the array bb is
obtained from some suitable array aa). If there are multiple answers,
you can print any.Input
The first line of the input contains one integer nn
(1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in aa.The second line of the input contains 2n2n integers
b1,b2,…,b2nb1,b2,…,b2n (2≤bi≤27501312≤bi≤2750131), where bibi is the
ii-th element of bb. 27501312750131 is the 199999199999-th prime
number.Output
In the only line of the output print nn integers a1,a2,…,ana1,a2,…,an
(2≤ai≤2⋅1052≤ai≤2⋅105) in any order — the array aa from which the
array bb can be obtained using the sequence of moves given in the
problem statement. If there are multiple answers, you can print any.
Examples
input
Copy
3
3 5 2 3 2 4
output
Copy
3 4 2
input
Copy
1
2750131 199999
output
Copy
199999
input
Copy
1
3 6
output
Copy
6
题解如下
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int Len = 1e6;
int prime[Len * 3];
int ar[Len * 3];
int br[3 * Len];
int barrel[3 * Len];
vector<int> vec;
int n;
bool cmp(int a,int b)
{
return a > b;
}
void Prime()
{
for(int i = 2; i <= Len * 3; i ++)
prime[i] = 1;
//素数筛选法
for(int i = 2; i * i <= Len * 3; i ++)
{
if(prime[i])
{
for(int j = i * i; j <= Len * 3; j += i)
prime[j] = 0;
}
}
}
void init()
{
Prime();
int pos = 1;
for(int i = 2; i <= Len * 3; i ++)
{
if(prime[i])
{
ar[pos ++] = i;
}
}
//输入
for(int i = 1; i <= 2 * n; i ++)
{
scanf("%d",&br[i]);
}
//统计各个数字出现的次数
for(int i = 1; i <= 2 * n; i ++)
{
barrel[br[i]] ++;
}
sort(br + 1 , br + 2 * n + 1 , cmp);
}
void Solve()
{
init();
for(int i = 1; i <= 2 * n; i ++)
{
int cnt = barrel[br[i]];
if(cnt > 0)
{
if(! prime[br[i]])
{
int mx_divisor;
for(int j = 2; ; j ++)
if(br[i] % j == 0)
{
mx_divisor = br[i] / j;
break;
}
if(barrel[mx_divisor] > 0)
{
barrel[mx_divisor] --;
vec.push_back(br[i]);
barrel[br[i]] --;
}
}
else
{
int pri = ar[br[i]];
if(barrel[pri] > 0)
{
barrel[pri] --;
vec.push_back(br[i]);
barrel[br[i]] --;
}
}
}
}
for(auto x : vec)
printf("%d ",x);
}
int main()
{
//freopen("test.txt","r",stdin);
scanf("%d",&n);
Solve();
return 0;
}
Codeforces Round #565 (Div. 3)--D. Recover it!--思维+欧拉筛的更多相关文章
- Codeforces Round #565 (Div. 3) C. Lose it! (思维)
题意:给你一串只含\(4,8,15,16,23,42\)的序列,如果它满足长度是\(6\)的倍数并且有\(\frac {k}{6}\)个子序列是\([4,8,15,16,23,42]\),则定义它是好 ...
- Codeforces Round #288 (Div. 2)D. Tanya and Password 欧拉通路
D. Tanya and Password Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/508 ...
- Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)
传送门 题目 \[ \begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{aligned} \] 思路 我们通过迭代发 ...
- Codeforces Round #565 (Div. 3) B. Merge it!
链接: https://codeforces.com/contest/1176/problem/B 题意: You are given an array a consisting of n integ ...
- Codeforces Round #565 (Div. 3) A. Divide it!
链接: https://codeforces.com/contest/1176/problem/A 题意: You are given an integer n. You can perform an ...
- Codeforces Round #565 (Div. 3) C. Lose it!
链接: https://codeforces.com/contest/1176/problem/C 题意: You are given an array a consisting of n integ ...
- Codeforces Round #565 (Div. 3) B
B. Merge it! 题目链接:http://codeforces.com/contest/1176/problem/B 题目 You are given an array a consistin ...
- Codeforces Round #565 (Div. 3) A
A. Divide it! 题目链接:http://codeforces.com/contest/1176/problem/A 题目 You are given an integer n You ca ...
- Codeforces Round #565 (Div. 3) F.Destroy it!
题目地址:http://codeforces.com/contest/1176/problem/F 思路:其实就是一个01背包问题,只是添加了回合和每回合的01限制,和每当已用牌数到了10的倍数,那张 ...
随机推荐
- Flutter环境搭建以及快捷命令
Flutter环境搭建 配置环境变量 用户变量 FLUTTER_STORAGE_BASE_URL : https://storage.flutter-io.cn PUB_HOSTED_URL : ht ...
- Excel之在单元格中生成随机密码
公式 =CHAR(INT(RAND()*26+97))&INT(RAND()*10)&CHAR(INT(RAND()*26+97))&INT(RAND()*10) 分析 CHA ...
- 轻量级MVC框架(自行开发)
源码及demo: https://github.com/killallspree/myFrame/
- 《前端之路》 - 初试 TypeScript(一)基础数据类型
一.先讲讲 TypeScript 什么是 typeScript ? typeScript 是 Javascript 的超集 我们用一张图来简单介绍下 ts 和 js 清清楚楚明明白白的关系- 为什么会 ...
- activated钩子函数
activated钩子函数是在组件被激活后的钩子函数,mounted是不保证组件在document中,也就是组件还没有被激活,因此可以理解为activated执行在mounted之后. 在跳转传值时, ...
- 附014.Kubernetes Prometheus+Grafana+EFK+Kibana+Glusterfs整合解决方案
一 glusterfs存储集群部署 注意:以下为简略步骤,详情参考<附009.Kubernetes永久存储之GlusterFS独立部署>. 1.1 架构示意 略 1.2 相关规划 主机 I ...
- mysql8 修改root密码
Navicat工具里选中mysql数据库 执行: ALTER user 'root'@'localhost' IDENTIFIED BY 'newpassward'; //newpassward 新密 ...
- linux入门系列19--数据库管理系统(DBMS)之MariaDB
前面讲完Linux下一系列服务的配置和使用之后,本文简单介绍一款数据库管理系统(MySQL的兄弟)MariaDB. 如果你有MySQL或其他数据的使用经验,MariaDB使用起来将非常轻松. 本文讲解 ...
- Jmeter之JSON提取器应用
在接口测试中有一个这样的场景:登录之后,需要进行昵称修改,怎么实现? 首先我们分别看下登录.昵称修改的接口说明: 以上业务中补充一点,昵称修改,还需要添加请求头Authorization传登录获取的t ...
- 毕业设计——基于ZigBee的智能窗户控制系统的设计与实现
题目:基于物联网的智能窗户控制系统的设计与实现 应用场景:突降大雨,家里没有关窗而进水:家中燃气泄漏,不能及时通风,威胁人身安全,存在火灾的隐患:家中窗户没关,让坏人有机可乘.长时间呆在人多.封闭的空 ...