coeforces 665D D. Simple Subset(最大团orsb题)
題目鏈接:
D. Simple Subset
time limit per test 1 second memory limit per test 256 megabytes input standard input output standard outputA 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.
?
InputThe 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.
?
OutputOn 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 22 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
題意:
選一個(gè)最大的子序列,滿足這個(gè)序列里的任意兩個(gè)數(shù)的和是素?cái)?shù);
思路:
可以是一個(gè)最大完全數(shù)的題,也可以是水題,因?yàn)槠鏀?shù)+奇數(shù)=偶數(shù),偶數(shù)+偶數(shù)=偶數(shù),(1除外);所以最多有一個(gè)奇數(shù)和一個(gè)偶數(shù);
我寫的分情況討論的代碼真是跟翔一樣;
AC代碼:
#include <bits/stdc++.h> using namespace std; const int N=2e6+6; typedef long long ll; int n,a[1010],flag[N]; int get_prime() {for(int i=2;i<N;i++){if(!flag[i]){for(int j=2*i;j<N;j+=i){flag[j]=1;}}} } 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=0;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&a[i]);if(a[i]==1){f++;qu.push(1);}}if(f>1){for(int i=1;i<=n;i++){if(a[i]>1&&!flag[a[i]+1]){for(int j=i+1;j<=n;j++){if(a[j]>1&&!flag[a[i]+a[j]]&&!flag[a[j]+1]){qu.push(a[i]);qu.push(a[j]);print();return 0;}}}}for(int i=1;i<=n;i++){if(a[i]>1&&!flag[a[i]+1]){qu.push(a[i]);print();return 0;}}print();}else if(f==1){for(int i=1;i<=n;i++){if(a[i]>1&&!flag[a[i]+1]){for(int j=i+1;j<=n;j++){if(a[j]>1&&!flag[a[i]+a[j]]&&!flag[a[j]+1]){qu.push(a[i]);qu.push(a[j]);print();return 0;}}}}for(int i=1;i<=n;i++){if(a[i]>1){for(int j=i+1;j<=n;j++){if(a[j]>1&&!flag[a[i]+a[j]]){printf("2\n");printf("%d %d",a[i],a[j]);return 0;}}}}for(int i=1;i<=n;i++){if(a[i]>1&&!flag[a[i]+1]){qu.push(a[i]);print();return 0;}}print();}else{for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++){if(!flag[a[i]+a[j]]){qu.push(a[i]);qu.push(a[j]);print();return 0;}}}printf("1\n");printf("%d",a[1]);}return 0; }
?
轉(zhuǎn)載于:https://www.cnblogs.com/zhangchengc919/p/5421033.html
總結(jié)
以上是生活随笔為你收集整理的coeforces 665D D. Simple Subset(最大团orsb题)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 有没有大神知道国产加密算法SM2的详细介
- 下一篇: Technical Artist的不归路