結果

問題 No.1815 K色問題
ユーザー 👑 NachiaNachia
提出日時 2022-01-20 22:36:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,341 ms / 2,000 ms
コード長 7,036 bytes
コンパイル時間 946 ms
コンパイル使用メモリ 85,220 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-13 11:30:38
合計ジャッジ時間 5,341 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 61 ms
6,676 KB
testcase_07 AC 17 ms
6,676 KB
testcase_08 AC 866 ms
6,676 KB
testcase_09 AC 11 ms
6,676 KB
testcase_10 AC 16 ms
6,676 KB
testcase_11 AC 228 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 1,192 ms
6,676 KB
testcase_15 AC 1,341 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


#include <vector>
#include <utility>

namespace nachia{

template<unsigned int MOD>
struct StaticModint{
private:
    using u64 = unsigned long long;
    unsigned int x;
public:

    using my_type = StaticModint;

    StaticModint() : x(0){}
    StaticModint(unsigned int v) : x(v){}
    unsigned int operator*() const { return x; }
    my_type& operator+=(const my_type& r){ auto t = x + r.x; if(t >= MOD) t -= MOD; x = t; return *this; }
    my_type operator+(const my_type& r) const { my_type res = *this; return res += r; }
    my_type& operator-=(const my_type& r){ auto t = x + MOD - r.x; if(t >= MOD) t -= MOD; x = t; return *this; }
    my_type operator-(const my_type& r) const { my_type res = *this; return res -= r; }
    my_type operator-() const { my_type res = *this; res.x = ((res.x == 0) ? 0 : (MOD - res.x)); return res; }
    my_type& operator*=(const my_type& r){ x = (u64)x * r.x % MOD; return *this; }
    my_type operator*(const my_type& r) const { my_type res = *this; return res *= r; }
    my_type pow(unsigned long long i) const {
        my_type a = *this, res = 1;
        while(i){ if(i & 1) res *= a; a *= a; i >>= 1; }
        return res;
    }
    my_type inv() const { return pow(MOD-2); }
    unsigned int val() const { return x; }
    static unsigned int get_mod() { return MOD; }
    my_type& operator/=(const my_type& r){ return operator*=(r.inv()); }
    my_type operator/(const my_type& r) const { return operator*(r.inv()); }
};

}

#include <cassert>


namespace nachia{

template<class Elem>
struct MatrixModulo{
private:
    int h;
    int w;
    std::vector<Elem> elems;

public:
    
    MatrixModulo(int new_h, int new_w){ h = new_h; w = new_w; elems.assign(h * w, 0); }
    MatrixModulo(const MatrixModulo&) = default;
    int height() const { return h; }
    int width() const { return w; }
    typename std::vector<Elem>::iterator operator[](int y){ return elems.begin() + (y * w); }
    typename std::vector<Elem>::const_iterator operator[](int y) const { return elems.begin() + (y * w); }
    static MatrixModulo identity(int idx){ auto res = MatrixModulo(idx, idx); for(int i = 0; i < idx; i++) res[i][i] = 1; return res; }
    MatrixModulo operator*(const MatrixModulo& r) const {
        assert(w == r.h);
        auto res = MatrixModulo(h, r.w);
        for (int i=0; i<h; i++) for (int j=0; j<w; j++) for (int k=0; k<r.w; k++) res[i][k] += (*this)[i][j] * r[j][k];
        return res;
    }
    Elem det() const {
        assert(h == w);
        MatrixModulo g = *this;
        Elem ans = 1;
        for (int i=0; i<h; i++) {
            int tg = -1;
            for (int j=i; j<h; j++) { if (g[j][i] != 0) tg = j; }
            if (tg == -1) return 0;
            if (tg != i) ans = -ans;
            for (int j=0; j<h; j++) std::swap(g[i][j], g[tg][j]); tg = i;
            ans *= g[i][i];
            Elem const_coeff = g[i][i].inv();
            for (int j=0; j<h; j++) g[i][j] *= const_coeff;
            for (int j=i+1; j<h; j++) for(int k=h-1; k>=i; k--) g[j][k] -= g[j][i] * g[i][k];
        }
        return ans;
    }
    int rank() const {
        MatrixModulo g = *this;
        int y = 0;
        for (int d=0; d<w; d++) {
            if(y == h) break;
            int tg = -1;
            for (int i=y; i<h; i++) { if (g[i][d] != 0){ tg = i; break; } }
            if (tg == -1) continue;
            for (int j=d; j<w; j++) std::swap(g[y][j], g[tg][j]); tg = y;
            Elem const_coeff = g[y][d].inv();
            for (int j=d; j<w; j++) g[y][j] *= const_coeff;
            for (int i=y+1; i<h; i++) for(int j=w-1; j>=d; j--) g[i][j] -= g[i][d] * g[y][j];
            y++;
        }
        return y;
    }
    MatrixModulo linear_equation() const {
        MatrixModulo g = *this;
        int y = 0;
        std::vector<std::pair<int,int>> det_var;
        std::vector<int> rank_var;
        for (int d=0; d<w-1; d++) {
            int tg = -1;
            for (int i=y; i<h; i++) { if (g[i][d] != 0){ tg = i; break; } }
            if (tg == -1){ rank_var.push_back(d); continue; }
            for (int j=d; j<w; j++) std::swap(g[y][j], g[tg][j]); tg = y;
            Elem const_coeff = g[y][d].inv();
            for (int j=d; j<w; j++) g[y][j] *= const_coeff;
            for (int i=0; i<h; i++) if (i != y) for(int j=w-1; j>=d; j--) g[i][j] -= g[i][d] * g[y][j];
            det_var.push_back(std::make_pair(d,y));
            y++;
        }
        for (int i=y; i<h; i++) if (g[i][w-1].val() != 0) return MatrixModulo(0,0);
        MatrixModulo solution(1 + rank_var.size(), w);
        for (auto [x,i] : det_var) { solution[0][x] = g[i][w-1]; }
        solution[0][w-1] = 1;
        for (int d=0; d<rank_var.size(); d++) {
            int varid = rank_var[d];
            solution[d+1][varid] = -1;
            for (auto [x,i] : det_var) { solution[d+1][x] = g[i][varid]; }
        }
        return solution;
    }
    MatrixModulo pow(unsigned long long i){
        auto a = *this;
        auto res = identity(height());
        while(i){
            if(i % 2 == 1) res = res * a;
            a = a * a;
            i /= 2;
        }
        return res;
    }
};

}

