結果

問題 No.1815 K色問題
ユーザー Ricky_ponRicky_pon
提出日時 2022-03-14 19:28:26
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 9,026 bytes
コンパイル時間 2,298 ms
コンパイル使用メモリ 211,304 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-13 11:32:52
合計ジャッジ時間 3,536 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 58 ms
6,676 KB
testcase_07 AC 17 ms
6,676 KB
testcase_08 AC 110 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 141 ms
6,676 KB
testcase_15 AC 145 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <atcoder/modint>

#include <atcoder/modint>
#include <vector>

template <class T>
struct Factorial {
    int n;
    std::vector<T> fact;
    std::vector<T> revfact;

    Factorial(int n) : n(n) {
        fact.resize(n + 1);
        revfact.resize(n + 1);
        fact[0] = 1;
        for (int i = 0; i < n; ++i) fact[i + 1] = fact[i] * (i + 1);

        revfact[n] = fact[n].inv();
        for (int i = n - 1; i >= 0; --i) revfact[i] = revfact[i + 1] * (i + 1);
    }

    T comb(int n, int r) {
        if (n < r || r < 0) return 0;
        return fact[n] * revfact[r] * revfact[n - r];
    }

    T perm(int n, int r) {
        if (n < r || r < 0) return 0;
        return fact[n] * revfact[n - r];
    }
};


#include <assert.h>

#include <vector>

template <class T>
struct Matrix {
    std::vector<std::vector<T>> a;

    Matrix(int n, int m) : a(n, std::vector<T>(m, 0)) {}
    Matrix(int n) : a(n, std::vector<T>(n, 0)) {}

    int height() const { return a.size(); }
    int width() const { return a[0].size(); }

    inline const std::vector<T> &operator[](int i) const { return a[i]; }
    inline std::vector<T> &operator[](int i) { return a[i]; }

    static Matrix id(int n) {
        Matrix res(n);
        for (int i = 0; i < n; i++) res[i][i] = 1;
        return res;
    }

    Matrix &operator*=(const Matrix &b) {
        assert(width() == b.height());
        std::vector<std::vector<T>> c(height(), std::vector<T>(b.width()));
        for (int i = 0; i < height(); ++i) {
            for (int k = 0; k < b.height(); ++k) {
                for (int j = 0; j < b.width(); ++j) {
                    c[i][j] += a[i][k] * b[k][j];
                }
            }
        }
        a.swap(c);
        return *this;
    }

    Matrix pow(long long n) const {
        auto x = (*this), res = id(height());
        while (n) {
            if (n & 1) {
                res *= x;
                --n;
            } else {
                x *= x;
                n >>= 1;
            }
        }
        return res;
    }

    Matrix operator*(const Matrix &b) const { return Matrix(*this) *= b; }

    std::vector<T> operator*(const std::vector<T> &v) {
        assert(width() == (int)v.size());
        std::vector<T> res(height(), 0);
        for (int i = 0; i < height(); ++i) {
            for (int j = 0; j < width(); ++j) {
                res[i] += a[i][j] * v[j];
            }
        }
        return res;
    }
};

template <class T>
std::pair<int, bool> gauss_jordan(Matrix<T> &a) {
    int rnk = 0;
    bool swp = false;
    for (int j = 0; j < a.width(); ++j) {
        int pivot = -1;
        for (int i = rnk; i < a.height(); ++i) {
            if (a[i][j] != 0) {
                pivot = i;
                break;
            }
        }
        if (pivot < 0) continue;
        swap(a[pivot], a[rnk]);
        if (pivot != rnk) swp ^= true;
        for (int i = 0; i < a.height(); ++i) {
            if (i != rnk && a[i][j] != 0) {
                auto coef = a[i][j] / a[rnk][j];
                for (int k = j; k < a.width(); ++k) {
                    a[i][k] -= a[rnk][k] * coef;
                }
            }
        }
        ++rnk;
    }
    return {rnk, swp};
}

template <class T>
T determinant(Matrix<T> a) {
    auto [rnk, swp] = gauss_jordan(a);
    if (rnk < a.height()) return 0;
    T res = 1;
    for (int i = 0; i < a.height(); ++i) res *= a[i][i];
    if (swp) res = -res;
    return res;
}

