CodeForces - 665D Simple Subset 想法题
//题意:给你n个数(可能有重复),问你最多可以取出多少个数使得任意两个数之和为质数。
//题解:以为是个C(2,n)复杂度,结果手摸几组,发现从奇偶性考虑,只有两种情况:有1,可以取出所有的1,并可以再取一个偶数(如果这个偶数+1是质数)。没有1,如果取了一个奇质数,那只能再拿一个2(如果有2的话)。
坑:一度把题目记错了,以为输入的是质数,结果比赛的时候一直wa到orz
ac代码:
#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = (1e6 + ) * ;
const int mod = 1e9 + ;
int isp[maxn];
int p[];
void setp() { for (int i = ; i <= maxn; i++)isp[i] = ;
isp[] = ;
for (int i = ; i*i <= maxn; i++)if (isp[i]) {
int x = i;
while (x*i <= maxn) {
isp[x*i] = ;
x++;
}
}
}
int main() {
int n;
cin >> n;
int cnt = , c2 = ;
setp();
//for(int i=1;i<=5;i++)if(isp[i])cout<<i<<' ';
for (int i = ; i <= n; i++) {
cin >> p[i];
if (p[i] == )cnt++;
if (p[i] == )c2 = ;//题目记错了
}
if (cnt >= ) {
//cout << cnt + c2 << endl;
int tmp=;
for (int i = ; i <= n; i++)if (p[i] % == ) {
if (isp[p[i] + ])tmp = p[i];
}
if (tmp==)cout << cnt;
else cout << cnt + ;
cout << endl;
while (cnt--)cout << << ' ';
if (tmp)cout << tmp;
cout << endl; //if (c2)cout << 2;
}
else { sort(p + , p + n + );
for (int i = ; i<n; i++) {
for (int j = i + ; j <= n; j++) {
if (isp[p[i] + p[j]]) { printf("%d\n%d %d", , p[i], p[j]); return ; }
}
}
int okk = ;
for (int i = ; i <= n; i++)if (isp[p[i]]) { okk = p[i]; break; }
if (okk) printf("%d\n%d", , okk);
else cout << << endl, cout << p[];//莫名其妙的一句,忘了当时为啥加的 }
}
CodeForces - 665D Simple Subset 想法题的更多相关文章
- Codeforces 665D Simple Subset【构造】
题目链接: http://codeforces.com/problemset/problem/665/D 题意: 给定序列,从中找出最大的子集,使得子集中的数两两相加均为质数. 分析: 貌似有用最大团 ...
- Codeforces 665D Simple Subset [简单数学]
题意: 给你n个数,让你从中选一个子集要求子集中的任何两个数相加都是质数. 思路: 一开始把自己坑了,各种想,后来发现一个简单的性质,那就是两个数相加的必要条件是这两个数之中必定一个奇数一个偶数,(除 ...
- codeforces 665D Simple Subset
题目链接 给一个数列, 让你选出其中的m个数, 使得选出的数中任意两个数之和都为质数, m尽可能的大. 首先, 除了1以外的任意两个相同的数相加结果都不是质数. 然后, 不考虑1的话, 选出的数的个数 ...
- CodeFores 665D Simple Subset(贪心)
D. Simple Subset time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- CodeForces - 344B Simple Molecules (模拟题)
CodeForces - 344B id=46665" style="color:blue; text-decoration:none">Simple Molecu ...
- codeforces 704B - Ant Man [想法题]
题目链接:http://codeforces.com/problemset/problem/704/B ------------------------------------------------ ...
- coeforces 665D D. Simple Subset(最大团orsb题)
题目链接: D. Simple Subset time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Educational Codeforces Round 12 D. Simple Subset 最大团
D. Simple Subset 题目连接: http://www.codeforces.com/contest/665/problem/D Description A tuple of positi ...
- CodeForces 111B - Petya and Divisors 统计..想法题
找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...
随机推荐
- window.location.href
WEB设置首页 <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-f ...
- swoole的进程模型架构
swoole的强大之处就在与其进程模型的设计,既解决了异步问题,又解决了并行. 主线程MainReactor swoole启动后主线程会负责监听server socket,如果有新的连接accept, ...
- My Apple Developer Library Catalog
Objective-C & Memory Management:Programming with Objective-CConcepts in Objective-C ProgrammingM ...
- CM和CDH的安装-进阶完成
安装Cloudera Manager Server 和Agent 1.在cdh1解压cloudera-manager-el6-cm5.9.0_x86_64.tar.gz(cdh1节点)tar -zcv ...
- drawCall_01
在屏幕上渲染物体,引擎需要发出一个绘制调用来访问图形API(iOS系统中为OpenGL ES).每个绘制调用需要进行大量的工作来访问图形API,从而导致了CPU方面显著的性能开销. Unity在运 ...
- 嵌入式之UBOOT
嵌入式Linux系统的结构分为四个区,如图所示: 1.Bootloader区存放的是Bootloader,Coidre972开发板上使用的uboot,它负责嵌入式系统最初的硬件初始化.驱动和内核加载. ...
- C语言中的正负数及其输出
在数学中,数字有正负之分.在C语言中也是一样,short.int.long 都可以带上正负号,例如: //负数 ; short a2 = -0x2dc9; //十六进制 //正数 ; ; //八进制 ...
- js控制滚动条的位置以及隐藏滚动条
document.documentElement.style.overflow = 'hidden'; //隐藏横竖滚动条 window.scrollTo(0,document.body.scroll ...
- 使用kendynet构建异步redis访问服务
使用kendynet构建异步redis访问服务 最近开始在kendynet上开发手游服务端,游戏类型是生存挑战类的,要存储的数据结构和类型都比较简单,于是选择了用redis做存储,数据类型使用stri ...
- try except与try finally不同之处
try except与try finally不同之处 try//尝试执行 {SomeCode} except//出错的时候执行, Except有特定的错误类型 {SomeCode} end; t ...