結果

問題 No.1815 K色問題
ユーザー 沙耶花沙耶花
提出日時 2022-01-20 08:34:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 6,708 bytes
コンパイル時間 12,143 ms
コンパイル使用メモリ 337,076 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2024-09-29 22:38:36
合計ジャッジ時間 19,919 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 TLE -
testcase_15 TLE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:15:9: warning: #pragma once in main file
   15 | #pragma once
      |         ^~~~

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include "testlib.h"
#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000001

/*
https://nyaannyaan.github.io/library/matrix/matrix.hpp
*/

#pragma once

template <class T>
struct Matrix {
  vector<vector<T> > A;

  Matrix() = default;
  Matrix(int n, int m) : A(n, vector<T>(m, T())) {}
  Matrix(int n) : A(n, vector<T>(n, T())){};

  int H() const { return A.size(); }

  int W() const { return A[0].size(); }

  int size() const { return A.size(); }

  inline const vector<T> &operator[](int k) const { return A[k]; }

  inline vector<T> &operator[](int k) { return A[k]; }

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

  Matrix &operator+=(const Matrix &B) {
    int n = H(), m = W();
    assert(n == B.H() && m == B.W());
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) (*this)[i][j] += B[i][j];
    return (*this);
  }

  Matrix &operator-=(const Matrix &B) {
    int n = H(), m = W();
    assert(n == B.H() && m == B.W());
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) (*this)[i][j] -= B[i][j];
    return (*this);
  }

  Matrix &operator*=(const Matrix &B) {
    int n = H(), m = B.W(), p = W();
    assert(p == B.H());
    vector<vector<T> > C(n, vector<T>(m, T{}));
    for (int i = 0; i < n; i++)
      for (int k = 0; k < p; k++)
        for (int j = 0; j < m; j++) C[i][j] += (*this)[i][k] * B[k][j];
    A.swap(C);
    return (*this);
  }

  Matrix &operator^=(long long k) {
    Matrix B = Matrix::I(H());
    while (k > 0) {
      if (k & 1) B *= *this;
      *this *= *this;
      k >>= 1LL;
    }
    A.swap(B.A);
    return (*this);
  }

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

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

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

  Matrix operator^(const long long k) const { return (Matrix(*this) ^= k); }

  bool operator==(const Matrix &B) const {
    assert(H() == B.H() && W() == B.W());
    for (int i = 0; i < H(); i++)
      for (int j = 0; j < W(); j++)
        if (A[i][j] != B[i][j]) return false;
    return true;
  }

  bool operator!=(const Matrix &B) const {
    assert(H() == B.H() && W() == B.W());
    for (int i = 0; i < H(); i++)
      for (int j = 0; j < W(); j++)
        if (A[i][j] != B[i][j]) return true;
    return false;
  }

  friend ostream &operator<<(ostream &os, const Matrix &p) {
    int n = p.H(), m = p.W();
    for (int i = 0; i < n; i++) {
      os << (i ? "   " : "") << "[";
      for (int j = 0; j < m; j++) {
        os << p[i][j] << (j + 1 == m ? "]\n" : ",");
      }
    }
    return (os);
  }

  T determinant() const {
    Matrix B(*this);
    assert(H() == W());
    T ret = 1;
    for (int i = 0; i < H(); i++) {
      int idx = -1;
      for (int j = i; j < W(); j++) {
        if (B[j][i] != 0) {
          idx = j;
          break;
        }
      }
      if (idx == -1) return 0;
      if (i != idx) {
        ret *= T(-1);
        swap(B[i], B[idx]);
      }
      ret *= B[i][i];
      T inv = T(1) / B[i][i];
      for (int j = 0; j < W(); j++) {
        B[i][j] *= inv;
      }
      for (int j = i + 1; j < H(); j++) {
        T a = B[j][i];
        if (a == 0) continue;
        for (int k = i; k < W(); k++) {
          B[j][k] -= B[i][k] * a;
        }
      }
    }
    return ret;
  }
};

/**
 * @brief 行列ライブラリ
 */
long long N,M,K;

vector<vector<int>> tt;
void check(vector<int> t){
	rep(i,2){
		rep(j,N-1){
			if(t[i*N+j] == t[i*N+j+1])return;
		}
	}
	rep(i,N){
		if(t[i]==t[i+N])return;
	}
	tt.push_back(t);
}

