結果

問題 No.3593 I Love Sudoku
コンテスト
ユーザー tkdgkb
提出日時 2026-07-17 22:05:23
言語 C++23
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 7 ms / 2,000 ms
+ 483µs
コード長 2,318 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,464 ms
コンパイル使用メモリ 207,296 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2026-07-17 22:05:26
合計ジャッジ時間 2,186 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#pragma GCC optimize("O3,unroll-loops")
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <random>

using namespace std;

int subG[9][9];

void init() {
    for (int i = 0; i < 9; ++i) {
        for (int j = 0; j < 9; ++j) {
            int ix = i / 3, iy = i % 3;
            int jx = j / 3, jy = j % 3;
            subG[i][j] = ((ix - jx + 3) % 3) * 3 + ((iy - jy + 3) % 3);
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    init();
    
    vector<int> a(9);
    iota(a.begin(), a.end(), 0);
    vector<int> b(9);
    
    mt19937 rng(1337);

    while (true) {
        shuffle(a.begin(), a.end(), rng);
        iota(b.begin(), b.end(), 0);
        
        int a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5], a6 = a[6], a7 = a[7], a8 = a[8];
        
        do {
            int mask = 0;
            bool ok = true;
            for (int y = 0; y < 9; ++y) {
                const int* row = subG[y];
                int sum = a0 * b[row[0]] + a1 * b[row[1]] + a2 * b[row[2]] +
                          a3 * b[row[3]] + a4 * b[row[4]] + a5 * b[row[5]] +
                          a6 * b[row[6]] + a7 * b[row[7]] + a8 * b[row[8]];
                sum %= 9;
                if (mask & (1 << sum)) {
                    ok = false;
                    break;
                }
                mask |= (1 << sum);
            }
            
            if (ok) {
                for (int i = 0; i < 9; ++i) {
                    for (int j = 0; j < 9; ++j) {
                        int PA = i;
                        int QA = (j % 3) * 3 + (j / 3);
                        cout << a[subG[QA][PA]] << (j == 8 ? "" : " ");
                    }
                    cout << "\n";
                }
                for (int j = 0; j < 9; ++j) {
                    for (int k = 0; k < 9; ++k) {
                        int PB = (j % 3) * 3 + (j / 3);
                        int K = k / 3, m = k % 3;
                        int QB = m * 3 + ((m + K) % 3);
                        cout << b[subG[QB][PB]] << (k == 8 ? "" : " ");
                    }
                    cout << "\n";
                }
                return 0;
            }
        } while (next_permutation(b.begin(), b.end()));
    }
    return 0;
}
0