use std::time::Instant; macro_rules! get { ($t:ty) => { { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim().parse::<$t>().unwrap() } }; ($($t:ty),*) => { { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( $(iter.next().unwrap().parse::<$t>().unwrap(),)* ) } }; ($t:ty; $n:expr) => { (0..$n).map(|_| get!($t) ).collect::>() }; ($($t:ty),*; $n:expr) => { (0..$n).map(|_| get!($($t),*) ).collect::>() }; ($t:ty ;;) => { { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.split_whitespace() .map(|t| t.parse::<$t>().unwrap()) .collect::>() } }; ($t:ty ;; $n:expr) => { (0..$n).map(|_| get!($t ;;)).collect::>() }; } #[allow(unused_macros)] macro_rules! chmin { ($base:expr, $($cmps:expr),+ $(,)*) => {{ let cmp_min = min!($($cmps),+); if $base > cmp_min { $base = cmp_min; true } else { false } }}; } #[allow(unused_macros)] macro_rules! chmax { ($base:expr, $($cmps:expr),+ $(,)*) => {{ let cmp_max = max!($($cmps),+); if $base < cmp_max { $base = cmp_max; true } else { false } }}; } #[allow(unused_macros)] macro_rules! min { ($a:expr $(,)*) => {{ $a }}; ($a:expr, $b:expr $(,)*) => {{ std::cmp::min($a, $b) }}; ($a:expr, $($rest:expr),+ $(,)*) => {{ std::cmp::min($a, min!($($rest),+)) }}; } #[allow(unused_macros)] macro_rules! max { ($a:expr $(,)*) => {{ $a }}; ($a:expr, $b:expr $(,)*) => {{ std::cmp::max($a, $b) }}; ($a:expr, $($rest:expr),+ $(,)*) => {{ std::cmp::max($a, max!($($rest),+)) }}; } #[allow(unused_macros)] macro_rules! mat { ($e:expr; $d:expr) => { vec![$e; $d] }; ($e:expr; $d:expr $(; $ds:expr)+) => { vec![mat![$e $(; $ds)*]; $d] }; } #[derive(Debug, Clone, Copy)] struct Requirement { digit: usize, value: usize, } #[derive(Debug, Clone)] struct Choice { requirements: [Requirement; 3], } #[derive(Debug, Clone)] struct Input { since: Instant, choices: Vec, } const CHOICES_COUNT: usize = 2048; const DIGIT_LEN: usize = 256; const TIME_LIMIT: f64 = 1.98; fn main() { let input = read_input(); let solution = solve(&input); write_output(&solution); } fn read_input() -> Input { let mut choices = vec![]; let mut counts = vec![0; DIGIT_LEN]; for _ in 0..CHOICES_COUNT { let (a, b, c, p, q, r) = get!(usize, usize, usize, usize, usize, usize); counts[a] += 1; counts[b] += 1; counts[c] += 1; let requirements = [ Requirement { digit: a, value: p }, Requirement { digit: b, value: q }, Requirement { digit: c, value: r }, ]; let choice = Choice { requirements }; choices.push(choice); } // TODO: 重なるケースとか真面目に見た方がいいかも for choice in choices.iter_mut() { choice .requirements .sort_unstable_by(|r0, r1| counts[r0.digit].cmp(&counts[r1.digit])); } let since = Instant::now(); let input = Input { since, choices }; input } fn solve(input: &Input) -> Vec { let init_solution = vec![0; CHOICES_COUNT]; let solution = annealing(input, init_solution, TIME_LIMIT); to_answer(input, &solution) } fn annealing(input: &Input, initial_solution: Vec, duration: f64) -> Vec { let mut solution = initial_solution; let mut best_solution = solution.clone(); let mut current_score = calc_score(input, &solution); let mut best_score = current_score; let mut all_iter = 0; let mut valid_iter = 0; let mut accepted_count = 0; let mut update_count = 0; let mut rng = Xorshift::with_seed(42); let duration_inv = 1.0 / duration; let since = std::time::Instant::now(); let mut time = 0.0; let temp0 = 1e1; let temp1 = 1e-1; let mut inv_temp = 1.0 / temp0; while time < 1.0 { all_iter += 1; if (all_iter & ((1 << 4) - 1)) == 0 { time = (std::time::Instant::now() - since).as_secs_f64() * duration_inv; let temp = f64::powf(temp0, 1.0 - time) * f64::powf(temp1, time); inv_temp = 1.0 / temp; } if rng.rand(10) != 0 { // 変形 let index = if (current_score as usize) < CHOICES_COUNT && rng.rand(20) == 0 { current_score as usize } else { rng.rand((best_score as usize + 50).min(CHOICES_COUNT)) }; let add = rng.rand(2) + 1; solution[index] = (solution[index] + add) % 3; // スコア計算 let new_score = calc_score(input, &solution); let score_diff = new_score - current_score; if score_diff >= 0 || rng.randf() < f64::exp(score_diff as f64 * inv_temp) { // 解の更新 current_score = new_score; accepted_count += 1; if chmax!(best_score, current_score) { best_solution = solution.clone(); update_count += 1; } } else { // ロールバック solution[index] = (solution[index] + 3 - add) % 3; } } else { let mut stack = vec![]; let index = current_score as usize; let add = rng.rand(2) + 1; let new_choice = (solution[index] + add) % 3; solution[index] = new_choice; stack.push((index, add)); let base_req = &input.choices[index].requirements[new_choice]; for i in 0..(current_score as usize) { let reqs = &input.choices[i].requirements; let req = &reqs[solution[i]]; if req.digit == base_req.digit && req.value != base_req.value { let add = rng.rand(2) + 1; let new_choice = (solution[i] + add) % 3; solution[i] = new_choice; stack.push((i, add)); } } // スコア計算 let new_score = calc_score(input, &solution); let score_diff = new_score - current_score; if score_diff >= 0 || rng.randf() < f64::exp(score_diff as f64 * inv_temp) { // 解の更新 current_score = new_score; accepted_count += 1; if chmax!(best_score, current_score) { best_solution = solution.clone(); update_count += 1; } } else { // ロールバック while let Some((i, add)) = stack.pop() { solution[i] = (solution[i] + 3 - add) % 3; } } } valid_iter += 1; } eprintln!("===== annealing ====="); eprintln!("score : {}", best_score); eprintln!("all iter : {}", all_iter); eprintln!("valid iter : {}", valid_iter); eprintln!("accepted : {}", accepted_count); eprintln!("updated : {}", update_count); eprintln!(""); eprintln!("score: {}", best_score); best_solution } fn calc_score(input: &Input, solution: &[usize]) -> i32 { let mut used = [false; DIGIT_LEN]; let mut values = [0; DIGIT_LEN]; for (i, (solution, choice)) in solution.iter().zip(input.choices.iter()).enumerate() { let req = choice.requirements[*solution]; if used[req.digit] && values[req.digit] != req.value { return i as i32; } used[req.digit] = true; values[req.digit] = req.value; } CHOICES_COUNT as i32 } fn to_answer(input: &Input, solution: &[usize]) -> Vec { let mut used = [false; DIGIT_LEN]; let mut values = vec![0; DIGIT_LEN]; for (solution, choice) in solution.iter().zip(input.choices.iter()) { let req = choice.requirements[*solution]; if used[req.digit] && values[req.digit] != req.value { return values; } used[req.digit] = true; values[req.digit] = req.value; } values } fn write_output(solution: &[usize]) { let mut result = String::new(); for v in solution.iter().rev() { if *v == 0 { result.push('0'); } else { result.push('1'); } } println!("{}", result); } #[derive(Debug)] #[allow(dead_code)] pub struct Xorshift { seed: usize, } impl Xorshift { #[allow(dead_code)] pub fn new() -> Xorshift { Xorshift { seed: 0xf0fb588ca2196dac, } } #[allow(dead_code)] pub fn with_seed(seed: usize) -> Xorshift { Xorshift { seed: seed } } #[inline] #[allow(dead_code)] pub fn next(&mut self) -> usize { self.seed = self.seed ^ (self.seed << 13); self.seed = self.seed ^ (self.seed >> 7); self.seed = self.seed ^ (self.seed << 17); self.seed } #[inline] #[allow(dead_code)] pub fn rand(&mut self, m: usize) -> usize { self.next() % m } #[inline] #[allow(dead_code)] // 0.0 ~ 1.0 pub fn randf(&mut self) -> f64 { use std::mem; const UPPER_MASK: usize = 0x3FF0000000000000; const LOWER_MASK: usize = 0xFFFFFFFFFFFFF; let tmp = UPPER_MASK | (self.next() & LOWER_MASK); let result: f64 = unsafe { mem::transmute(tmp) }; result - 1.0 } }