void dfs(vector<int> &t,int cur){
	if(t.size()==N*2){
		check(t);
		return;
	}
	rep(i,cur){
		t.push_back(i);
		dfs(t,cur);
		t.pop_back();
	}
	t.push_back(cur);
	cur++;
	dfs(t,cur);
	t.pop_back();
}

vector<int> trans(vector<int> t){
	map<int,int> used;
	rep(i,t.size()){
		if(used.count(t[i])){
			t[i] = used[t[i]];
		}
		else{
			int tt = used.size();
			used[t[i]] = tt;
			t[i] = tt;
		}
	}
	
	return t;
}

struct combi{
	deque<mint> kaijou;
	deque<mint> kaijou_;
	
	combi(int n){
		kaijou.push_back(1);
		for(int i=1;i<=n;i++){
			kaijou.push_back(kaijou[i-1]*i);
		}
		
		mint b=kaijou[n].inv();
		
		kaijou_.push_front(b);
		for(int i=1;i<=n;i++){
			int k=n+1-i;
			kaijou_.push_front(kaijou_[0]*k);
		}
	}
	
	mint combination(int n,int r){
		if(r>n)return 0;
		mint a = kaijou[n]*kaijou_[r];
		a *= kaijou_[n-r];
		return a;
	}
	
	mint junretsu(int a,int b){
		mint x = kaijou_[a]*kaijou_[b];
		x *= kaijou[a+b];
		return x;
	}
	
	mint catalan(int n){
		return combination(2*n,n)/(n+1);
	}
	
};

int main(){
	
	
	cin>>N>>M>>K;
	
	{
		vector<int> t;
		
		dfs(t,0);
	}
	
	sort(tt.begin(),tt.end());

	vector<vector<int>> t;
	
	rep(i,tt.size()){
		vector<int> temp;
		rep(j,N){
			temp.push_back(tt[i][j]);
		}
		t.push_back(temp);
	}
	sort(t.begin(),t.end());
	t.erase(unique(t.begin(),t.end()),t.end());

	//cout<<tt.size()<<endl;
	mint ans= 0;
	combi C(400000);
	
	vector<int> A(tt.size()),B(tt.size());
	vector<vector<int>> Minus(tt.size());
	
	rep(j,tt.size()){
		int cnt = 0;
		vector<bool> f(N*2,false);
		vector<int> x,y;
		rep(k,tt[j].size()){
			if(k<N){
				if(f[tt[j][k]]){
					continue;
				}
				else{
					cnt++;
					f[tt[j][k]] = true;
				}
				x.push_back(tt[j][k]);
			}
			else{
				if(f[tt[j][k]]){
					
				}
				else{
					Minus[j].push_back(cnt);
					cnt++;
					f[tt[j][k]] = true;
				}
				y.push_back(tt[j][k]);
			}
		}
		y = trans(y);
		int d0 = distance(t.begin(),lower_bound(t.begin(),t.end(),x));
		int d1 = distance(t.begin(),lower_bound(t.begin(),t.end(),y));
		A[j]= d0;
		B[j] = d1;
	}		
	
	vector<vector<int>> Minus2(t.size());
	rep(j,t.size()){
		int cnt = 0;
		vector<bool> f(N,false);
		rep(k,t[j].size()){
			if(f[t[j][k]]){
				continue;
			}
			else{
				Minus2[j].push_back(cnt);
				cnt++;;
				f[t[j][k]] = true;
			}
		}
		
	}
	
	rep(i,K){
		Matrix<mint> mat(t.size(),t.size());;
		//matrix<mint,op0,e0,op1,e1> mat(t.size(),t.size());
		rep(j,tt.size()){
			mint v = 1;
			
			rep(k,Minus[j].size()){
				v *= K-i-Minus[j][k];
			}
			
			mat[B[j]][A[j]] += v;
		}		
		mat ^= M-1;
		Matrix<mint> mat2(t.size(),1);
		
		rep(j,t.size()){
			mint v = 1;
			rep(k,Minus2[j].size()){
				v *= K-i-Minus2[j][k];
			}
			mat2[j][0] = v;
		}
		mat2 = mat * mat2;
		
		mint sum = 0;
		rep(j,t.size()){
			sum += mat2[j][0];
		}

		sum *= C.combination(K,i);
		if(i%2==1)sum *= -1;
		ans += sum;
		
	}
	cout<<ans.val()<<endl;
	
	
	
	return 0;
}
0