結果

問題 No.1775 Love Triangle 2
ユーザー 👑 hitonanodehitonanode
提出日時 2021-11-28 00:17:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 406 ms / 8,000 ms
コード長 6,663 bytes
コンパイル時間 7,093 ms
コンパイル使用メモリ 253,816 KB
実行使用メモリ 5,084 KB
最終ジャッジ日時 2023-09-16 22:21:06
合計ジャッジ時間 24,884 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
4,376 KB
testcase_01 AC 32 ms
4,380 KB
testcase_02 AC 30 ms
4,376 KB
testcase_03 AC 31 ms
4,376 KB
testcase_04 AC 378 ms
4,876 KB
testcase_05 AC 398 ms
5,084 KB
testcase_06 AC 406 ms
4,968 KB
testcase_07 AC 406 ms
5,012 KB
testcase_08 AC 389 ms
4,948 KB
testcase_09 AC 125 ms
4,876 KB
testcase_10 AC 133 ms
4,876 KB
testcase_11 AC 97 ms
4,896 KB
testcase_12 AC 89 ms
4,976 KB
testcase_13 AC 77 ms
4,896 KB
testcase_14 AC 68 ms
4,832 KB
testcase_15 AC 60 ms
4,928 KB
testcase_16 AC 58 ms
4,928 KB
testcase_17 AC 56 ms
4,840 KB
testcase_18 AC 56 ms
4,956 KB
testcase_19 AC 116 ms
4,968 KB
testcase_20 AC 107 ms
4,884 KB
testcase_21 AC 99 ms
4,956 KB
testcase_22 AC 91 ms
4,880 KB
testcase_23 AC 78 ms
4,876 KB
testcase_24 AC 97 ms
4,968 KB
testcase_25 AC 86 ms
4,948 KB
testcase_26 AC 77 ms
4,832 KB
testcase_27 AC 87 ms
5,012 KB
testcase_28 AC 71 ms
4,880 KB
testcase_29 AC 52 ms
4,948 KB
testcase_30 AC 72 ms
4,920 KB
testcase_31 AC 115 ms
4,932 KB
testcase_32 AC 157 ms
5,080 KB
testcase_33 AC 207 ms
5,080 KB
testcase_34 AC 208 ms
4,956 KB
testcase_35 AC 235 ms
4,952 KB
testcase_36 AC 278 ms
4,832 KB
testcase_37 AC 323 ms
4,972 KB
testcase_38 AC 383 ms
4,876 KB
testcase_39 AC 65 ms
4,876 KB
testcase_40 AC 95 ms
4,952 KB
testcase_41 AC 121 ms
4,840 KB
testcase_42 AC 138 ms
5,008 KB
testcase_43 AC 138 ms
4,944 KB
testcase_44 AC 102 ms
4,952 KB
testcase_45 AC 228 ms
4,948 KB
testcase_46 AC 155 ms
4,880 KB
testcase_47 AC 83 ms
4,888 KB
testcase_48 AC 183 ms
4,896 KB
testcase_49 AC 57 ms
4,896 KB
testcase_50 AC 77 ms
4,836 KB
testcase_51 AC 117 ms
4,876 KB
testcase_52 AC 159 ms
4,876 KB
testcase_53 AC 216 ms
4,952 KB
testcase_54 AC 222 ms
4,928 KB
testcase_55 AC 264 ms
4,832 KB
testcase_56 AC 291 ms
4,844 KB
testcase_57 AC 331 ms
4,948 KB
testcase_58 AC 393 ms
4,876 KB
testcase_59 AC 67 ms
4,892 KB
testcase_60 AC 87 ms
4,872 KB
testcase_61 AC 112 ms
4,924 KB
testcase_62 AC 82 ms
5,084 KB
testcase_63 AC 105 ms
4,952 KB
testcase_64 AC 124 ms
4,884 KB
testcase_65 AC 78 ms
4,876 KB
testcase_66 AC 113 ms
4,896 KB
testcase_67 AC 149 ms
4,892 KB
testcase_68 AC 99 ms
5,080 KB
testcase_69 AC 163 ms
4,932 KB
testcase_70 AC 305 ms
4,876 KB
testcase_71 AC 368 ms
4,952 KB
testcase_72 AC 377 ms
4,876 KB
testcase_73 AC 353 ms
4,940 KB
testcase_74 AC 379 ms
4,928 KB
testcase_75 AC 390 ms
4,968 KB
testcase_76 AC 339 ms
4,936 KB
testcase_77 AC 378 ms
4,952 KB
testcase_78 AC 387 ms
4,884 KB
testcase_79 AC 131 ms
4,956 KB
testcase_80 AC 152 ms
4,876 KB
testcase_81 AC 196 ms
4,844 KB
testcase_82 AC 187 ms
4,888 KB
testcase_83 AC 220 ms
4,888 KB
testcase_84 AC 172 ms
4,872 KB
testcase_85 AC 223 ms
5,080 KB
testcase_86 AC 153 ms
4,840 KB
testcase_87 AC 202 ms
4,940 KB
testcase_88 AC 307 ms
4,948 KB
testcase_89 AC 265 ms
4,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// Validator
#include "testlib.h"
#include <array>
#include <cassert>
#include <chrono>
#include <iostream>
#include <random>
#include <vector>
using namespace std;
// https://epubs.siam.org/doi/10.1137/1.9781611973099.139

