题目链接:

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. apk 签名

    给apk签名步骤:(比方apk名称是EasyMsg.apk) (1)将EasyMsg.apk包后缀改为zip, EasyMsg.zip (2)删除EasyMsg.zip文件包中的META-INF目录, ...

  2. ARC forbids Objective-C objects in structs or unions

    解决方法有二种: 1.在出错的地方加入__unsafe_unretained 2.关闭系统ARC.1.点击project   2.点击Build Setting    3.找到其以下的Objetive ...

  3. c#列表操作

    Enumerable[从元数据]   //        // 摘要:         //     从序列的开头返回指定数量的连续元素.        //        // 参数:        ...

  4. Controller//控制器

    #include<opencv2\core\core.hpp> #include<opencv2\imgproc\imgproc.hpp> #include<opencv ...

  5. 在eclipse中查找指定文件 [多种方法]

    在eclipse中查找指定文件   1.ctrl+h打开搜索界面 File Search: containing text填*,File name patterns填写hello.*,可以找到hell ...

  6. [GUIDE] How to Setup Ubuntu 16.04 LTS Xenial Xerus for Compiling Android ROMs

    With a new version of Ubuntu comes an update to my guide for setting up a build environment to compi ...

  7. MySQL-获取某天的数据

    今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 近7天 DAY) <= date(时间字段名) 近30天 DAY) & ...

  8. 宜人贷蜂巢ELK Stack之elasticsearch权限探索

    前言 上文<宜人贷蜂巢API网关技术解密之Netty使用实践>提到了,API网关“承外对内”,将外部请求,转发到内部各个抓取服务.在网关中,不仅可以做鉴权.加解密.路由.限流功能:如果想了 ...

  9. python pyinotify模块详解

    转载于http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=23504396&id=2929446 1年多前就看过相关内容了, ...

  10. Java链接MySql数据库(转)

    import java.sql.*; public class JDBCTest { public static void main(String[] args){ // 驱动程序名 String d ...