题目链接:

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. 【spring boot】13.在spring boot下使用多线程

    使用场景: 方法处理到某一步,需要将信息交给另一个线程去处理!! =================================================================== ...

  2. pycharm5.0 激活方式

    Pycharm5注册方式   0x1 ,安装 0x2 , 调整时间到2038年. 0x3 ,申请30天试用 0x4, 退出pycharm 0x5, 时间调整回来. ##注册方法2### 注册方法:   ...

  3. MyEclipse的html/JSP编辑器添加代码自动提示

    http://lusterfly.iteye.com/blog/1872627 在myeclipse 9以前的版本中,我们如果要为html编辑器添加自动的代码提示可以这样操作: windows--&g ...

  4. Something about cache

    http://www.tyut.edu.cn/kecheng1/2008/site04/courseware/chapter5/5.5.htm 5.5 高速缓冲存储器cache 随着CPU时钟速率的不 ...

  5. 2.Qt Creator的使用

    下面以一个简单的程序来说明Qt Creator的使用: 首先,按图片步骤创建一个Qt项目 创建完成后 上图标记处工具栏提供了简化树形视图.分栏等功能(自行尝试吧...) 在使用Qt制作一个界面时,我们 ...

  6. python3短信接口使用

    import http.client from urllib import parse host = "106.ihuyi.com" sms_send_uri = "/w ...

  7. Oracle SQL_杂记

    1. 查询当前用户角色,当前用户下的表权限以及所有用户表权限 desc user_role_privs;select * from user_role_privs; desc user_tab_pri ...

  8. 08 comet反向ajax

    一:HTTP协议与技久链接+分块传输---->反向ajax 反向ajax又叫comet, server push,服务器推技术. 应用范围: 网页聊天服务器,, 新浪微博在线聊天,google ...

  9. Java ClassLoader详解(转载)

    Java ClassLoader详解 类加载器是 Java 语言的一个创新,也是 Java 语言流行的重要原因之一.它使得 Java 类可以被动态加载到 Java 虚拟机中并执行.类加载器从 JDK ...

  10. linux关机命令详解(转载)

    在linux下一些常用的关机/重启命令有shutdown.halt.reboot.及init,它们都可以达到重启系统的目的,但每个命令的内部工作过程是不同的. Linux centos重启命令: 1. ...