#include <iostream>
#include <algorithm>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using i32 = int;
using u32 = unsigned int;
#define rep(i,n) for(int i=0; i<(n); i++)


using modint = nachia::StaticModint<1000000007>;
using matrix = nachia::MatrixModulo<modint>;


modint f1(int k, u64 m){
    if(k < 1) return 0;
    return modint(k) * modint(k-1).pow(m-1);
}
modint f2(int k, u64 m){
    if(k < 2) return 0;
    if(k == 2) return 2;
    modint a = modint(k) * (k-1);
    modint b = a - modint(k*2-2) + 1;
    return a * b.pow(m-1);
}
modint f3(int k, u64 m){
    if(k < 2) return 0;
    if(k == 2) return 2;
    matrix A(2,2);
    modint kP0 = modint(1);
    modint kP1 = modint(k);
    modint kP2 = modint(k) * (k-1);
    modint kP3 = modint(k) * (k-1) * (k-2);
    A[0][0] = modint(k-2) * (k-2) + (k-1);
    A[1][0] = modint(k-2) * (k-3) + (k-1);
    A[0][1] = ( modint(k-2) * (k-3) + (k-1) ) * (k-2);
    A[1][1] = modint(k) * (k-1) * (k-2) - modint(k-1) * (k-2) * 3 + modint(k-2) * 3 - 1;

    A = A.pow(m-1);

    return A[0][0] * kP2 + A[0][1] * kP2 + A[1][0] * kP3 + A[1][1] * kP3;
}
modint f(int n, int k, u64 m){
    if(n == 1) return f1(k,m);
    if(n == 2) return f2(k,m);
    if(n == 3) return f3(k,m);
    return 0;
}


int main() {
    int N; cin >> N;
    u64 M; cin >> M;
    int K; cin >> K;
    modint ans = 0;
    vector<modint> F(K+1,1);
    for(int i=1; i<=K; i++) F[i]=F[i-1]*i;
    vector<modint> iF(K+1); iF[K] = F[K].inv();
    for(int i=K; i>=1; i--) iF[i-1]=iF[i]*i;
    for(int k=0; k<=K; k++){
        auto tmp = f(N,K-k,M) * F[K] * iF[k] * iF[K-k];
        if(k % 2 == 0) ans += tmp;
        else ans -= tmp;
    }
    cout << *ans << endl;
    return 0;
}



struct ios_do_not_sync{
    ios_do_not_sync(){
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }
} ios_do_not_sync_instance;
0