結果

問題 No.3739 Stronger Network
コンテスト
ユーザー てんぷら
提出日時 2026-09-19 18:05:52
言語 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  
実行時間 36 ms / 2,000 ms
+ 724µs
コード長 2,483 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,059 ms
コンパイル使用メモリ 341,736 KB
実行使用メモリ 10,028 KB
最終ジャッジ日時 2026-09-19 18:06:06
合計ジャッジ時間 6,255 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

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;

vector<int> create_permutation(int n) {
    assert(n % 2 == 0);
    int b = 0;
    while((1 << (b + 1)) <= n) {
        b += 1;
    }
    vector<int> ret;
    int cur = 0;
    rep(i, (1 << b)) {
        ret.push_back(cur);
        if(i % 2 == 0) {
            if((cur ^ (1 << b)) < n) {
                ret.push_back(cur ^ (1 << b));
                ret.push_back(cur ^ (1 << b) ^ 1);
            }
        }
        int c = countr_zero(unsigned(i + 1));
        cur ^= 1 << c;
    }
    debug(ret);
    return ret;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int h, w;
    cin >> h >> w;
    if(h % 2 == 1 || w % 2 == 1) {
        cout << -1 << endl;
        return 0;
    }
    auto vh = create_permutation(h), vw = create_permutation(w);
    int bh = 32 - countl_zero(unsigned(h - 1)), bw = 32 - countl_zero(unsigned(w - 1));
    vector<int> ord(bh + bw);
    rep(i, bw) ord[bh + i] = 1;
    int ma = 1 << 30;
    vector<int> best;
    do {
        int ch = 0, cw = 0;
        int res = 0;
        for(auto e : ord) {
            if(e == 0) {
                res |= ((h - 1) >> ch & 1) << (ch + cw);
                ch += 1;
            } else {
                res |= ((w - 1) >> cw & 1) << (ch + cw);
                cw += 1;
            }
        }
        if(res < ma) {
            ma = res;
            best = ord;
        }
    } while(next_permutation(ord.begin(), ord.end()));
    rep(i, h) rep(j, w) {
        int ch = 0, cw = 0;
        int res = 0;
        for(auto e : best) {
            if(e == 0) {
                res |= (vh[i] >> ch & 1) << (ch + cw);
                ch += 1;
            } else {
                res |= (vw[j] >> cw & 1) << (ch + cw);
                cw += 1;
            }
        }
        cout << res << " \n"[j + 1 == w];
    }
    return 0;
}
0