Problem: 2049C - MEX Cycle
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;



#define arrin   ll n; cin>>n; int a[n];for(int i=0;i<n;i++)cin>>a[i];

#define arrout  for(int i=0;i<N;i++){cout<<dp[i]<<" ";}cout<<endl;

#define f(a,n)    for(int it=a;it<n;it++)

#define test    ll t; cin>>t; while(t--){solve();}

#define primetest ll t; cin>>t;while(t--){primesolve(prime,n);}

#define re      return

#define pb      emplace_back

#define st      set<ll> s;

#define ms      multiset<ll> m;

#define uom     unordered_map<ll,ll>m; 

#define fdp(W)  for(int i=0; i<N; i++) dp[i]=W;

#define fill(W) for(int i=0; i<n; i++) a[i]=W;

#define gcd(a,b) (__gcd(a, b))

#define lcm(a,b) (a / gcd(a, b) * b)

#define sieve const int n = 1e5+10; bool prime[n + 1]; memset(prime, true, sizeof(prime));for (int p = 2; p * p <= n; p++) if (prime[p] == true) for (int i = p * p; i <= n; i += p) prime[i] = false;

#define primesolution void primesolve(bool prime[],ll N)

#define solution void solve()

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

#define complement(number,number_of_bits) (((1 << number_of_bits) - 1) ^ number)

#define all(x) (x).begin(), (x).end()

#define sz(x) int(x.size())

#define mp(x,y) make_pair(x,y)



// const int N = 1e5+10;

// int dp[N];

// vector<int>g[N];

// bool vis[N];



// bool vis[1010][1010];



// bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)

// {

//     return (a.second < b.second);

// }



// inline bool isPalindrome(const string& s) {

//     return std::equal(s.begin(), s.end(), s.rbegin());

// }



// void primeFactors(int n) 

// { 

//     while (n % 2 == 0) 

//     { 

//         cout << 2 << " "; 

//         n = n/2; 

//     } 

//     for (int i = 3; i <= sqrt(n); i = i + 2) 

//     {  

//         while (n % i == 0) 

//         { 

//             cout << i << " "; 

//             n = n/i; 

//         } 

//     } 

//     if (n > 2) 

//         cout << n << " "; 

// } 



// bool isprime(ll m)

// {   

//     ll n=m;

//     bool prime[n + 1];

//     memset(prime, true, sizeof(prime));

//     for (int p = 2; p * p <= n; p++) if (prime[p] == true) for (int i = p * p; i <= n; i += p) prime[i] = false;

//     return prime[m] ? true : false;

// }



// ll power(ll x, ll y, ll p){//modular Power

//     int res = 1;

//     while (y > 0) {

//         if (y % 2 == 1)

//             res = (res * x);

//         y = y >> 1;

//         x = (x * x);

//     }

//     return res % p;

// }



// void dijkstra(ll vertex){

//     set<pair<ll,ll>>pq;

//     dist[vertex]=0;

//     pq.insert(make_pair(0,vertex));

//     while (!pq.empty())

//     {

//         auto it = pq.begin();

//         ll v = it->second;

//         ll wt = it->first;

//         pq.erase(it);

//         if(wt > dist[v]) continue;

//         if(vis[v]) continue;

//         vis[v]=1;

//         for (auto x:g[v])

//         {

//             ll c = x.first;

//             ll w = x.second;

//             if(dist[c]>dist[v]+w){

//                 dist[c] = dist[v]+w;

//             }

//             pq.insert(make_pair(dist[c],c));

//         }

//     }

// }



// void bfs(int vertex) 

// {

//     queue<int> q;

//     vis[vertex] = true;

//     q.push(vertex);



//     while (!q.empty()) {

//         int curr = q.front();

//         q.pop();

//         //operation on current (curr) node

//         for (int x : g[curr]) {

//             if (!vis[x]) {

//                 vis[x] = 1;

//                 q.push(x);

//             }

//         }

//     }

// }



// void matrixdfs(int i, int j, int n, int m){

//        if(i<0 || j<0) return;

//        if(i>=n || j>=m) return;

//        vis[i][j]=true;



//        matrixdfs(i-1,j,n,m);

//        matrixdfs(i+1,j,n,m);

//        matrixdfs(i,j-1,n,m);

//        matrixdfs(i,j+1,n,m);

// }



// void dfs(int vertex){

//     //Take action on vertex before entering the vertex

//     //Reject invalid edges here; Return 'em

       // vis[vertex]=true;



//     for(int child : g[vertex]){

           // if(vis[child]) continue;

//         //Take action on child before entering the child node // first child then second .. deepest child

//         dfs(child);

//         //Take action on child after exiting the child node // deepest child then second deep.. first child

//     }

//     //Take action on vertex before exiting the vertex

// }

//------------------------BE THE BEST----------------------------------------------

solution{

    ll n,x,y;

    cin>>n>>x>>y;

    x--; y--;

    ll a[n];

    f(0,n) a[it]=0;

    ll left = x-1;

    ll right = x+1;

    if(left==-1) {left = n-1;}

    if(right==n) {right = 0;}

    a[left]++; a[right]++;

    while(1)

    {

       if(left==y) break;

       if(a[left]==0) {

              if(left==0) a[n-1]++;

              else a[left-1]++;

       }

       else if(a[left]==1){

              if(left==0 && a[n-1]==1) a[n-1]++;

              else if(a[left-1]==1) a[left-1]++;

       }

       left--;

       if(left==-1) {left = n-1;}

    }

    while(1)

    {

       if(right==y) break;

       if(a[right]==0) {

              if(right==n-1) a[0]++;

              else a[right+1]++;

       }

       else if(a[right]==1){

              if(right==n-1 && a[0]==1) a[0]++;

              else if(a[right+1]==1) a[right+1]++;

       }

       right++;

       if(right==n) {right = 0;}

    }

    if(a[y]==0) {

       a[y]++;

    }

    if(a[y]==1 && (((y==n-1 && a[0]==1) ||(y!=n-1 && a[y+1]==1)) || ((y==0 && a[n-1]==1) || (y!=0 && a[y-1]==1)))) a[y]++;

    f(0,n) cout<<a[it]<<" ";cout<<endl;

}

//Prove It!! Through math or Examples.

//Can it be any SIMPler? Make it!

//Can it be solved by other Approach?

//Take a Break if exhausted.

//Use prebuilt fxns and algorithms

// Pen and Paper.

//:Tiger:

int main()

{optimize() test; re 0;}