Educational Codeforces Round 12 D. Simple Subset
来源:互联网 发布:淘宝起名字 编辑:程序博客网 时间:2024/06/11 08:41
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 a with 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 froma by removing some (possibly all) elements of it.
The first line contains integer n (1 ≤ n ≤ 1000) — the number of integers in the arraya.
The second line contains n integers ai (1 ≤ ai ≤ 106) — the elements of the arraya.
On the first line print integer m — the maximum possible size of simple subset ofa.
On the second line print m integers bl — the elements of the simple subset of the arraya 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.
22 3
23 2
22 2
12
32 1 1
31 1 2
283 14
214 83题意:给你一个序列,问你能保证序列中每两个元素和为质数的最长的子序列有多长。分析:把1要单独考虑,如果有多于两个1就去找剩下有没有a[i]+1是质数的,如果有就加进来没有就输出所有的1;其他情况就看能不能找到两个数使其和为素数。#include <cstdio>#include <iostream>#include <algorithm>using namespace std;int n,tot,cnt,prim[200000],a[1005];bool jud[2000005],f[2000005];int main(){jud[1] = true;for(int i = 2;i <= 2000000;i++) if(!jud[i]) {for(int j = i*2;j <= 2000000;j+=i) jud[j] = true;prim[++tot] = i; }cin>>n;for(int i = 1;i <= n;i++) cin>>a[i];sort(a+1,a+1+n);for(int i = 1;i <= n;i++){if(a[i] == 1) cnt++;f[a[i]] = true;}if(cnt > 1){for(int i = 2;i <= tot;i++) if(f[prim[i] - 1]) { cout<<cnt+1<<endl; for(int j = 1;j <= cnt;j++) cout<<1<<" "; cout<<prim[i]-1<<endl; return 0; }cout<<cnt<<endl;for(int j = 1;j <= cnt;j++) cout<<1<<" ";return 0;} else {for(int i = 1;i <= n;i++) for(int j = 2;j <= tot;j++) if(prim[j] > a[i] && f[prim[j]-a[i]]) { cout<<2<<endl; cout<<a[i]<<" "<<prim[j]-a[i]<<endl; return 0; } for(int i = 1;i <= n;i++) if(!jud[a[i]]){cout<<1<<endl;cout<<a[i]<<endl;return 0;} cout<<1<<endl; cout<<a[1]<<endl; return 0;}}
- Educational Codeforces Round 12 D. Simple Subset
- Educational Codeforces Round 12 D. Simple Subset
- Educational Codeforces Round 26 D Round Subset
- cf Educational Codeforces Round 26 D. Round Subset
- Educational Codeforces Round 12 C.Simple Strings
- CodeForces 665D Simple Subset
- Codeforces 665D. Simple Subset
- codeforces 665D Simple Subset
- Codeforces 665D Simple Subset
- Educational Codeforces Round 26 D. Round Subset 滚动数组优化DP
- Codeforces 837 D Round Subset
- Educational Codeforces Round 21-D
- Educational Codeforces Round 5 D
- Educational Codeforces Round 26 D
- Educational Codeforces Round 12 C. Simple Strings 贪心
- [思维] codeforces 665D. Simple Subset
- Codeforces 665D Simple Subset【构造】
- Codeforces-837D Round Subset(dp)
- 【codevs 1106】篝火晚会 (2005年NOIP全国联赛提高组)(置换群)
- 数组随机访问计算方法
- Sqlite demo 和总结
- Android Studio 中build.gradle文件的详细解析
- Spark兼容Hive入门解析
- Educational Codeforces Round 12 D. Simple Subset
- Github资源整理
- k8s 部署
- c++之继承1
- 作业——在线学习Android课程之第十二周(内存优化)
- jQuery uploadify 在chrome上崩溃的解决办法
- 简单炫酷的轮播图
- 编码规范 目录管理
- 自增id算法snowflake