use std::{collections::HashSet, io}; #[derive(Debug)] pub struct Xorshift { seed: u64, } impl Xorshift { pub fn new() -> Xorshift { Xorshift { seed: 0xf0fb588ca2196dac, } } pub fn with_seed(seed: u64) -> Xorshift { Xorshift { seed } } pub fn next(&mut self) -> u64 { self.seed = self.seed ^ (self.seed << 13); self.seed = self.seed ^ (self.seed >> 7); self.seed = self.seed ^ (self.seed << 17); self.seed } pub fn next_u128(&mut self) -> u128 { let first = self.next() as u128; let second = self.next() as u128; (first << 64) + second } pub fn rand(&mut self, m: u64) -> u64 { self.next() % m } // 0.0 ~ 1.0 pub fn randf(&mut self) -> f64 { use std::mem; const UPPER_MASK: u64 = 0x3FF0000000000000; const LOWER_MASK: u64 = 0xFFFFFFFFFFFFF; let tmp = UPPER_MASK | (self.next() & LOWER_MASK); let result: f64 = unsafe { mem::transmute(tmp) }; result - 1.0 } } #[derive(Clone, Copy)] struct Card { number: [u64; 2], } struct Problem { cards: Vec, operation_count: usize, hash_table: Vec>, } fn read_problem() -> Problem { // 標準入力からの読み取り用バッファ let mut input = String::new(); // N の読み取り io::stdin().read_line(&mut input).expect("Failed to read line"); let n: usize = input.trim().parse().expect("Input was not a number"); // 各 A_i, B_i の読み取り let mut pairs = Vec::new(); // (A_i, B_i) のペアを格納するベクター for _ in 0..n { input.clear(); // バッファをクリア io::stdin().read_line(&mut input).expect("Failed to read line"); // 空白で分割し、それぞれ u64 として解析 let parts: Vec = input .trim() .split_whitespace() .map(|x| x.parse().expect("Input was not a number")) .collect(); if parts.len() != 2 { eprintln!("Invalid input format"); continue; } // ベクターに追加 pairs.push((parts[0], parts[1])); } let cards = pairs .into_iter() .map(|(v1, v2)| Card { number: [v1, v2] }) .collect::>(); let mut xorshift = Xorshift::new(); // table[card_id][select_count] let mut hash_table = vec![vec![0u128; 51]; n]; for y in 0..n { for x in 0..51 { hash_table[y][x] = xorshift.next_u128(); } } Problem { cards, operation_count: 50, hash_table, } } #[derive(Clone)] struct State { card_pool: Vec, select_count: Vec, hash: u128, operation_list: Vec<(u8, u8)>, } const TARGET_VALUE: u64 = 50_0000_0000_0000_0000; impl State { fn new(problem: &Problem) -> State { let mut hash = 0u128; for i in 0..problem.cards.len() { hash ^= problem.hash_table[i][0]; } State { card_pool: problem.cards.clone(), select_count: vec![0; problem.cards.len()], hash, operation_list: vec![], } } fn copy_from(&mut self, state: &State) { for i in 0..state.card_pool.len() { self.card_pool[i] = state.card_pool[i] } self.select_count.resize(state.select_count.len(), 0); self.select_count.copy_from_slice(&state.select_count); self.hash = state.hash; self.operation_list.resize(state.operation_list.len(), (0, 0)); self.operation_list.copy_from_slice(&state.operation_list); } fn manipulate(&mut self, problem: &Problem, c1: usize, c2: usize) { let first = (self.card_pool[c1].number[0] + self.card_pool[c2].number[0]) / 2; let second = (self.card_pool[c1].number[1] + self.card_pool[c2].number[1]) / 2; self.hash ^= problem.hash_table[c1][self.select_count[c1] as usize]; self.hash ^= problem.hash_table[c2][self.select_count[c2] as usize]; self.card_pool[c1].number[0] = first; self.card_pool[c2].number[0] = first; self.card_pool[c1].number[1] = second; self.card_pool[c2].number[1] = second; self.select_count[c1] += 1; self.select_count[c2] += 1; self.hash ^= problem.hash_table[c1][self.select_count[c1] as usize]; self.hash ^= problem.hash_table[c2][self.select_count[c2] as usize]; self.operation_list.push((c1 as u8, c2 as u8)); } } fn calculate_cost_diff(first: u64, second: u64) -> u64 { first.abs_diff(TARGET_VALUE).max(second.abs_diff(TARGET_VALUE)) } fn simulate_with_greedy(problem: &Problem, state: &mut State, turn: usize) -> u64 { let mut state = state.clone(); for _iter in 0..turn { // 平均化して改善率の最も高い組合せを選択 let mut best_improve_pair = (0, 0); let mut best_gain = std::i64::MIN; let c1 = 0; for c2 in c1 + 1..state.card_pool.len() { let first = (state.card_pool[c1].number[0] + state.card_pool[c2].number[0]) / 2; let second = (state.card_pool[c1].number[1] + state.card_pool[c2].number[1]) / 2; let before_c1_diff = calculate_cost_diff(state.card_pool[c1].number[0], state.card_pool[c1].number[1]); let before_c2_diff = calculate_cost_diff(state.card_pool[c2].number[0], state.card_pool[c2].number[1]); let before_diff = before_c1_diff + before_c2_diff; let after_diff = calculate_cost_diff(first, second) * 2; let gain = before_diff as i64 - after_diff as i64; if best_gain < gain { best_gain = gain; best_improve_pair = (c1, c2); } } let (c1, c2) = best_improve_pair; state.manipulate(problem, c1, c2); } // calculate score // ⌊2000000−100000 log 10 (max(V_1 ,V_2)+1)⌋ // higher is better let diff = calculate_cost_diff(state.card_pool[0].number[0], state.card_pool[0].number[1]); (200_0000f64 - 10_0000f64 * (diff as f64).log10()).floor() as u64 } fn solve(problem: &Problem) -> Vec<(usize, usize)> { let mut state_buffer1 = vec![State::new(problem)]; let mut state_buffer2 = vec![]; let mut init_state = state_buffer1[0].clone(); let beam_width = 100; let mut diff_list = vec![]; let mut hash_set = HashSet::::new(); let mut xorshift = Xorshift::new(); for turn in 1..=problem.operation_count { diff_list.clear(); let (before_buffer, mut after_buffer) = if turn % 2 == 1 { (state_buffer1, state_buffer2) } else { (state_buffer2, state_buffer1) }; for (i, s) in before_buffer.iter().enumerate() { let c1 = 0; for c2 in c1 + 1..s.card_pool.len() { init_state.copy_from(&s); init_state.manipulate(problem, c1, c2); let hash = init_state.hash; let score = simulate_with_greedy(problem, &mut init_state, problem.operation_count - turn); let noise = xorshift.next() % 100; let score = score * 100 + noise; diff_list.push((score as i64, hash, i, c1, c2)); } } diff_list.sort_by_key(|p| -p.0); hash_set.clear(); let mut ti = 0; for (_s, hash, si, c1, c2) in diff_list.iter() { if hash_set.contains(hash) { continue; } hash_set.insert(*hash); if after_buffer.len() <= ti { let state = State::new(problem); after_buffer.push(state); } after_buffer[ti].copy_from(&before_buffer[*si as usize]); after_buffer[ti].manipulate(problem, *c1, *c2); if after_buffer.len() >= beam_width { break; } ti += 1; } (state_buffer1, state_buffer2) = if turn % 2 == 1 { (before_buffer, after_buffer) } else { (after_buffer, before_buffer) }; } state_buffer1[0] .operation_list .iter() .map(|(c1, c2)| (1 + *c1 as usize, 1 + *c2 as usize)) .collect::>() } fn main() { let problem = read_problem(); let ret = solve(&problem); println!("{}", ret.len()); for (c1, c2) in ret.iter() { println!("{} {}", c1, c2); } }