Kaosar and M(ex+od)#12362

Kaosar and M(ex+od) (Editorial)

Problem Author:

Al Kaosar Hussain

Prepared by:

Al Kaosar Hussain

Solution

The main observation for this problem is that i\mod i+1=i.

Now this problem can be solved in many ways here

is one.There are two base cases for k=0,1.

For k=0 one valid solution is 1,2,..,n and for k=1 one valid solution is 2,3,...n,1 and for n<k

there are no valid permutation.

The general case is if we include at first of then integers from

1,2,..,k-1,k+1,..,n then p\mod 1 will contribute and 1\mod 2,2\mod 3,..,k-1\mod k+1 will

contribute 1,2,3,...,k-1 and the mex will be turned into exactly and which is an optimal solution

for this problem.

#include <bits/stdc++.h>
using namespace std;

#define rep(i,a,b) for(int i = (a); i < (b); ++i)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

using ll = long long;
using pii = pair<int,int>;
using vi = vector<int>;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        int n;
        ll k;
        cin >> n >> k;
        if (k >= n) {
            cout << -1 <<end;
            continue;
        }
        if (k == 0) {
            rep(i, 1, n + 1) cout << i <<  " ";
            cout <<endl;
        } else if (k == 1) {
            rep(i, 2, n + 1) cout << i << " ";
            cout << 1 <<endl;
        } else {
            vi ans;
            ans.push_back((int)k);
            rep(i, 1, n + 1) {
                if (i != k) ans.push_back(i);
            }
            rep(i, 0, n) {
                cout << ans[i] <<  " ";
            }
            cout <<endl;
        }
    }

    return 0;
}