Cod sursa(job #3379428)

Utilizator fmi-studentnu sunt de acord fmi-student Data 30 noiembrie 2026 16:21:06
Problema Curcubeu Scor 100
Compilator rs Status done
Runda Arhiva de probleme Marime 1.59 kb
use std::fmt::Write;
use std::fs;

fn find(parent: &mut [usize], mut u: usize) -> usize {
    while parent[u] != u {
        parent[u] = parent[parent[u]];
        u = parent[u];
    }

    u
}

fn main() {
    let input = fs::read_to_string("curcubeu.in").expect("");
    let mut tokens = input.split_whitespace();

    let n: usize = tokens.next().unwrap().parse().unwrap();
    let mut a: u64 = tokens.next().unwrap().parse().unwrap();
    let mut b: u64 = tokens.next().unwrap().parse().unwrap();
    let mut c: u64 = tokens.next().unwrap().parse().unwrap();

    let modulo = n as u64;

    let mut operations: Vec<(usize, usize, u64)> =
        Vec::with_capacity(n - 1);

    for i in 1..n {
        if i > 1 {
            let factor = i as u64;

            a = a * factor % modulo;
            b = b * factor % modulo;
            c = c * factor % modulo;
        }

        let left = a.min(b) as usize;
        let right = a.max(b) as usize;

        operations.push((left, right, c));
    }

    let mut parent: Vec<usize> = (0..=n).collect();
    let mut color = vec![0_u64; n + 1];

    for (left, right, value) in operations.into_iter().rev() {
        let mut position = find(&mut parent, left);

        while position <= right {
            color[position] = value;

            let next = find(&mut parent, position + 1);

            parent[position] = next;
            position = next;
        }
    }

    let mut output = String::with_capacity(n * 8);

    for position in 1..n {
        writeln!(&mut output, "{}", color[position]).unwrap();
    }

    fs::write("curcubeu.out", output).expect("");
}