zoukankan      html  css  js  c++  java
  • CodeForces

    //题意:给你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 + 5) * 2;
    const int mod = 1e9 + 7;
    int isp[maxn];
    int p[1005];
    void setp() {
    
        for (int i = 1; i <= maxn; i++)isp[i] = 1;
        isp[1] = 0;
        for (int i = 2; i*i <= maxn; i++)if (isp[i]) {
            int x = i;
            while (x*i <= maxn) {
                isp[x*i] = 0;
                x++;
            }
        }
    }
    int main() {
        int n;
        cin >> n;
        int cnt = 0, c2 = 0;
        setp();
        //for(int i=1;i<=5;i++)if(isp[i])cout<<i<<' ';
        for (int i = 1; i <= n; i++) {
            cin >> p[i];
            if (p[i] == 1)cnt++;
            if (p[i] == 2)c2 = 1;//题目记错了  
        }
        if (cnt >= 2) {
            //cout << cnt + c2 << endl;
            int tmp=0;
            for (int i = 1; i <= n; i++)if (p[i] % 2 == 0) {
                if (isp[p[i] + 1])tmp = p[i];
            }
            if (tmp==0)cout << cnt;
            else cout << cnt + 1;
            cout << endl;
            while (cnt--)cout << 1 << ' ';
            if (tmp)cout << tmp;
            cout << endl;
    
            //if (c2)cout << 2;
        }
        else {
            
            sort(p + 1, p + n + 1);
            for (int i = 1; i<n; i++) {
                for (int j = i + 1; j <= n; j++) {
                    if (isp[p[i] + p[j]]) { printf("%d
    %d %d", 2, p[i], p[j]); return 0; }
                }
            }
            int okk = 0;
            for (int i = 1; i <= n; i++)if (isp[p[i]]) { okk = p[i]; break; }
            if (okk) printf("%d
    %d", 1, okk);
            else cout << 1 << endl, cout << p[1];//莫名其妙的一句,忘了当时为啥加的
    
        }
    }
    成功的路并不拥挤,因为大部分人都在颓(笑)
  • 相关阅读:
    英语翻译预测
    mybatis 实现增删改查
    jsp项目 在maven中使用,web.xml pome.xml 的配置
    来整理一份我觉得比较重要的小概念
    前端开发项目资源网站
    css 选择器符号
    css实现三栏布局的几种方法及优缺点
    MVC设计思想
    WebSocket 与 Polling , Long-Polling , Streaming 的比较!
    移动端视频h5表现问题汇总
  • 原文地址:https://www.cnblogs.com/SuuT/p/8561268.html
Copyright © 2011-2022 走看看