結果
| 問題 |
No.3131 Twin Slide Puzzles
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2025-04-25 23:49:17 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 596 ms / 4,000 ms |
| コード長 | 6,352 bytes |
| コンパイル時間 | 1,528 ms |
| コンパイル使用メモリ | 128,856 KB |
| 実行使用メモリ | 12,720 KB |
| 最終ジャッジ日時 | 2025-06-20 02:48:40 |
| 合計ジャッジ時間 | 23,180 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 59 |
ソースコード
#ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(i64 i=0; i<i64(n); i++)
const i64 INF = 1001001001001001001;
template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; }
template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; }
using namespace std;
#include <cassert>
#include <cstdint>
#include <array>
namespace nachia{
class Xoshiro256pp{
public:
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
private:
std::array<u64, 4> s;
// https://prng.di.unimi.it/xoshiro256plusplus.c
static inline uint64_t rotl(const uint64_t x, int k) noexcept {
return (x << k) | (x >> (64 - k));
}
inline uint64_t gen(void) noexcept {
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;
}
// https://xoshiro.di.unimi.it/splitmix64.c
u64 splitmix64(u64& x) {
u64 z = (x += 0x9e3779b97f4a7c15);
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
return z ^ (z >> 31);
}
public:
void seed(u64 x = 7001){
assert(x != 0);
s[0] = x;
for(int i=1; i<4; i++) s[i] = splitmix64(x);
}
std::array<u64, 4> getState() const { return s; }
void setState(std::array<u64, 4> a){ s = a; }
Xoshiro256pp(){ seed(); }
u64 rng64() { return gen(); }
u64 operator()(){ return gen(); }
// generate x : l <= x <= r
u64 random_unsigned(u64 l,u64 r){
assert(l<=r);
r-=l;
auto res = rng64();
if(res<=r) return res+l;
u64 d = r+1;
u64 max_valid = 0xffffffffffffffff/d*d;
while(true){
auto res = rng64();
if(res<=max_valid) break;
}
return res%d+l;
}
// generate x : l <= x <= r
i64 random_signed(i64 l,i64 r){
assert(l<=r);
u64 unsigned_l = (u64)l ^ (1ull<<63);
u64 unsigned_r = (u64)r ^ (1ull<<63);
u64 unsigned_res = random_unsigned(unsigned_l,unsigned_r) ^ (1ull<<63);
return (i64)unsigned_res;
}
// permute x : n_left <= x <= n_right
// output r from the front
template<class Int>
std::vector<Int> random_nPr(Int n_left, Int n_right, Int r){
Int n = n_right-n_left;
assert(n>=0);
assert(r<=(1ll<<27));
if(r==0) return {};
assert(n>=r-1);
std::vector<Int> V;
std::unordered_map<Int,Int> G;
for(int i=0; i<r; i++){
Int p = random_signed(i,n);
Int x = p - G[p];
V.push_back(x);
G[p] = p - (i - G[i]);
}
for(Int& v : V) v+=n_left;
return V;
}
// V[i] := V[perm[i]]
// using swap
template<class E,class PermInt_t>
void permute_inplace(std::vector<E>& V,std::vector<PermInt_t> perm){
assert(V.size() == perm.size());
int N=V.size();
for(int i=0; i<N; i++){
int p=i;
while(perm[p]!=i){
assert(0 <= perm[p] && perm[p] < N);
assert(perm[p] != perm[perm[p]]);
std::swap(V[p],V[perm[p]]);
int pbuf = perm[p]; perm[p] = p; p = pbuf;
}
perm[p] = p;
}
}
template<class E>
std::vector<E> shuffle(const std::vector<E>& V){
int N=V.size();
auto P = random_nPr(0,N-1,N);
std::vector<E> res;
res.reserve(N);
for(int i=0; i<N; i++) res.push_back(V[P[i]]);
return res;
}
// shuffle using swap
template<class E>
void shuffle_inplace(std::vector<E>& V){
int N=V.size();
permute_inplace(V,random_nPr(0,N-1,N));
}
};
} // namespace nachia
#include <random>
#include <chrono>
nachia::Xoshiro256pp rng;
namespace RngInitInstance { struct RngInit { RngInit(){
unsigned long long seed1 = std::random_device()();
unsigned long long seed2 = std::chrono::high_resolution_clock::now().time_since_epoch().count();
rng.seed(seed1 ^ seed2);
} } a; }
void testcase(){
i64 N; cin >> N;
i64 nn = N;
vector<i64> A(N*N); rep(i,N*N) cin >> A[i];
vector<i64> Tx(N*N); rep(i,N) rep(j,N) Tx[i*N+j] = (i+j)%2;
N = N * N;
i64 n = N;
unordered_map<u64, i64> D;
auto eval = [&](const vector<i64>& F) -> i64 {
i64 vis[16] = {};
i64 q = 0;
rep(i,n) q += A[i] * F[i];
i64 t = n % 2;
rep(i,n) if(vis[i] == 0){
i64 p = i;
while(vis[p] == 0){ vis[p] = 1; p = F[p]; }
t ^= 1;
}
rep(i,n) if(F[i] == 0 && Tx[i] != t) return -1;
return q;
};
auto decodeF = [&](u64 x) -> vector<i64> {
vector<i64> res(n);
rep(i,n) res[i] = (x >> (4*i)) % 16;
return res;
};
auto output = [&](vector<i64> X) -> void {
rep(i,nn){
rep(j,nn){
if(j) cout << " ";
i64 a = i * nn + j;
a = (a < n ? X[a] : a);
cout << a;
}
cout << "\n";
}
};
auto query = [&](const vector<i64>& F) -> bool {
i64 d = eval(F);
if(d < 0) return false;
u64 enc = 0;
rep(f,n) enc += u64(F[f]) << (4*f);
if(D.find(d) != D.end()){
auto G = decodeF(D[d]);
cout << "Yes\n";
output(F);
output(G);
return true;
}
D[d] = enc;
return false;
};
if(N <= 9){
vector<i64> F(n);
rep(i,n) F[i] = i;
do{
if(query(F)) return;
} while(next_permutation(F.begin(), F.end()));
cout << "No\n";
} else {
n = 16;
vector<i64> F(n);
rep(i,n) F[i] = i;
while(true){
rng.shuffle_inplace(F);
if(query(F)) return;
}
}
}
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
testcase();
return 0;
}
Nachia