template <class T>
std::pair<std::vector<T>, std::vector<std::vector<T>>>
system_of_linear_equations(Matrix<T> a, const std::vector<T> &b) {
    assert(a.height() == (int)b.size());
    Matrix<T> aug(a.height(), a.width() + 1);
    for (int i = 0; i < a.height(); ++i) {
        for (int j = 0; j < a.width(); ++j) {
            aug[i][j] = a[i][j];
        }
        aug[i][a.width()] = b[i];
    }

    auto rnk = gauss_jordan(a).first, rnk_aug = gauss_jordan(aug).first;

    std::vector<T> solution;
    std::vector<std::vector<T>> kernel;
    if (rnk < rnk_aug) return {solution, kernel};

    solution.resize(a.width(), 0);
    std::vector<bool> used(a.width(), false);
    std::vector<int> pos(rnk);
    for (int i = 0; i < rnk; ++i) {
        for (int j = 0; j < a.width(); ++j) {
            if (aug[i][j] != 0) {
                solution[j] = aug[i][a.width()] / aug[i][j];
                used[j] = true;
                pos[i] = j;
                break;
            }
        }
    }
    for (int j = 0; j < a.width(); ++j) {
        if (!used[j]) {
            std::vector<T> v(a.width(), 0);
            v[j] = 1;
            for (int i = 0; i < rnk; ++i) {
                v[pos[i]] = -aug[i][j] / aug[i][pos[i]];
            }
            kernel.push_back(v);
        }
    }
    return {solution, kernel};
}


#include <algorithm>
#include <cassert>
#include <vector>

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

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

template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}

template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

template <typename T>
struct CoordComp {
    std::vector<T> v;
    bool sorted;

    CoordComp() : sorted(false) {}

    int size() { return v.size(); }

    void add(T x) { v.push_back(x); }

    void build() {
        std::sort(v.begin(), v.end());

        v.erase(std::unique(v.begin(), v.end()), v.end());
        sorted = true;
    }

    int get_idx(T x) {
        assert(sorted);
        return lower_bound(v.begin(), v.end(), x) - v.begin();
    }

    T &operator[](int i) { return v[i]; }
};


#define For(i, a, b) for (int i = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For(i, 0, n)
#define rrep(i, n) rFor(i, n, 0)
#define fi first
#define se second

using namespace std;

using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;

using mint = atcoder::modint1000000007;

int main() {
    int n, K;
    lint m;
    scanf("%d%lld%d", &n, &m, &K);
    if (m * n < K) {
        printf("%d\n", 0);
        return 0;
    }
    if (K == 1) {
        printf("%d\n", m * n == 1 ? 1 : 0);
        return 0;
    }
    Factorial<mint> fc(K);

    if (n == 1) {
        mint ans = 0;
        rep(i, K - 1) {
            mint c = K - i;
            mint tmp = fc.comb(K, i) * c * (c - 1).pow(m - 1);
            if (i % 2 == 1)
                ans -= tmp;
            else
                ans += tmp;
        }
        printf("%u\n", ans.val());
    } else if (n == 2) {
        mint ans = 0;
        rep(i, K - 1) {
            mint c = K - i;
            mint b = (c - 1) * (c - 1) - (c - 2);
            mint tmp = fc.comb(K, i) * c * (c - 1) * b.pow(m - 1);
            if (i % 2 == 1)
                ans -= tmp;
            else
                ans += tmp;
        }
        printf("%u\n", ans.val());
    } else {
        constexpr lint MOD = 1'000'000'007;
        mint ans = 0;

        rep(i, K - 1) {
            lint c = K - i;

            lint A00, A01 = 0, A10 = 0, A11 = 0;
            A00 = ((c - 1) * (c - 1) - (c - 2) + MOD) % MOD;
            if (c >= 3) {
                A01 = ((c - 2) * (c - 1) - (c - 3) + MOD) % MOD;
                A10 = ((c - 1) * (c - 1) * (c - 1) - 2 * (c - 2) * (c - 1) -
                       (c - 1) * (c - 1) + 2 * (c - 2) + MOD * MOD) %
                      MOD;
                A11 = ((c - 1) * (c - 1) * (c - 1) - 3 * (c - 2) * (c - 1) +
                       2 * (c - 3) + MOD * MOD) %
                      MOD;
            }

            vector<lint> b = {(c * (c - 1)) % MOD,
                              (c * (c - 1) * (c - 2)) % MOD};
            lint t = m - 1;
            while (t) {
                if (t & 1) {
                    lint nb0, nb1;
                    nb0 = (A00 * b[0] + A01 * b[1]) % MOD;
                    nb1 = (A10 * b[0] + A11 * b[1]) % MOD;
                    b[0] = nb0;
                    b[1] = nb1;
                    --t;
                }
                if (t > 0) {
                    lint nA00, nA01, nA10, nA11;
                    nA00 = (A00 * A00 + A01 * A10) % MOD;
                    nA01 = (A00 * A01 + A01 * A11) % MOD;
                    nA10 = (A10 * A00 + A11 * A10) % MOD;
                    nA11 = (A10 * A01 + A11 * A11) % MOD;
                    A00 = nA00;
                    A01 = nA01;
                    A10 = nA10;
                    A11 = nA11;
                    t >>= 1;
                }
            }
            mint tmp = fc.comb(K, i) * ((b[0] + b[1]) % MOD);
            if (i % 2 == 1)
                ans -= tmp;
            else
                ans += tmp;
        }
        printf("%u\n", ans.val());
    }
}
0