Problem: 2049C - MEX Cycle
Language: Rust 2021

Previous submission (verdict: Wrong answer on test 1):
// https://codeforces.com/contest/2049/problem/C



#[allow(unused_imports)]

use alg::io::read;



fn main() {

    for _ in 0..read() {

        solve();

    }

}



fn solve() {

    input! {

        n: usize,

        x: usize,

        y: usize,

    }

    let a = ring(n, x, y);

    for i in 0..n {

        print!("{} ", a[i]);

    }

    println!();

}



fn ring(n: usize, x: usize, y: usize) -> Vec<usize> {

    let x = x - 1;

    let y = y - 1;

    assert!(x < y);

    let mut a = vec![3; n];

    if x + 1 == y || (y + 1) % n == x {

        a[0] = n % 2 + 1;

        for i in 1..n {

            a[i] = (a[i - 1] + 1) % 2;

        }

        return a;

    }

    let l1 = y - x + 1;

    let l2 = x + n - y + 1;

    if n == 4 {

        a[(x + n - 1) % n] = 1;

        a[x] = 2;

        a[x + 1] = 1;

        a[y] = 0;

    } else {

        a[(x + n - 1) % n] = l2 % 2 + 1;

        a[x] = 0;

        a[x + 1] = l1 % 2 + 1;

        a[y] = 0;

    }

    for i in (x + 2)..y {

        a[i] = (a[i - 1] + 1) % 2;

    }

    for i in (y + 1)..(n + x - 1) {

        a[i % n] = (a[(i - 1) % n] + 1) % 2;

    }

    a

}



#[cfg(test)]

mod tests {

    use super::*;



    fn mex(a: &[usize]) -> usize {

        for i in 0.. {

            if !a.iter().any(|&e| e == i) {

                return i;

            }

        }

        unreachable!()

    }



    fn check(n: usize, x: usize, y: usize) {

        let a = ring(n, x, y);

        let x = x - 1;

        let y = y - 1;

        for i in 0..n {

            let mut tmp = vec![a[(i + 1) % n], a[(i + n - 1) % n]];

            if i == x {

                tmp.push(a[y]);

            } else if i == y {

                tmp.push(a[x]);

            }

            assert_eq!(a[i], mex(&tmp), "{i} {a:?}");

        }

    }



    #[test]

    fn test1() {

        check(4, 2, 4);

        check(4, 1, 4);

    }



    #[test]

    fn test2() {

        for n in 3..10 {

            for x in 1..n {

                for y in x + 1..=n {

                    check(n, x, y);

                }

            }

        }

    }

}



mod alg {

pub mod io {

#[macro_export]

macro_rules! input {

    (mut $var:ident: $kind:tt, $($rest:tt)*) => {

        let mut $var = $crate::_input_kind!($kind);

        input!($($rest)*)

    };

    ($var:ident: $kind:tt, $($rest:tt)*) => {

        let $var = $crate::_input_kind!($kind);

        input!($($rest)*)

    };

    (mut $var:ident: $kind:tt) => { let mut $var = $crate::_input_kind!($kind); };

    ($var:ident: $kind:tt) => { let $var = $crate::_input_kind!($kind); };

    () => {};

}



#[doc(hidden)]

#[macro_export]

macro_rules! _input_kind {

    ([$t:tt; $n:expr]) => {

        (0..$n).map(|_| $crate::_input_kind!($t)).collect::<Vec<_>>()

    };

    ([$t:tt]) => {{

        let n = $crate::_input_kind!(usize);

        (0..n).map(|_| $crate::_input_kind!($t)).collect::<Vec<_>>()

    }};

    (($a:tt, $b:tt)) => {{

        let a = $crate::_input_kind!($a);

        let b = $crate::_input_kind!($b);

        (a, b)

    }};

    (bytes) => {

        $crate::_input_kind!(String).into_bytes()

    };

    (usize1) => {

        $crate::_input_kind!(usize) - 1

    };

    ($t:ty) => {

        $crate::alg::io::read::<$t>()

    };

}



pub fn read<T>() -> T

where

