#include #include #include #include #include #include #include #include #include #include // コンパイルオプション推奨: g++ -std=c++17 -O3 -march=native -o a.out main.cpp using namespace std; // --- xoshiro256++ 高速乱数生成器 --- // see: https://prng.di.unimi.it/ class Xoshiro256 { public: using result_type = uint64_t; static constexpr result_type min() { return 0; } static constexpr result_type max() { return UINT64_MAX; } Xoshiro256() : Xoshiro256(chrono::steady_clock::now().time_since_epoch().count()) {} explicit Xoshiro256(uint64_t seed) { s[0] = split_mix64(seed); s[1] = split_mix64(seed); s[2] = split_mix64(seed); s[3] = split_mix64(seed); } result_type operator()() { const uint64_t result = rotl(s[0] + s[3], 23) + s[0]; const uint64_t t = s[1] << 17; s[2] ^= s[0]; s[3] ^= s[1]; s[1] ^= s[2]; s[0] ^= s[3]; s[2] ^= t; s[3] = rotl(s[3], 45); return result; } // [0, upper_bound) の整数乱数を生成 inline uint64_t next_int(uint64_t upper_bound) { return operator()() % upper_bound; } // [0.0, 1.0) の浮動小数点数乱数を生成 inline double next_double() { return (operator()() >> 11) * (1.0 / (1ULL << 53)); } private: array s; static inline uint64_t rotl(const uint64_t x, int k) { return (x << k) | (x >> (64 - k)); } static uint64_t split_mix64(uint64_t& x) { x += 0x9e3779b97f4a7c15; uint64_t z = x; z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; z = (z ^ (z >> 27)) * 0x94d049bb133111eb; return z ^ (z >> 31); } }; // グローバル乱数生成器 Xoshiro256 rng; // --- パラメータ調整欄 --- // --- 戦略パラメータ --- const int INITIAL_PHASE_TARGETS = 80; const int MAX_DIST_INITIAL_PHASE = 12; const int MIN_DIST_FOR_TARGETING = 10; // --- 焼きなましパラメータ --- const double TIME_LIMIT_MS = 1980.0; const double SA_START_TEMP = 100000.0; const double SA_END_TEMP = 1.0; // --- グローバル定数 (変更不要) --- const int N_CONST = 10; const int T_CONST = 1000; /** * @struct State * @brief 現在のゲームの状態を管理する構造体 * @note スコアを差分更新するように変更 */ struct State { int r, c; int s; vector> grid; int ops_count; string ops_log; long long score; // ★ スコアを差分更新 State(const vector>& initial_grid) { r = 0; c = 0; s = 0; grid = initial_grid; ops_count = 0; ops_log.reserve(T_CONST + 50); // ★ 事前にメモリ確保 score = 0; for(const auto& row : grid) { for(int cell_val : row) { score += cell_val; } } } void apply_op(char op) { if (ops_count >= T_CONST) return; bool valid = true; if (op == 'U') { if (r > 0) r--; else valid = false; } else if (op == 'D') { if (r < N_CONST - 1) r++; else valid = false; } else if (op == 'L') { if (c > 0) c--; else valid = false; } else if (op == 'R') { if (c < N_CONST - 1) c++; else valid = false; } else if (op == 'W') { long long old_val = grid[r][c]; grid[r][c] ^= s; score += (long long)grid[r][c] - old_val; // ★ スコアの差分更新 } else if (op == 'C') { s ^= grid[r][c]; } else { valid = false; } if (valid) { ops_count++; ops_log += op; } } void apply_ops_string(const string& ops) { for (char op : ops) { apply_op(op); } } }; // --- ヘルパー関数 --- string get_simple_move_ops(int r1, int c1, int r2, int c2) { string path_ops = ""; string vert_move = (r1 < r2) ? "D" : "U"; string horz_move = (c1 < c2) ? "R" : "L"; for (int i = 0; i < abs(r1 - r2); ++i) path_ops += vert_move; for (int i = 0; i < abs(c1 - c2); ++i) path_ops += horz_move; return path_ops; } /** * @brief 特定のターゲット(tr, tc)への最適な操作列を見つける(修正版) * @param current_state 現在の状態 * @param tr ターゲットの行 * @param tc ターゲットの列 * @return pair {操作列, スコア増加量} */ pair find_best_ops_for_target(const State& current_state, int tr, int tc) { string move_ops = get_simple_move_ops(current_state.r, current_state.c, tr, tc); string total_ops = ""; long long total_increase = 0; int temp_s = current_state.s; vector> temp_grid = current_state.grid; int temp_r = current_state.r; int temp_c = current_state.c; // --- 経路上での操作シミュレーション --- for (char op : move_ops) { if (current_state.ops_count + total_ops.length() >= T_CONST) break; total_ops += op; if (op == 'U') temp_r--; else if (op == 'D') temp_r++; else if (op == 'L') temp_c--; else if (op == 'R') temp_c++; long long potential_w_increase = (long long)(temp_grid[temp_r][temp_c] ^ temp_s) - temp_grid[temp_r][temp_c]; int dist_to_target = abs(temp_r - tr) + abs(temp_c - tc); if (potential_w_increase > 0 && current_state.ops_count + total_ops.length() + 1 + dist_to_target <= T_CONST) { total_ops += 'W'; total_increase += potential_w_increase; temp_grid[temp_r][temp_c] ^= temp_s; } if (current_state.ops_count + total_ops.length() < T_CONST) { int s_after_copy = temp_s ^ temp_grid[temp_r][temp_c]; int target_value = temp_grid[tr][tc]; long long score_at_target_if_no_copy = (long long)(target_value ^ temp_s); long long score_at_target_if_copy = (long long)(target_value ^ s_after_copy); if (score_at_target_if_copy > score_at_target_if_no_copy) { total_ops += 'C'; temp_s = s_after_copy; } } } // --- 目的地での最終的な書き込み('W') --- if (current_state.ops_count + total_ops.length() < T_CONST) { long long final_increase = (long long)(temp_grid[tr][tc] ^ temp_s) - temp_grid[tr][tc]; if (final_increase > 0) { total_ops += 'W'; total_increase += final_increase; } } if (total_increase > 0) { return {total_ops, total_increase}; } else { return {move_ops, -1}; } } /** * @brief 指定された訪問順で完全なシミュレーションを実行し、最終状態を返す * @param visit_order 訪問するマスの順序 * @param initial_grid 初期盤面 * @return State シミュレーション後の最終状態 * @note vector::eraseの代わりにインデックスを使うように変更 */ State run_simulation(const vector>& visit_order, const vector>& initial_grid) { State current_state(initial_grid); vector> postponed_queue; postponed_queue.reserve(N_CONST * N_CONST); size_t visit_idx = 0; // 序盤フェーズ int targets_visited = 0; while (targets_visited < INITIAL_PHASE_TARGETS && visit_idx < visit_order.size()) { if (current_state.ops_count >= T_CONST) break; pair target_pos = visit_order[visit_idx]; int dist = abs(current_state.r - target_pos.first) + abs(current_state.c - target_pos.second); if (dist > MAX_DIST_INITIAL_PHASE) { postponed_queue.push_back(target_pos); visit_idx++; continue; } pair result = find_best_ops_for_target(current_state, target_pos.first, target_pos.second); current_state.apply_ops_string(result.first); visit_idx++; targets_visited++; } // 中盤フェーズ (残りのターゲットと後回しにしたターゲットを処理) vector> mid_game_order; mid_game_order.reserve(N_CONST * N_CONST); for(size_t i = visit_idx; i < visit_order.size(); ++i) mid_game_order.push_back(visit_order[i]); mid_game_order.insert(mid_game_order.end(), postponed_queue.begin(), postponed_queue.end()); for(const auto& target_pos : mid_game_order) { if (current_state.ops_count >= T_CONST) break; pair result = find_best_ops_for_target(current_state, target_pos.first, target_pos.second); current_state.apply_ops_string(result.first); } // 後半戦略 while (true) { int remaining_ops = T_CONST - current_state.ops_count; if (remaining_ops < 2) break; int best_target_r = -1, best_target_c = -1; int min_score = -1; for (int r = 0; r < N_CONST; ++r) { for (int c = 0; c < N_CONST; ++c) { int dist = abs(current_state.r - r) + abs(current_state.c - c); if (dist == 0 || dist >= remaining_ops) continue; bool is_target_candidate = false; if (remaining_ops > MIN_DIST_FOR_TARGETING + dist + 5) { if (dist >= MIN_DIST_FOR_TARGETING) is_target_candidate = true; } else { is_target_candidate = true; } if (is_target_candidate) { if (best_target_r == -1 || current_state.grid[r][c] < min_score) { min_score = current_state.grid[r][c]; best_target_r = r; best_target_c = c; } } } } if (best_target_r == -1) break; pair result = find_best_ops_for_target(current_state, best_target_r, best_target_c); current_state.apply_ops_string(result.first); } return current_state; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); auto start_time = chrono::steady_clock::now(); int N_in, T_in; cin >> N_in >> T_in; vector> initial_grid(N_CONST, vector(N_CONST)); for (int i = 0; i < N_CONST; ++i) { for (int j = 0; j < N_CONST; ++j) { cin >> initial_grid[i][j]; } } // --- 焼きなまし法の初期化 --- vector> base_order; base_order.reserve(N_CONST * N_CONST - 1); for (int i = 0; i < N_CONST; ++i) { for (int j = 0; j < N_CONST; ++j) { if (i == 0 && j == 0) continue; base_order.push_back({i, j}); } } shuffle(base_order.begin(), base_order.end(), rng); vector> current_order = base_order; State best_state = run_simulation(current_order, initial_grid); long long best_score = best_state.score; long long current_score = best_score; int iteration = 0; int accepted_count = 0; cerr << "[SA] Initial Score: " << best_score << endl; // --- 焼きなましループ --- while(true) { auto current_time = chrono::steady_clock::now(); double elapsed_ms = chrono::duration_cast(current_time - start_time).count(); if (elapsed_ms > TIME_LIMIT_MS) break; iteration++; vector> new_order = current_order; // 近傍操作 (2-opt or 2-swap) if (rng.next_int(10) == 0) { // 2-opt int i = rng.next_int(new_order.size()); int j = rng.next_int(new_order.size()); if (i == j) continue; if (i > j) swap(i, j); reverse(new_order.begin() + i, new_order.begin() + j); } else { // 2-swap int i = rng.next_int(new_order.size()); int j = rng.next_int(new_order.size()); if (i == j) continue; swap(new_order[i], new_order[j]); } State new_state = run_simulation(new_order, initial_grid); long long new_score = new_state.score; // ★ 差分更新されたスコアを使用 double temp = SA_START_TEMP + (SA_END_TEMP - SA_START_TEMP) * elapsed_ms / TIME_LIMIT_MS; double delta = new_score - current_score; if (delta > 0 || (temp > 0 && rng.next_double() < exp(delta / temp))) { current_order = move(new_order); // ★ moveで効率化 current_score = new_score; if (current_score > best_score) { best_score = current_score; best_state = move(new_state); // ★ moveで効率化 } accepted_count++; } if (iteration % 100 == 0) { cerr << "[SA] iter:" << iteration << " time:" << (int)elapsed_ms << "ms" << " temp:" << (int)temp << " score:" << current_score << " best:" << best_score << " accept_rate:" << (double)accepted_count/iteration << endl; } } // --- 最良解の操作ログを出力 --- for (char op : best_state.ops_log) { cout << op << "\n"; } // --- 最終デバッグ出力 --- cerr << "--- Final Debug Info ---" << endl; cerr << "SA iterations: " << iteration << endl; cerr << "Best score found: " << best_score << endl; cerr << "Final operations count: " << best_state.ops_log.length() << "/" << T_CONST << endl; // 検算 State final_check_state(initial_grid); final_check_state.apply_ops_string(best_state.ops_log); cerr << "Score from final output (re-calculated): " << final_check_state.score << endl; return 0; }