题目链接:

D. Simple Subset

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i,  j) (1  ≤ i  <  j ≤ k), xi  +  xj is a prime.

You are given an array a with n positive integers a1,  a2,  ...,  an (not necessary distinct). You want to find a simple subset of the array awith the maximum size.

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it.

Input
 

The first line contains integer n (1 ≤ n ≤ 1000) — the number of integers in the array a.

The second line contains n integers ai (1 ≤ ai ≤ 106) — the elements of the array a.

Output
 

On the first line print integer m — the maximum possible size of simple subset of a.

On the second line print m integers bl — the elements of the simple subset of the array a with the maximum size.

If there is more than one solution you can print any of them. You can print the elements of the subset in any order.

Examples
 
input
2
2 3
output
2
3 2
input
2
2 2
output
1
2
input
3
2 1 1
output
3
1 1 2
input
2
83 14
output
2
14 83 题意: 选一个最大的子序列,满足这个序列里的任意两个数的和是素数; 思路: 可以是一个最大完全数的题,也可以是水题,因为奇数+奇数=偶数,偶数+偶数=偶数,(1除外);所以最多有一个奇数和一个偶数;
我写的分情况讨论的代码真是跟翔一样; AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=2e6+;
typedef long long ll;
int n,a[],flag[N];
int get_prime()
{
for(int i=;i<N;i++)
{
if(!flag[i])
{
for(int j=*i;j<N;j+=i)
{
flag[j]=;
}
}
}
}
queue<int>qu;
void print()
{
printf("%d\n",qu.size());
while(!qu.empty())
{
printf("%d ",qu.front());
qu.pop();
}
} int main()
{
get_prime();
int f=;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==)
{
f++;
qu.push();
}
}
if(f>)
{
for(int i=;i<=n;i++)
{
if(a[i]>&&!flag[a[i]+])
{
for(int j=i+;j<=n;j++)
{
if(a[j]>&&!flag[a[i]+a[j]]&&!flag[a[j]+])
{
qu.push(a[i]);
qu.push(a[j]);
print();
return ;
}
}
}
}
for(int i=;i<=n;i++)
{
if(a[i]>&&!flag[a[i]+])
{
qu.push(a[i]);
print();
return ;
}
}
print();
}
else if(f==)
{
for(int i=;i<=n;i++)
{
if(a[i]>&&!flag[a[i]+])
{
for(int j=i+;j<=n;j++)
{
if(a[j]>&&!flag[a[i]+a[j]]&&!flag[a[j]+])
{
qu.push(a[i]);
qu.push(a[j]);
print();
return ;
}
}
}
}
for(int i=;i<=n;i++)
{
if(a[i]>)
{
for(int j=i+;j<=n;j++)
{
if(a[j]>&&!flag[a[i]+a[j]])
{
printf("2\n");
printf("%d %d",a[i],a[j]);
return ;
}
}
}
}
for(int i=;i<=n;i++)
{
if(a[i]>&&!flag[a[i]+])
{
qu.push(a[i]);
print();
return ;
}
}
print();
}
else
{
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
if(!flag[a[i]+a[j]])
{
qu.push(a[i]);
qu.push(a[j]);
print();
return ;
}
}
}
printf("1\n");
printf("%d",a[]); } return ;
}