    T: std::str::FromStr,

    T::Err: std::fmt::Debug,

{

    use std::cell::RefCell;

    use std::io::{stdin, BufRead, StdinLock};

    thread_local! {

        pub static STDIN: RefCell<StdinLock<'static>> = RefCell::new(stdin().lock());

    }

    STDIN.with(|r| {

        let mut r = r.borrow_mut();

        let mut s = vec![];

        loop {

            let buf = r.fill_buf().unwrap();

            if buf.is_empty() {

                break;

            }

            if let Some(i) = buf.iter().position(u8::is_ascii_whitespace) {

                s.extend_from_slice(&buf[..i]);

                r.consume(i + 1);

                if !s.is_empty() {

                    break;

                }

            } else {

                s.extend_from_slice(buf);

                let n = buf.len();

                r.consume(n);

            }

        }

        std::str::from_utf8(&s).unwrap().parse().unwrap()

    })

}



#[macro_export]

macro_rules! println {

        ($($arg:tt)*) => {

            $crate::alg::io::_with_stdout(|w| {

                use std::io::Write;

                std::writeln!(w, $($arg)*).unwrap()

            })

        };

    }



#[macro_export]

macro_rules! print {

        ($($arg:tt)*) => {

            $crate::alg::io::_with_stdout(|w| {

                use std::io::Write;

                std::write!(w, $($arg)*).unwrap()

            })

        };

    }



#[doc(hidden)]

pub fn _with_stdout<F>(f: F)

where

    F: FnOnce(&mut std::io::BufWriter<std::io::StdoutLock>),

{

    use std::cell::RefCell;

    use std::io::{stdout, BufWriter, StdoutLock};

    thread_local! {

        pub static STDOUT: RefCell<BufWriter<StdoutLock<'static>>> =

            RefCell::new(BufWriter::new(stdout().lock()));

    }

    STDOUT.with(|w| f(&mut w.borrow_mut()))

}

}

}



Submission (verdict: Accepted):
// https://codeforces.com/contest/2049/problem/C



#[allow(unused_imports)]

use alg::io::read;



fn main() {

    for _ in 0..read() {

        solve();

    }

}



fn solve() {

    input! {

        n: usize,

        x: usize,

        y: usize,

    }

    let a = ring(n, x, y);

    for i in 0..n {

        print!("{} ", a[i]);

    }

    println!();

}



fn ring(n: usize, x: usize, y: usize) -> Vec<usize> {

    let x = x - 1;

    let y = y - 1;

    assert!(x < y);

    let mut a = vec![3; n];

    if x + 1 == y || (y + 1) % n == x {

        a[0] = n % 2 + 1;

        for i in 1..n {

            a[i] = (a[i - 1] + 1) % 2;

        }

        return a;

    }

    let l1 = y - x + 1;

    let l2 = x + n - y + 1;

    if n == 4 {

        a[(x + n - 1) % n] = 1;

        a[x] = 2;

        a[x + 1] = 1;

        a[y] = 0;

    } else {

        a[(x + n - 1) % n] = l2 % 2 + 1;

        a[x] = 0;

        a[x + 1] = l1 % 2 + 1;

        a[y] = 1;

    }

    for i in (x + 2)..y {

        a[i] = (a[i - 1] + 1) % 2;

    }

    for i in (y + 1)..(n + x - 1) {

        a[i % n] = (a[(i - 1) % n] + 1) % 2;

    }

    a

}



#[cfg(test)]

mod tests {

    use super::*;



    fn mex(a: &[usize]) -> usize {

        for i in 0.. {

            if !a.iter().any(|&e| e == i) {

                return i;

            }

        }

        unreachable!()

    }