// Nimber (64 bit)
// Reference:
// - https://en.wikipedia.org/wiki/Nimber
// - https://kyopro-friends.hatenablog.com/entry/2020/04/07/195850
// - https://judge.yosupo.jp/submission/4542 (implementation idea)
struct Nimber {
    using ull = unsigned long long;
    ull v;
    const static std::array<std::array<unsigned, 256>, 256> small_table;
    const static std::array<std::array<std::array<ull, 256>, 8>, 8> precalc;

    explicit operator bool() const { return v != 0; }
    Nimber(ull val = 0) : v(val) {}
    Nimber operator+(const Nimber &x) const { return Nimber(v ^ x.v); }
    Nimber operator-(const Nimber &x) const { return Nimber(v ^ x.v); }
    Nimber operator-() const { return *this; }
    Nimber &operator+=(const Nimber &x) { return *this = *this + x; }
    Nimber &operator-=(const Nimber &x) { return *this = *this + x; }
    template <class IStream> friend IStream &operator>>(IStream &is, Nimber &x) {
        ull v;
        return is >> v, x = Nimber(v), is;
    }
    template <class OStream> friend OStream &operator<<(OStream &os, const Nimber &x) { return os << x.v; }
    bool operator==(const Nimber &x) const { return v == x.v; }
    bool operator!=(const Nimber &x) const { return v != x.v; }
    bool operator<(const Nimber &x) const { return v < x.v; }

    static ull _rec(ull x, ull y) {
        if (x == 0 or y == 0) return 0;
        if (x < y) x ^= y, y ^= x, x ^= y; // Make x >= y
        if (y == 1) return x;
        for (int shift = 64 / 2;; shift >>= 1) {
            ull mask = (1ULL << shift) - 1;
            if (y >> shift) {
                ull v00 = _rec(x & mask, y & mask);
                ull v01 = _rec(x & mask, y >> shift);
                ull v10 = _rec(x >> shift, y & mask);
                ull v11 = _rec(x >> shift, y >> shift);
                return v00 ^ ((v01 ^ v10 ^ v11) << shift) ^ _rec(v11, 1ULL << (shift - 1));
            } else if (x >> shift) {
                return (_rec(x >> shift, y) << shift) ^ _rec(x & mask, y);
            }
        }
    }
    Nimber operator*(const Nimber &x) const {
        ull ret = 0;
        for (int d = 0; d < 8; ++d) {
            for (int e = 0; e < 8; ++e) { ret ^= precalc[d][e][small_table[(v >> (d * 8)) & 255][(x.v >> (e * 8)) & 255]]; }
        }
        return Nimber(ret);
    }
    Nimber &operator*=(const Nimber &x) { return *this = *this * x; }
};

