//#pragma GCC optimize("O3,inline") //#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC target("movbe") //#pragma GCC target("aes,pclmul,rdrnd") #ifdef IS_LOCAL #include"template.hpp" #pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #ifdef ATCODER #pragma GCC target("arch=skylake-avx512") #endif #else #pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #ifdef ATCODER #pragma GCC target("arch=skylake-avx512") #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef __GNUG__ #include #endif #endif #define rep(i, n) for (int i = 0; i < (int)n; ++i) #define reps(i, s, n) for (int i = s; i < (int)n; ++i) #define repv(x, v) for (const auto& x : v) #define rep2(i,j,n) for (int i = 0; i < (int)n; ++i) for (int j = 0; j < (int)n; ++j) #define rep3(i,j,k,n) for (int i = 0; i < (int)n; ++i) for (int j = 0; j < (int)n; ++j) for (int k= 0; k < (int)n; ++k) #define all(v) v.begin(),v.end() template struct mvec_impl { using type = std::vector::type>; }; template struct mvec_impl { using type = std::vector; }; template using vec = typename mvec_impl::type; template auto make_vec(size_t n, T x) { return vec(n, x); } template auto make_vec(size_t n, Args... args) { return std::vector(n, make_vec(args...)); } using ull = unsigned long long; using uint = unsigned int; using ushort = unsigned short; using uchar = unsigned char; using ll = long long; template using t2 = std::pair; template using t3 = std::tuple; using i2 = t2; using i3 = t3; using clk = std::chrono::steady_clock; // global constexpr int N = 64; constexpr int N2 = N*N; constexpr int M = 4 * N; vec A; #ifdef IS_LOCAL constexpr bool local = true; #else constexpr bool local = false; #endif std::map LOG; std::random_device rnd; std::mt19937 mt(rnd()); std::uniform_real_distribution<> rand01(0.0, 1.0); clk::time_point start_time; std::ofstream ofstr; // result file // utils template concept StringLike = requires(T x) { {std::string_view(x) }; }; template concept TupleLike = requires { typename std::tuple_size>::type; }; template concept Range = std::ranges::range && !StringLike && !TupleLike; void print(const TupleLike auto&); void print(const Range auto&); void print(const auto&); void print(const TupleLike auto& tp) { std::cerr << "("; size_t i = 0; std::apply([&](auto&&... args){ ((std::cerr << (i++ ? " " : ""), print(args)), ...); }, tp); std::cerr << ")"; } void print(const Range auto& r) { std::cerr << "["; bool f = true; for (auto&& x : r) { if (!std::exchange(f, false)) std::cerr << " "; print(x); } std::cerr << "]"; } void print(const auto& x) { std::cerr << x; } // ---- debug ---- template //void dprint_impl(std::string_view names, const Ts&... xs) { void dprint(const Ts&... xs) { if (!local) return; std::cerr << "# ";// << names << ": "; ((print(xs), std::cerr << " "), ...); std::cerr << '\n'; } //#define dprint(...) dprint_impl(#__VA_ARGS__, __VA_ARGS__) double getElapsed(const clk::time_point& start){ return (std::chrono::duration(clk::now()-start)).count(); } template T sum(const vec& v){ return std::accumulate(all(v), T{}); } template auto argmax(const vec& v) { auto it = std::ranges::max_element(v); return std::pair{size_t(it - v.begin()), *it}; } template auto argmin(const vec& v) { auto it = std::ranges::min_element(v); return std::pair{size_t(it - v.begin()), *it}; } vec range(const int n){ vec v(n); std::iota(all(v), 0); return v; } uint64_t xorShift64() { static uint64_t x = 88172645463325252ULL; //static uint64_t y = 1315423911ULL; //uint64_t t = x; //x = y; //t ^= t << 23; //t ^= t >> 17; //t ^= y ^ (y >> 26); //y = t; //return t + y; x ^= x << 7ull; x ^= x >> 9ull; return x; } double rand01fast(){ uint64_t x = xorShift64(); return (x >> 11 ) * (1.0 / (1ULL << 53)); } uint32_t xorShift() { static uint32_t y = 2463534242; y = y ^ (y << 13); y = y ^ (y >> 17); return y = y ^ (y << 5); } uint32_t randint(const uint32_t size){ uint32_t a = xorShift(); uint64_t m = (uint64_t)a * (uint64_t) size; return m >> 32; } template T choice(const vec& v){ return v[randint(v.size())]; } vec sample(int mx, int num){ vec res; while(res.size() < num){ res.emplace_back(randint(mx)); std::sort(all(res)); auto unique_end = std::unique(all(res)); res.erase(unique_end, res.end()); } return res; } bool randbool(){ return xorShift() & 1; } template vec argsort(const vec& v){ auto res = range(v.size()); std::ranges::sort(res, {}, [&](int i){return v[i];}); return res; } // functions constexpr int LOG_TABLE_SIZE = 1 << 14; double log_table[LOG_TABLE_SIZE]; inline double log_rand() { static int idx = 0; if (idx == LOG_TABLE_SIZE) { idx = 0; } return log_table[idx++]; } void initialize(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); if(local){ ofstr = std::ofstream("./result.txt"); auto original_buf = std::cout.rdbuf(); std::cout.rdbuf(ofstr.rdbuf()); } start_time = clk::now(); for (int i = 0; i < LOG_TABLE_SIZE; i++) { log_table[i] = log(rand01(mt)); } int n; std::cin >> n; rep(m,M){ int a; std::cin >> a; A.push_back(a); } } void deconstruct(){ } using Board = vec; int reflect(int m, int d){ if(m==1){ switch(d){ case 0: return 1; case 1: return 0; case 2: return 3; case 3: return 2; } } else { switch(d){ case 0: return 3; case 1: return 2; case 2: return 1; case 3: return 0; } } throw std::runtime_error("ERROR"); } // Thanks for https://kopricky.github.io/code/Computation_Advanced/inversion_count.html template class BIT { private: int n; vec bit; public: void add(int i, T x){ i++; while(i < n){ bit[i] += x, i += i & -i; } } T sum(int i){ i++; T s = 0; while(i > 0){ s += bit[i], i -= i & -i; } return s; } BIT(){} BIT(int sz) : n(sz+1), bit(n, 0){} BIT(const vec& v) : n((int)v.size()+1), bit(n, 0){ for(int i = 0; i < n-1; i++){ add(i,v[i]); } } }; long long inv_count(const Board& u) { int n = (int)u.size(); BIT bt(n); long long ans = 0; for(int i = 0; i < n; i++){ ans += i - bt.sum(u[i]); bt.add(u[i], 1); } return ans; } vec simulate(const Board& board){ const vec dir{{1,0},{0,-1},{-1,0},{0,1}}; vec B(M); rep(d,4){ rep(n,N){ int td = d; int a = A[d*N+n]; int x,y; switch(d){ case 0:{ x = 0; y = n; break; } case 1:{ x = n; y = N-1; break; } case 2:{ x = N-1; y = N-1-n; break; } case 3:{ x = N-1-n; y = 0; break; } } bool in_board = true; while(in_board){ int m = board[N*x+y]; if(m != 0){ td = reflect(m, td); } const auto& [dx,dy] = dir[td]; x += dx; y += dy; if(x < 0){ B[y] = a; in_board = false; } if(N <= x){ B[N*2+N-1-y] = a; in_board = false; } if(y < 0){ B[N*3+N-1-x] = a; in_board = false; } if(N <= y){ B[N+x] = a; in_board = false; } } } } return B; } Board solve(){ auto bboard = Board(N2,0); auto B = simulate(bboard); int bscore = inv_count(B); int nscore = bscore; auto nboard = bboard; const double stemp = 1.0; const double etemp = 0; double elapsed = getElapsed(start_time); constexpr double end_time = 0.3; int subCnt = 0; int loopCnt = 0; int naccept = 0; int gaccept = 0; double temp = stemp; while(elapsed < end_time){ subCnt++; if(subCnt == 10){ subCnt = 0; elapsed = getElapsed(start_time); temp = stemp + elapsed * (etemp - stemp) / end_time; } int x = randint(N); int y = randint(N); int v = N*x+y; int ps = nboard[v]; int s = randint(3); if(ps == s){ s = (s+1)%3; } nboard[v] = s; auto tB = simulate(nboard); double tscore = inv_count(tB); //dprint(tB,tscore); //return nboard; const double diff = nscore - tscore; bool accept = false; if(diff > 0){ accept = true; } else if(diff <= 0){ if(log_rand() * temp < diff){ accept = true; } } if(accept){ naccept++; nscore = tscore; if(nscore < bscore){ gaccept++; dprint("bscore:", tscore, elapsed); bscore = nscore; bboard = nboard; } } else { nboard[v] = ps; } loopCnt++; } dprint("perm loopCnt:", loopCnt); dprint("perm bscore:", bscore); return bboard; } // main int main(){ initialize(); auto board = solve(); rep(x,N){ rep(y,N){ int v = N*x+y; std::cout << board[v]; } std::cout << std::endl; } if(local){ double elapsed = getElapsed(start_time); dprint("time:", elapsed); } deconstruct(); return 0; }