AtCoder Beginner Contest 313 D

https://atcoder.jp/contests/abc313/tasks/abc313_d

 N = 5, K = 3のとき、
 A_1 + A_4 + A_5 = T_1
 A_2 + A_4 + A_5 = T_2
 A_3 + A_4 + A_5 = T_3
 A_1 + A_3 + A_5 = T_4
 A_1 + A_3 + A_4 = T_5
とすれば、上下の差を取ると、1と2、2と3、1と4、4と5が逆になっているか否かが分かります。

ジャッジシステムの問題を初めて解いたのですが、どう書けばいいか分かりにくいし、デバッグもふつうと違うので、戸惑いました。

// Odd or Even
#![allow(non_snake_case)]


//////////////////// library ////////////////////

fn read<T: std::str::FromStr>() -> T {
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).ok();
    line.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace()
            .map(|e| e.parse().ok().unwrap()).collect()
}


//////////////////// process ////////////////////

fn read_input() -> (usize, usize) {
    let v = read_vec();
    (v[0], v[1])
}

fn create_query(i: usize, N: usize, K: usize) -> Vec<usize> {
    let mut q: Vec<i32> = vec![0; N];
    for j in N-K+1..N {
        q[j] = 1
    }
    
    if i <= N - K {
        q[i] = 1;
    }
    else {
        q[0] = 1;
        q[N-K] = 1;
        q[i] = 0
    }
    
    (1..N+1).filter(|&i| q[i-1] == 1).collect::<Vec<usize>>()
}

fn print_query(query: Vec<usize>) {
    let B: Vec<String> = query.iter().map(|d| d.to_string()).collect();
    println!("? {}", B.join(" "))
}

fn query(N: usize, K: usize) -> Vec<bool> {
    let mut T: Vec<bool> = vec![];
    for i in 0..N {
        let query = create_query(i, N, K);
        print_query(query);
        let d: i32 = read();
        T.push(d == 1)
    }
    T
}

fn determine_inversions(T: &Vec<bool>, N: usize, K: usize) -> Vec<bool> {
    let mut bs: Vec<bool> = vec![false; N];
    for i in 1..N {
        if i == N-K+1 {
            bs[i] = bs[i-1] ^ T[i] ^ T[0]
        }
        else {
            bs[i] = bs[i-1] ^ T[i] ^ T[i-1]
        }
    }
    bs
}

fn f(N: usize, K: usize) -> Vec<bool> {
    let T = query(N, K);
    let invs = determine_inversions(&T, N, K);
    let b = ((N-K+1)..N).fold(false, |x, i| x ^ T[i]);
    if b == T[0] {
        invs
    }
    else {
        invs.iter().map(|b| !b).collect::<Vec<bool>>()
    }
}

fn print_answer(A: Vec<bool>) {
    let B: Vec<i32> = A.iter().map(|b| if *b { 1 } else { 0 }).collect();
    let C: Vec<String> = B.iter().map(|d| d.to_string()).collect();
    println!("! {}", C.join(" "))
}


//////////////////// main ////////////////////

fn main() {
    let (N, K) = read_input();
    print_answer(f(N, K));
}