const std::array<std::array<unsigned, 256>, 256> Nimber::small_table = []() {
    std::array<std::array<unsigned, 256>, 256> ret;
    for (int i = 0; i < 256; ++i) {
        for (int j = 0; j < 256; ++j) ret[i][j] = _rec(i, j);
    }
    return ret;
}();
const std::array<std::array<std::array<unsigned long long, 256>, 8>, 8> Nimber::precalc = []() {
    std::array<std::array<std::array<unsigned long long, 256>, 8>, 8> ret;
    for (int d = 0; d < 8; ++d) {
        for (int e = 0; e < 8; ++e) {
            ull p = _rec(1ULL << (8 * d), 1ULL << (8 * e));
            for (int i = 0; i < 256; ++i) ret[d][e][i] = _rec(p, i);
        }
    }
    return ret;
}();

struct rand_int_ {
    using lint = long long;
    mt19937 mt;
    rand_int_() : mt(chrono::steady_clock::now().time_since_epoch().count()) {}
    lint operator()(lint x) { return this->operator()(0, x); } // [0, x)
    lint operator()(lint l, lint r) {
        uniform_int_distribution<lint> d(l, r - 1);
        return d(mt);
    }
} rnd_gen;

template <class Fq> int solve(const vector<vector<Fq>> &w, int x, int y, int z, int dlim) {
    // x を始点に y, z を経由し N に到達する長さ dlim 以下のパスがあるかを判定
    int N = w.size() - 1;
    // dp[visited y][visited z][current v][previous v]
    vector dp(2, vector(2, vector(N + 1, vector<Fq>(N + 1, 0))));
    dp[0][0][N][N] = 1;
    for (int d = 1; d <= dlim; ++d) {
        vector dpnxt(2, vector(2, vector(N + 1, vector<Fq>(N + 1, 0))));
        for (int visy = 0; visy < 2; ++visy) {
            for (int visz = 0; visz < 2; ++visz) {
                for (int now = 0; now <= N; ++now) {
                    Fq sum = 0;
                    for (int prv = 0; prv <= N; ++prv) sum += dp[visy][visz][now][prv];
                    for (int nxt = 0; nxt <= N; ++nxt) {
                        if (visy and (nxt == y)) continue;
                        if (visz and (nxt == z)) continue;
                        Fq wadd = sum - ((now == y or now == z) ? dp[visy][visz][now][nxt] : 0);
                        dpnxt[visy | (nxt == y)][visz | (nxt == z)][nxt][now] += wadd * w[now][nxt];
                    }
                }
            }
        }
        dp = dpnxt;
        for (int i = 0; i < N; ++i) {
            if (dp[1][1][x][i] != 0) return d;
        }
    }
    return -1; // not found
}

int main(int argc, char *argv[]) {
    registerValidation(argc, argv);
    int N = inf.readInt(1, 100);
    inf.readSpace();
    int M = inf.readInt(3, N * (N - 1) / 2);
    inf.readEoln();

    int x = inf.readInt(1, N);
    inf.readSpace();
    int y = inf.readInt(1, N);
    inf.readSpace();
    int z = inf.readInt(1, N);
    inf.readEoln();

    assert(x < y and y < z);

    --x, --y, --z;
    vector weight(N + 1, vector<Nimber>(N + 1));
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < i; ++j) {
            Nimber w = rnd_gen(1LL << 16);
            Nimber wn = rnd_gen(1LL << 16);
            if (j != x) {
                weight[j][i] = w;
            } else {
                weight[N][i] = wn;
            }
            if (i != x) {
                weight[i][j] = w;
            } else {
                weight[N][j] = wn;
            }
        }
    }

    vector<vector<int>> conn(N + 1, vector<int>(N + 1));
    for (int i = 1; i <= M; ++i) {
        int a = inf.readInt(1, N);
        inf.readSpace();
        int b = inf.readInt(1, N);
        inf.readEoln();

        assert(a < b);

        --a, --b;

        if (i == 1) assert(a == x and b == y);
        if (i == 2) assert(a == x and b == z);
        if (i == 3) assert(a == y and b == z);

        assert(conn[a][b] == 0);
        conn[a][b] = 1;

        weight[a][b] = weight[b][a] = 0;
        if (a == x) weight[N][b] = 0;
        if (b == x) weight[N][a] = 0;
    }

    inf.readEof();
    cout << solve(weight, x, y, z, N) << '\n';
}
0