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的倍数,那张 ...
随机推荐
- (26)ASP.NET Core EF保存(基本保存、保存相关数据、级联删除、使用事务)
1.简介 每个上下文实例都有一个ChangeTracker,它负责跟踪需要写入数据库的更改.更改实体类的实例时,这些更改会记录在ChangeTracker中,然后在调用SaveChanges时会被写入 ...
- 全面认识HBase架构(建议收藏)
在网上看过很多HBaes架构相关的文章,内容深浅不一,直到发现了一篇MapR官网的文章https://mapr.com/blog/in-depth-look-hbase-architecture/#. ...
- js中~~和^=分别代表什么,用处是什么?
先看个栗子: ~~false === 0 ~~true === 1 ~~undefined === 0 ~~!undefined === 1 ~~null === 0 ~~!null === 1 ~~ ...
- redis 出现(error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details
如果在ubuntu安装的redis含端口使用,但是某些时候常常出现 (error) MISCONF Redis is configured to save RDB snapshots, but is ...
- Java基础(五):数组
数组,一种应用非常广泛的数据结构,简单地来说就是一组类型相同且无序的元素的存储在固定长度且有序的内存空间. 创建一个数组 在Java中,我们可以通过[]去声明一个指定类型的数组 int[] a; // ...
- php7连接mysql8
最近因为剁手买了mac所以在mac上搭建lnmp环境. 刚好看到mysql从5.7跳到8,性能据说快上一倍,果断尝鲜! lnmp基本都弄好了,但是到用php连接Mysql这一步出了岔子. 出错原因: ...
- 返回运行方法,可以写在一行 callback&&callback()
return DiscountMap[discountType] && DiscountMap[discountType](price)
- 解决不管其他元素z-index设置多高,都在视频下面的方法
<div style="z-index:-1"> <embed name="Movie1" src="http://ecards.s ...
- Asp.Net Core 中IdentityServer4 实战之 Claim详解
一.前言 由于疫情原因,让我开始了以博客的方式来学习和分享技术(持续分享的过程也是自己学习成长的过程),同时也让更多的初学者学习到相关知识,如果我的文章中有分析不到位的地方,还请大家多多指教:以后我会 ...
- Natas0-34 Writeup
Natas是一个教授服务器端Web安全基础知识的 wargame,通过在每一关寻找Web安全漏洞,来获取通往下一关的秘钥,适合新手入门Web安全. 传送门~ 接下来给大家分享一下,1-34题的Writ ...