codeforces 665D Simple Subset
题目链接
给一个数列, 让你选出其中的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的更多相关文章
- Codeforces 665D Simple Subset【构造】
题目链接: http://codeforces.com/problemset/problem/665/D 题意: 给定序列,从中找出最大的子集,使得子集中的数两两相加均为质数. 分析: 貌似有用最大团 ...
- Codeforces 665D Simple Subset [简单数学]
题意: 给你n个数,让你从中选一个子集要求子集中的任何两个数相加都是质数. 思路: 一开始把自己坑了,各种想,后来发现一个简单的性质,那就是两个数相加的必要条件是这两个数之中必定一个奇数一个偶数,(除 ...
- CodeForces - 665D Simple Subset 想法题
//题意:给你n个数(可能有重复),问你最多可以取出多少个数使得任意两个数之和为质数.//题解:以为是个C(2,n)复杂度,结果手摸几组,发现从奇偶性考虑,只有两种情况:有1,可以取出所有的1,并可以 ...
- CodeFores 665D Simple Subset(贪心)
D. Simple Subset time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Educational Codeforces Round 12 D. Simple Subset 最大团
D. Simple Subset 题目连接: http://www.codeforces.com/contest/665/problem/D Description A tuple of positi ...
- coeforces 665D D. Simple Subset(最大团orsb题)
题目链接: D. Simple Subset time limit per test 1 second memory limit per test 256 megabytes input standa ...
- CodeForces 837D - Round Subset | Educational Codeforces Round 26
/* CodeForces 837D - Round Subset [ DP ] | Educational Codeforces Round 26 题意: 选k个数相乘让末尾0最多 分析: 第i个数 ...
- Codeforces 626E Simple Skewness(暴力枚举+二分)
E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...
- 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 ...
随机推荐
- C++ Primer之 十二章 类
1.关于类的const对象 const对象只能调用声明为const的成员函数,在这篇csdn博客中也讨论了这个问题. 究其原因是因为 const 对象A 调用了非const函数F, F因为没有cons ...
- C# C++ Java接口类型转换
最近这几天做了一个兼职 ,主要是把C语言以及C#语言封装的dll,经过C++中转为Java语言支持的,主要其中的问题就是类型转换,在此列出常用类型的转换过程. #include "stdaf ...
- linux 系统中的特殊文件
特殊文件是UNIX系统中最具特色的文件特性之一.特殊文件也称设备文件.提供用户访问外部设备,而不必知道各种设备的具体操作.UNIX利用特殊文件作为用户与设备文件的接口,使用户能像访问普通文件那样访问特 ...
- cvReleaseImage()函数说明
IplImage *tmp = NULL; 这一句话定义了一个IplImage类型的指针变量tmp,这个指针变量在内存中的示意图为: tmp = cvLoadImage(file_path); 这句话 ...
- linux 系统分区方案建议
前言: 以前初识Linux时,对Linux系统安装时分区的选择,一点都不了解,导致几次没法进行下一步安装,因此就静下心来,专门拿出时间研究了研究这方面的知识: 以下内容就是以前通过研究Linux安装过 ...
- 给Extjs的GridPanel增加“合计”行(转)
再Google,找到一个看似写的比较好的 http://www.cnblogs.com/over140/archive/2009/05/06/1449892.html 期间主要部分也是借鉴官方论坛上的 ...
- JS取地址栏参数的两种方法
第一种方法: function GetQueryString(name){ var reg = new RegExp("(^|&)"+ name +"=([^&a ...
- requireJS配置选项
1. baseUrl 当设置的目录最前面带有/,则是从电脑的根目录开始算起,若是baseUrl='another/path' 则是从require.js的目录开始算起 而且当require(['a. ...
- shopncv4 短信接口 提供商 中国短信网
前提是在后台开启手机注册功能:具体是在设置->账号同步->手机短信 里开启. 修改 siteroot\core\framework\libraries\sms.php 修改 sit ...
- 有了bootstrap,为什么还要做amaze ui
1.Bootstrap介绍Bootstrap,来自 Twitter,是目前很受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加 ...