coeforces 665D D. Simple Subset(最大团orsb题)
题目链接:
1 second
256 megabytes
standard input
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.
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.
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.
2
2 3
2
3 2
2
2 2
1
2
3
2 1 1
3
1 1 2
2
83 14
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题)的更多相关文章
- Educational Codeforces Round 12 D. Simple Subset 最大团
D. Simple Subset 题目连接: http://www.codeforces.com/contest/665/problem/D Description A tuple of positi ...
- CodeFores 665D Simple Subset(贪心)
D. Simple Subset time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces 665D Simple Subset【构造】
题目链接: http://codeforces.com/problemset/problem/665/D 题意: 给定序列,从中找出最大的子集,使得子集中的数两两相加均为质数. 分析: 貌似有用最大团 ...
- Codeforces 665D Simple Subset [简单数学]
题意: 给你n个数,让你从中选一个子集要求子集中的任何两个数相加都是质数. 思路: 一开始把自己坑了,各种想,后来发现一个简单的性质,那就是两个数相加的必要条件是这两个数之中必定一个奇数一个偶数,(除 ...
- codeforces 665D Simple Subset
题目链接 给一个数列, 让你选出其中的m个数, 使得选出的数中任意两个数之和都为质数, m尽可能的大. 首先, 除了1以外的任意两个相同的数相加结果都不是质数. 然后, 不考虑1的话, 选出的数的个数 ...
- CodeForces - 665D Simple Subset 想法题
//题意:给你n个数(可能有重复),问你最多可以取出多少个数使得任意两个数之和为质数.//题解:以为是个C(2,n)复杂度,结果手摸几组,发现从奇偶性考虑,只有两种情况:有1,可以取出所有的1,并可以 ...
- POJ 3692 幼儿园做游戏 最大团 模板题
Kindergarten Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6191 Accepted: 3052 Desc ...
- 水题:HDU-1088-Write a simple HTML Browser(模拟题)
解题心得: 1.仔细读题,细心细心...... 2.题的几个要求:超过八十个字符换一行,<br>换行,<hr>打印一个分割线,最后打印一个新的空行.主要是输出要求比较多. 3. ...
- 洛谷P1466 集合 Subset Sums_01背包水题
不多解释,适当刷刷水… Code: #include<cstdio> #include<algorithm> using namespace std; const int ma ...
随机推荐
- Please configure Spring facet or use 'Create Default Context' to add one including all unmapped files.
有时候我们刚进入 Intellij IDEA时会出现这样一个情况,原因是IDEA没有找到spring的配置文件,我们需要添加spring文件给idea管理 参考: 1.https://www.jetb ...
- 关于查看python的trace的方法
lptrace本质上是基于GDB的,进入到进程内存空间,然后执行了一段python指令把当时的trace给print出来 使用工具:https://github.com/khamidou/lptrac ...
- SilverLight:布局(2)GridSplitter(网格分割)垂直分割、水平分割
ylbtech-SilverLight-Layout: 布局(2)GridSplitter(网格分割)垂直分割.水平分割 A, Splitter(分割)对象之 GridSplitter(网格分割)1: ...
- js json 对象
JSON 语法规则 JSON 语法是 JavaScript 对象表示法语法的子集. 数据在名称/值对中 数据由逗号分隔 大括号保存对象 中括号保存数组 JSON 名称/值对 JSON 数据的书写格式是 ...
- C#自定义类型数组排序
在数组或者集合中对自定义类型进行排序分为两种方法. 1.如果这个自定义类型是自己定义编写的,那么我可以使它继承ICompareable<T>接口,实现其中的CompareTo(Object ...
- Vb.net/VB 声明API函数实现父窗口功能
回想第一次敲机房收费.自己调用了api函数实现了父窗口及其子窗口最小化的功能.现在再次遇到,自己就在思考,能不能继续使用API函数呢?答案当然是Of Course! 事实上细致看两者并没有多大的差别. ...
- Java集合01----ArrayList的遍历方式及应用
Java集合01----ArrayList的遍历方式及应用 前面已经学习了ArrayList的源代码,为了学以 ...
- MyEclipse 设置智能提示
choice 1: -->window→Preferences→Java→Editor→Content Assist, --->将Auto activation delay 的数值改为一个 ...
- 笔记13 winform
一:panel 和 tablelayoutpanel,FlowLayoutPanel FlowLayoutPanel和TableLayoutPanel是.NET Framework的新增控件.顾名思义 ...
- angular.js 入门
1.安装nodejs 首先要安装nodejs,如果你的电脑已经装过了,最好确认是比较新的版本,否则可能会出问题. 没有安装的直接去nodejs官网下载nodejs安装.安装过程很简单,官网有教程. 安 ...