    fn check(n: usize, x: usize, y: usize) {

        let a = ring(n, x, y);

        let x = x - 1;

        let y = y - 1;

        for i in 0..n {

            let mut tmp = vec![a[(i + 1) % n], a[(i + n - 1) % n]];

            if i == x {

                tmp.push(a[y]);

            } else if i == y {

                tmp.push(a[x]);

            }

            assert_eq!(a[i], mex(&tmp), "{i} {a:?}");

        }

    }



    #[test]

    fn test1() {

        check(5, 1, 3);

        check(4, 2, 4);

    }



    #[test]

    fn test2() {

        for n in 3..10 {

            for x in 1..n {

                for y in x + 1..=n {

                    check(n, x, y);

                }

            }

        }

    }

}



mod alg {

pub mod io {

#[macro_export]

macro_rules! input {

    (mut $var:ident: $kind:tt, $($rest:tt)*) => {

        let mut $var = $crate::_input_kind!($kind);

        input!($($rest)*)

    };

    ($var:ident: $kind:tt, $($rest:tt)*) => {

        let $var = $crate::_input_kind!($kind);

        input!($($rest)*)

    };

    (mut $var:ident: $kind:tt) => { let mut $var = $crate::_input_kind!($kind); };

    ($var:ident: $kind:tt) => { let $var = $crate::_input_kind!($kind); };

    () => {};

}



#[doc(hidden)]

#[macro_export]

macro_rules! _input_kind {

    ([$t:tt; $n:expr]) => {

        (0..$n).map(|_| $crate::_input_kind!($t)).collect::<Vec<_>>()

    };

    ([$t:tt]) => {{

        let n = $crate::_input_kind!(usize);

        (0..n).map(|_| $crate::_input_kind!($t)).collect::<Vec<_>>()

    }};

    (($a:tt, $b:tt)) => {{

        let a = $crate::_input_kind!($a);

        let b = $crate::_input_kind!($b);

        (a, b)

    }};

    (bytes) => {

        $crate::_input_kind!(String).into_bytes()

    };

    (usize1) => {

        $crate::_input_kind!(usize) - 1

    };

    ($t:ty) => {

        $crate::alg::io::read::<$t>()

    };

}



pub fn read<T>() -> T

where

    T: std::str::FromStr,

    T::Err: std::fmt::Debug,

{

    use std::cell::RefCell;

    use std::io::{stdin, BufRead, StdinLock};

    thread_local! {

        pub static STDIN: RefCell<StdinLock<'static>> = RefCell::new(stdin().lock());

    }

    STDIN.with(|r| {

        let mut r = r.borrow_mut();

        let mut s = vec![];

        loop {

            let buf = r.fill_buf().unwrap();

            if buf.is_empty() {

                break;

            }

            if let Some(i) = buf.iter().position(u8::is_ascii_whitespace) {

                s.extend_from_slice(&buf[..i]);

                r.consume(i + 1);

                if !s.is_empty() {

                    break;

                }

            } else {

                s.extend_from_slice(buf);

                let n = buf.len();

                r.consume(n);

            }

        }

        std::str::from_utf8(&s).unwrap().parse().unwrap()

    })

}



#[macro_export]

macro_rules! println {

        ($($arg:tt)*) => {

            $crate::alg::io::_with_stdout(|w| {

                use std::io::Write;

                std::writeln!(w, $($arg)*).unwrap()

            })

        };

    }



#[macro_export]

macro_rules! print {

        ($($arg:tt)*) => {

            $crate::alg::io::_with_stdout(|w| {

                use std::io::Write;

                std::write!(w, $($arg)*).unwrap()

            })

        };

    }



#[doc(hidden)]

pub fn _with_stdout<F>(f: F)

where

    F: FnOnce(&mut std::io::BufWriter<std::io::StdoutLock>),

{

    use std::cell::RefCell;

    use std::io::{stdout, BufWriter, StdoutLock};

    thread_local! {

        pub static STDOUT: RefCell<BufWriter<StdoutLock<'static>>> =

            RefCell::new(BufWriter::new(stdout().lock()));

    }

    STDOUT.with(|w| f(&mut w.borrow_mut()))

}

}

}