思路与easy版本如出一辙。
代码也相同:
#include<bits/stdc++.h>
using namespace std;
#define int long long
using T = pair<int, int>;
//set<int>S;
//unordered_map<int, int>mp;
const int N = 1e6 + 10;
int a[N];
bool isprime(int x) {
if (x == 1)return false;
int i = 2;
for (; i <= sqrt(x); ++i) {
if (x % i == 0)return false;
}
return true;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n; cin >> n;
int i;
int pos = n;
for (i = 1; i <= pos; ++i) {
if (a[i])break;
if (isprime(pos + i)) {
a[i] = pos;
pos = i - 1;
i = 0;
}
}
if (i > n)cout << -1 << endl;
else {
for (i = 1; i <= n; ++i) {
if (a[i] == 0) {
a[i] = a[i - 1] - 1;
cout << a[i] << " ";
}
else {
cout << a[i] << " ";
}
}
}
//for (i = 1; i <= n; ++i)cout << a[i] << " ";
return 0;
}