結果

問題 No.3735 Offbeat Permutation Tree
コンテスト
ユーザー てんぷら
提出日時 2026-09-19 16:15:51
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 175 ms / 2,000 ms
+ 312µs
コード長 6,472 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,691 ms
コンパイル使用メモリ 359,820 KB
実行使用メモリ 10,032 KB
最終ジャッジ日時 2026-09-19 16:16:14
合計ジャッジ時間 9,286 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#ifdef TEMPURA
#else
#define debug(...) ((void)0)
#define msg(...) ((void)0)
#endif
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for(int i = (int)(m); i < (int)(n); i++)

using ll = long long;
using ull = unsigned long long;
using i128 = __int128_t;

template <class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}
// #include <atcoder/modint>
// using mint = atcoder::modint998244353;

struct Xor128 {
    unsigned x, y, z, w;

    Xor128() : x(123456789), y(362436069), z(521288629), w(88675123){};

    inline unsigned xor128() {
        unsigned t;
        t = x ^ (x << 11);
        x = y;
        y = z;
        z = w;
        return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    }

    int nextInt(int x, int y) {
        return x + ((ull(xor128()) * (y - x)) >> 32);
    }

    double nextDouble(double a, double b) {
        return (double)(xor128() & 0xffff) / 0xffff * (b - a) + a;
    }
};
auto rnd = Xor128();

vector<pair<int, int>> create_random_path(int l, int r, int u, int d, int x, int y) {
    if(l > r || u > d) {
        return {};
    }
    if(rnd.nextInt(0, 2)) {
        auto v = x == l ? create_random_path(l + 1, r, u, d, x + 1, u + d - y) : create_random_path(l, r - 1, u, d, x - 1, u + d - y);
        if(y == u) {
            for(int j = d; j >= u; j--) {
                v.push_back({x, j});
            }
        } else {
            for(int j = u; j <= d; j++) {
                v.push_back({x, j});
            }
        }
        return v;
    } else {
        auto v = y == u ? create_random_path(l, r, u + 1, d, l + r - x, y + 1) : create_random_path(l, r, u, d - 1, l + r - x, y - 1);
        if(x == l) {
            for(int j = r; j >= l; j--) {
                v.push_back({j, y});
            }
        } else {
            for(int j = l; j <= r; j++) {
                v.push_back({j, y});
            }
        }
        return v;
    }
}

void solve() {
    int n;
    cin >> n;

    vector<int> a(n * n);
    rep(i, n * n) cin >> a[i];
    int s = 0;
    rep(i, n * n) s += a[i];
    if(s % n != 0) {
        cout << -1 << endl;
        return;
    }
    rep(_, 50) {
        auto v = create_random_path(0, n - 1, 0, n - 1, rnd.nextInt(0, 2) * (n - 1), rnd.nextInt(0, 2) * (n - 1));
        vector<int> ord;
        for(auto [x, y] : v) {
            ord.push_back(x * n + y);
        }
        vector<int> sum(n * n + 1);
        rep(i, n * n) {
            sum[i + 1] = (sum[i] + a[ord[i]]) % n;
        }
        vector<int> cnt(n);
        rep(i, n * n) {
            cnt[sum[i]] += 1;
        }
        if(cnt[0] < n) {
            continue;
        }
        vector<int> ans(n * n);
        int cur = 1;
        rep(i, n * n) {
            ans[ord[i]] = cur;
            if(sum[i + 1] == 0) {
                cur = min(n, cur + 1);
            }
        }
        rep(i, n * n) {
            cout << ans[i] << " \n"[i % n == n - 1];
        }
        return;
    }
    cout << -1 << endl;
}

vector<string> ans5r = {"0111", "1010", "1110", "0101", "1101"};
vector<string> ans5d = {"1011", "1000", "0001", "1110", "1101"};
vector<string> ans4r = {"101", "100", "011", "101"};
vector<string> ans4d = {"101", "011", "110", "101"};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    if(n <= 3) {
        cout << -1 << endl;
        return 0;
    }
    vector a(n, vector(n, 0));
    rep(i, n) rep(j, n) {
        a[i][j] = i * n + j + 1;
    }
    vector<pair<int, int>> ans;
    auto dfs = [&](auto &&self, int l, int sz, int f) -> void {
        int y = l;
        if(sz == 4) {
            rep(x, sz) rep(i, sz - 1) {
                if(ans4r[x][i] == '1') {
                    ans.push_back({a[x][y + i], a[x][y + i + 1]});
                }
            }
            rep(i, sz) rep(x, sz - 1) {
                if(ans4d[i][x] == '1') {
                    ans.push_back({a[x][y + i], a[x + 1][y + i]});
                }
            }
            return;
        } else if(sz == 5) {
            rep(x, sz) rep(i, sz - 1) {
                if(ans5r[x][i] == '1') {
                    ans.push_back({a[x][y + i], a[x][y + i + 1]});
                }
            }
            rep(i, sz) rep(x, sz - 1) {
                if(ans5d[i][x] == '1') {
                    ans.push_back({a[x][y + i], a[x + 1][y + i]});
                }
            }
            return;
        }
        if(f) {
            self(self, l + 2, sz - 2, !f);
            int y = l;
            ans.push_back({a[0][y], a[0][y + 1]});
            rep(i, sz - 1) {
                ans.push_back({a[i][y], a[i + 1][y]});
            }
            rep(i, sz - 1) {
                if(i != sz - 3) {
                    ans.push_back({a[i][y + 1], a[i + 1][y + 1]});
                }
            }
            REP(i, 1, sz - 1) {
                ans.push_back({a[sz - 1][y + i], a[sz - 1][y + i + 1]});
            }
            REP(i, 2, sz - 1) {
                ans.push_back({a[sz - 2][y + i], a[sz - 2][y + i + 1]});
            }
            ans.push_back({a[sz - 2][y + sz - 1], a[sz - 1][y + sz - 1]});
            ans.push_back({a[sz - 3][y + 1], a[sz - 3][y + 2]});
            ans.push_back({a[sz - 2][y + 2], a[sz - 3][y + 2]});
        } else {
            int y = l;
            self(self, l, sz - 2, !f);
            ans.push_back({a[0][y + sz - 1], a[0][y + sz - 2]});
            rep(i, sz - 1) {
                ans.push_back({a[i][y + sz - 1], a[i + 1][y + sz - 1]});
            }
            rep(i, sz - 1) {
                if(i != sz - 3) {
                    ans.push_back({a[i][y + sz - 2], a[i + 1][y + sz - 2]});
                }
            }
            rep(i, sz - 2) {
                ans.push_back({a[sz - 1][y + i], a[sz - 1][y + i + 1]});
            }
            rep(i, sz - 3) {
                ans.push_back({a[sz - 2][y + i], a[sz - 2][y + i + 1]});
            }
            ans.push_back({a[sz - 2][y], a[sz - 1][y]});
            ans.push_back({a[sz - 3][y + sz - 2], a[sz - 3][y + sz - 3]});
            ans.push_back({a[sz - 2][y + sz - 3], a[sz - 3][y + sz - 3]});
        }
    };
    dfs(dfs, 0, n, 1);
    for(auto [x, y] : ans) {
        cout << x << " " << y << endl;
    }
}
0