1844B - Permutations Primes - CodeForces Solution


constructive algorithms greedy math

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
void init_code(){ 
    #ifndef ONLINE_JUDGE 
    freopen("input.txt", "r", stdin); 
    freopen("output.txt", "w", stdout); 
    #endif // ONLINE_JUDGE
} 
int32_t main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    init_code();
    int t;cin>>t; 
    while(t--){ 
        int n;
        cin>>n; 
        if(n==1) cout<<"1"<<endl;
        else if(n==2) cout<<"2 1"<<endl;
        else if(n==3) cout<<"2 1 3"<<endl; 
        else if(n==4) cout<<"2 1 4 3"<<endl;
        else{
            vector<int>v;
            v.push_back(2);
            for(int i=1;i<=n;i++) if(i!=2)v.push_back(i); 
            swap(v[2],v[n-1]); 
            swap(v[1],v[n/2]);
            for(auto &i:v) cout<<i<<" ";
            cout<<endl;
        }
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

454B - Little Pony and Sort by Shift
1152A - Neko Finds Grapes
1719B - Mathematical Circus
1719C - Fighting Tournament
1642A - Hard Way
285C - Building Permutation
1719E - Fibonacci Strings
1696C - Fishingprince Plays With Array
1085A - Right-Left Cipher
1508B - Almost Sorted
1690C - Restoring the Duration of Tasks
1055A - Metro
1036D - Vasya and Arrays
1139C - Edgy Trees
37A - Towers
353A - Domino
409H - A + B Strikes Back
1262A - Math Problem
158C - Cd and pwd commands
194A - Exams
1673B - A Perfectly Balanced String
1104B - Game with string
1169B - Pairs
1567D - Expression Evaluation Error
78A - Haiku
1287A - Angry Students
1428A - Box is Pull
234B - Reading
581B - Luxurious Houses
1481C - Fence Painting