题解 | #小红的排列构造#(数学)

小红的排列构造

https://www.nowcoder.com/practice/2b0a9318cc0740d1a78eec06162e7bea?tpId=376&tqId=10097249&ru=/exam/oj&qru=/ta/15-days-help/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E9%259D%25A2%25E8%25AF%2595%26topicId%3D376

题目解析:

想满足a[i]+i不是质数

我们分奇偶情况

如果是奇数放在奇数位置,加起来一定为偶数

偶数放偶数位置加起来也一定为偶数

偶数必然不是质数(2除外)

因此对于n<3的情况特判一下,其他情况的构造就出来了

#include "bits/stdc++.h"

using namespace std;
#define int long long
#define endl "\n"
#define PII pair<int,int>
#define PIII pair<int,PII>
const int MOD = 1e9 + 7;
const int N = 3e5;

bool cmp(PII p1, PII p2) {
    return p1.first + p1.second < p2.first + p2.second;
}

bool isP(char t1, char t2) {
    int c1 = t1 - '0';
    int c2 = t2 - '0';
    if ((c1 + c2) & 1)return false;
    return true;
}

void slu() {
    int n;
    cin >> n;
    if (n <= 2) {
        cout << -1 << endl;
    } else {
        string t = "3 2 1";
        vector<int> a;
        for (int i = 4; i <= n; i++)a.push_back(i);
        cout << t;
        for (int i = 0; i < a.size(); i++)cout << " " << a[i];
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int T;
//    cin >> T;
    T = 1;
    while (T--)slu();

}

全部评论

相关推荐

逆流河上万仙退:我觉得佬没必要 学历在这里 去了也不会对履历有很大提升 只是有可能让自己更熟练 是我的话会更倾向于找暑期或者中大厂日常
查看13道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务