题目链接:

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. Go -- pprof协程监控

    go中有pprof包来做代码的性能监控,在两个地方有包: net/http/pprof runtime/pprof 其实net/http/pprof中只是使用runtime/pprof包来进行封装了一 ...

  2. JSP/SERVLET新手教程--Servlet 使用入门

    如今的JSP书籍有的是直接讲述JSP的使用,然后再解说SERVERLET的使用;也有书籍是先讲述SERVERLET的使用,然后解说JSP使用.个人觉得另外一种相对好一些,至于原因大家能够在学习体会到! ...

  3. 一天时间用OpenFire打造自己的IM聊天工具

    Openfire采用Java开发,开源的实时协作(RTC)服务器基于XMPP(Jabber)协议.Openfire安装和使用都非常简单,并利用Web进行管理.单台服务器可支持上万并发用户. 好友界面 ...

  4. SVN服务器配置说明 【转】

    http://www.cnblogs.com/ricksun/articles/1564905.html 1.前 言 花了72小时,终于把 Subversion 初步掌握了.从一个连“什么是版本控制” ...

  5. angular - 编辑html文件-4

    启动服务器: angular默认端口:4200 ng serve --port 3000 --open 输入本条命令后,会自动打开默认浏览器以及打开APP页 推荐开发工具webStorm,全平台兼容M ...

  6. sql导入数据库

    有问题可以尝试加上这句 DROP TABLE IF EXISTS `t_saler_login_log`;

  7. sql 表的部分字段查找 的结果集

    传统sql从多个对象中获得的list<Object> ,可以这样处理(利用Map)  List list = query.getList(sql);  //封装成BB类型  List< ...

  8. C#中的 SET ,GET

    C#中get和SET,看来看去还是看不懂,通俗一点解释一下,用了有什么好处,不用会怎么样如果你这样写是没有什么不一样的. private int __Old; public int Old{ get{ ...

  9. Model Vaildation

    https://docs.asp.net/en/latest/mvc/models/validation.html 许多有用的验证属性都必须引用命名空间: System.ComponentModel. ...

  10. byte数组和文件的相互转换

    /** * 获得指定文件的byte数组 */ private byte[] getBytes(String filePath){ byte[] buffer = null; try { File fi ...