题目链接

给一个数列, 让你选出其中的m个数, 使得选出的数中任意两个数之和都为质数, m尽可能的大。



首先, 除了1以外的任意两个相同的数相加结果都不是质数。

然后, 不考虑1的话, 选出的数的个数不大于2。

假设我们选了3个数, a1, a2, a3。 a1+a2是质数的话, 那么a1, a2中一个为奇数一个为偶数。 那么如果a3无论为奇数或偶数都无法满足条件了。

所以我们按1出现的次数分类讨论一下就好了。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
int prime[2000006], a[1005], cnt[1000005];
void init() {
prime[1] = 1;
for(int i = 2; i <= 2000000; i++) {
if(!prime[i]) {
for(int j = i+i; j <= 2000000; j+=i)
prime[j] = 1;
}
}
}
int main()
{
init();
int n;
cin>>n;
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
cnt[a[i]]++;
}
if(cnt[1]>1) {
int ans = -1;
for(int j = 0; j < n; j++) {
if(!prime[a[j]+1]&&a[j]!=1) {
ans = j;
break;
}
}
cout<<cnt[1]+(ans>=0)<<endl;
for(int i = 0; i < cnt[1]; i++)
printf("1 ");
if(ans>=0)
cout<<a[ans]<<endl;
} else {
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
if(!prime[a[i]+a[j]]) {
cout<<2<<endl;
cout<<a[i]<<" "<<a[j]<<endl;
return 0;
}
}
}
for(int i = 0; i < n; i++) {
if(!prime[a[i]]) {
cout<<1<<endl;
cout<<a[i]<<endl;
return 0;
}
}
cout<<1<<endl<<a[0]<<endl;
}
return 0;
}

codeforces 665D Simple Subset的更多相关文章

  1. Codeforces 665D Simple Subset【构造】

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

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

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

  3. CodeForces - 665D Simple Subset 想法题

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

  4. CodeFores 665D Simple Subset(贪心)

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

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

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

  6. coeforces 665D D. Simple Subset(最大团orsb题)

    题目链接: D. Simple Subset time limit per test 1 second memory limit per test 256 megabytes input standa ...

  7. CodeForces 837D - Round Subset | Educational Codeforces Round 26

    /* CodeForces 837D - Round Subset [ DP ] | Educational Codeforces Round 26 题意: 选k个数相乘让末尾0最多 分析: 第i个数 ...

  8. Codeforces 626E Simple Skewness(暴力枚举+二分)

    E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...

  9. Codeforces 837D Round Subset - 动态规划 - 数论

    Let's call the roundness of the number the number of zeros to which it ends. You have an array of n ...

随机推荐

  1. ubuntu下nvm,node以及npm的安装与使用

    一:安装nvm 首先下载nvm,这里我们需要使用git,如果没有安装git,可以使用 sudo apt-get install git 来安装 git clone https://github.com ...

  2. JS赋值传递的问题

    根据值的类型是基本类型值还是复杂类型的值在传递时会有不同. JS函数的参数传递是按值传递,基本类型值传递的是副本,复杂类型值传递的是引用.从而会影响原来的值,不会改变原来的复制前的引用. functi ...

  3. BZOJ 1564: [NOI2009]二叉查找树( dp )

    树的中序遍历是唯一的. 按照数据值处理出中序遍历后, dp(l, r, v)表示[l, r]组成的树, 树的所有节点的权值≥v的最小代价(离散化权值). 枚举m为根(p表示访问频率): 修改m的权值 ...

  4. UML中的交互图<转>

      转自>>http://blog.csdn.net/mingxuanyun/article/details/8572128 交互图用来描述系统中的对象是如何进行相互作用的,即一组对象是如 ...

  5. Java 集合嵌套List of List

    在LeetCode上遇到这样返回值 public class Solution { public List<List<Integer>> levelOrder(TreeNode ...

  6. android4.0 HttpClient 以后不能在主线程发起网络请求

    android4.0以后不能在主线程发起网络请求,该异步网络请求. new Thread(new Runnable() { @Override public void run() { // TODO ...

  7. docker 容器管理上

    Docker 容器管理: docker create -it centos //这样可以创建一个容器,但该容器并没有启动: docker start container_id //启动容器后,可以使用 ...

  8. css案例学习之父子块的margin

    两边还会有些距离,这是body默认的. 代码: <head> <title>父子块的margin</title> <style type="text ...

  9. 解决 MySQL manager or server PID file could not be found! 的方法

    [root@centos var]# service mysqld stop MySQL manager or server PID file could not be found!       [F ...

  10. ios常用的框架(源自知乎上的回答)

    首先,学习这类开源项目的主要目的是为了实现产品汪需求,如果不是这个目的,完全可以看Explore GitHub,当前最火的开源项目都在这里,当然你需要过滤一下语言. 好了,介绍几个希望能帮助到你. 普 ...