-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathPermutation Prime Sums.cpp
More file actions
57 lines (51 loc) · 1.23 KB
/
Permutation Prime Sums.cpp
File metadata and controls
57 lines (51 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define endl '\n'
const int N = 1000000;
vector<char> is_prime(N+1, true);
vector<int> primes;
void seive(){
is_prime[0] = is_prime[1] = false;
is_prime[2] = true;
for (int i = 3; i * i <= N; i+=2) {
if (is_prime[i]) {
for (int j = i * i; j <= N; j += i)
is_prime[j] = false;
}
}
primes.push_back(2);
for(int i=3;i<N;i+=2){ // if we need only the mapping then remove this
if( is_prime[i] ) primes.push_back( i );
}
}
void solve() {
int n;
cin >> n;
int ara[n+1] = {0};
int cur = n;
while(cur > 0) {
int prime = *upper_bound(primes.begin(),primes.end(),cur);
int a = prime/2;
int b = prime - a;
while(a>0 and b<=cur) {
ara[a] = b;
ara[b] = a;
a--;
b++;
}
cur = a;
}
for(int i=1;i<=n;i++) cout<<i<<" "; cout<<endl;
for(int i=1;i<=n;i++) cout<<ara[i]<<" "; cout<<endl;
}
signed main() {
seive();
FASTIO;
int tc = 1;
// cin>>tc;
for(int i=1;i<=tc;i++) {
solve();
}
}