結果

問題 No.1815 K色問題
ユーザー 沙耶花沙耶花
提出日時 2022-01-19 22:59:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,380 bytes
コンパイル時間 5,809 ms
コンパイル使用メモリ 298,548 KB
実行使用メモリ 13,460 KB
最終ジャッジ日時 2024-03-13 11:30:08
合計ジャッジ時間 11,976 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.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

template <class S,
          S (*op0)(S, S),
          S (*e0)(),
		  S (*op1)(S, S),
          S (*e1)()
          >
struct matrix{
	vector<vector<S>> v;
	int _h,_w;
	
	matrix(vector<vector<S>> X){
		v = X;
		_h = X.size();
		_w = 0;
		if(X.size()>0)_w = X[0].size();
	}
	
	matrix(int h,int w){
		v.resize(h,vector<S>(w,e0()));
		_h = h;
		_w = w;
	}
	
	void add_element(int from,int to,S x){
		v[to][from] = op0(v[to][from],x);
	}
	
	matrix e(){
		assert(_h==_w);
		matrix<S,op0,e0,op1,e1> ret(_h,_w);
		for(int i=0;i<_h;i++){
			for(int j=0;j<_w;j++){
				if(i==j)ret.v[i][j] = e1();
				else ret.v[i][j] = e0();
			}
		}
		return ret;
	}
	
	matrix &operator*=(const matrix &another){
		matrix<S,op0,e0,op1,e1> ret(_h,another._w);
		for(int i=0;i<_h;i++){
			for(int j=0;j<another._w;j++){
				ret.v[i][j] = e0();
				for(int k=0;k<_w;k++){
					ret.v[i][j] = op0(ret.v[i][j],op1(v[i][k],another.v[k][j]));
				}
			}
		}

		v = ret.v;
		return (*this);
	}
	
	matrix operator*(const matrix &another)const{
		return (matrix(*this)*=another);
	}
	
	matrix pow(long long cnt){
		matrix<S,op0,e0,op1,e1> ret = e();
		auto temp = *this;
		while(cnt!=0LL){
			if((cnt&1)==1){
				ret *= temp;
			}
			temp *= temp;
			cnt>>=1;
		}
		return ret;
	}
};

mint op0(mint a,mint b){
	return a+b;
}

mint e0(){
	return 0;
}

mint op1(mint a,mint b){
	return a*b;
}

mint e1(){
	return 1;
}

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);
	
	rep(i,K){
		matrix<mint,op0,e0,op1,e1> mat(t.size(),t.size());
		rep(j,tt.size()){
			mint remain = K-i;
			vector<bool> f(N*2,false);
			mint v = 1;
			vector<int> x,y;
			rep(k,tt[j].size()){
				if(k<N){
					if(f[tt[j][k]]){
						continue;
					}
					else{
						remain--;
						f[tt[j][k]] = true;
					}
					x.push_back(tt[j][k]);
				}
				else{
					if(f[tt[j][k]]){
						
					}
					else{
						v *= remain;
						remain--;
						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));
			mat.add_element(d0,d1,v);
		}		
		mat = mat.pow(M-1);
		matrix<mint,op0,e0,op1,e1> mat2(t.size(),1);
		rep(j,t.size()){
			mint remain = K-i;
			mint v = 1;
			vector<bool> f(N,false);
			rep(k,t[j].size()){
				if(f[t[j][k]]){
					continue;
				}
				else{
					v *= remain;
					remain--;
					f[t[j][k]] = true;
				}
			}
			mat2.v[j][0] = v;
		}
		mat2 = mat * mat2;
		
		mint sum = 0;
		rep(j,t.size()){
			sum += mat2.v[j][0];
		}

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