coeforces 665D D. Simple Subset(最大团orsb题)的更多相关文章

  1. Educational Codeforces Round 12 D. Simple Subset 最大团

    D. Simple Subset 题目连接: http://www.codeforces.com/contest/665/problem/D Description A tuple of positi ...

  2. CodeFores 665D Simple Subset(贪心)

    D. Simple Subset time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  3. Codeforces 665D Simple Subset【构造】

    题目链接: http://codeforces.com/problemset/problem/665/D 题意: 给定序列,从中找出最大的子集,使得子集中的数两两相加均为质数. 分析: 貌似有用最大团 ...

  4. Codeforces 665D Simple Subset [简单数学]

    题意: 给你n个数,让你从中选一个子集要求子集中的任何两个数相加都是质数. 思路: 一开始把自己坑了,各种想,后来发现一个简单的性质,那就是两个数相加的必要条件是这两个数之中必定一个奇数一个偶数,(除 ...

  5. codeforces 665D Simple Subset

    题目链接 给一个数列, 让你选出其中的m个数, 使得选出的数中任意两个数之和都为质数, m尽可能的大. 首先, 除了1以外的任意两个相同的数相加结果都不是质数. 然后, 不考虑1的话, 选出的数的个数 ...

  6. CodeForces - 665D Simple Subset 想法题

    //题意:给你n个数(可能有重复),问你最多可以取出多少个数使得任意两个数之和为质数.//题解:以为是个C(2,n)复杂度,结果手摸几组,发现从奇偶性考虑,只有两种情况:有1,可以取出所有的1,并可以 ...

  7. POJ 3692 幼儿园做游戏 最大团 模板题

    Kindergarten Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6191   Accepted: 3052 Desc ...

  8. 水题:HDU-1088-Write a simple HTML Browser(模拟题)

    解题心得: 1.仔细读题,细心细心...... 2.题的几个要求:超过八十个字符换一行,<br>换行,<hr>打印一个分割线,最后打印一个新的空行.主要是输出要求比较多. 3. ...

  9. 洛谷P1466 集合 Subset Sums_01背包水题

    不多解释,适当刷刷水… Code: #include<cstdio> #include<algorithm> using namespace std; const int ma ...

随机推荐

  1. Java加密技术(八)——数字证书

    原文:http://snowolf.iteye.com/blog/391931 请大家在阅读本篇内容时先阅读 Java加密技术(四),预先了解RSA加密算法. 在构建Java代码实现前,我们需要完成证 ...

  2. FastDFS 使用经验分享

    原文:http://www.ttlsa.com/fastdfs/fastdfs-experience-sharing/ 应用背景 文件被上传到FastDFS后Storage服务端将返回的文件索引(FI ...

  3. 卸载ArcGISDesktop低版本程序遇到异常,如何完全卸载?

    [解决办法]:正常情况下,运行 ArcGIS for Desktop 光盘中的 “冲突检测”工具,会自动完全卸载低版本的ArcGIS 程序.如果遇到异常无法卸载(例如安装过非正式版软件),如下位置是A ...

  4. 【Protocol Buffers】grpc默认使用的Google 开源的一套成熟的结构数据序列化机制

    grpc默认使用的Google 开源的一套成熟的结构数据序列化机制 参考地址:https://blog.csdn.net/shensky711/article/details/69696392 参考地 ...

  5. iOS开发 准确计算Coretext高度

    - (int)getAttributedStringHeightWithString:(NSAttributedString *)  string  WidthValue:(int) width{   ...

  6. cache数据库之表的存储结构

    1.我们已经建了一个person类,接下来就是表的存储结构 2.打开Inspector,先输入rowid名字为p_RowID,选class->Storage 3.新建一个Storage,选择Ca ...

  7. react 自定义 百度地图(BMap)组件

    1.html 页面引入 相关js public/index.html <!DOCTYPE html> <html lang="en"> <head&g ...

  8. linux系列之-—02 设置和查看环境变量

    一.Linux环境变量种类 按变量的生存周期来划分,Linux变量可分为两类: 1 永久的:需要修改配置文件,变量永久生效. 2 临时的:使用export命令声明即可,变量在关闭shell时失效. 二 ...

  9. GG链路过多port不足导致的报错OGG-01223

    假设我们GG同步链路在增多.就有可能出现这个报错.在日志中能体现. 2014-05-20 13:32:38 WARNING OGG-01223 TCP/IP error 111 (Connection ...

  10. web前端面试系列 - 算法( 数组去重 )

    1. 思路:设置一个临时数组temp,然后遍历要去重的数组arr,如果arr中的元素能够在temp中找到,则跳过此元素,否则将此元素存入temp,最后返回temp. 实现一 function uniq ...