結果

問題 No.310 2文字しりとり
ユーザー 👑 kmjpkmjp
提出日時 2015-12-13 14:41:15
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,841 bytes
コンパイル時間 1,889 ms
コンパイル使用メモリ 149,096 KB
実行使用メモリ 197,508 KB
最終ジャッジ日時 2023-10-13 14:54:22
合計ジャッジ時間 9,860 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,632 KB
testcase_01 AC 2 ms
5,756 KB
testcase_02 AC 2 ms
5,500 KB
testcase_03 AC 2 ms
5,504 KB
testcase_04 AC 3 ms
5,576 KB
testcase_05 AC 2 ms
5,656 KB
testcase_06 AC 2 ms
5,600 KB
testcase_07 AC 2 ms
5,612 KB
testcase_08 AC 2 ms
5,544 KB
testcase_09 AC 2 ms
5,588 KB
testcase_10 AC 2 ms
5,500 KB
testcase_11 AC 2 ms
5,508 KB
testcase_12 AC 2 ms
5,640 KB
testcase_13 AC 2 ms
5,504 KB
testcase_14 AC 2 ms
5,528 KB
testcase_15 AC 3 ms
5,528 KB
testcase_16 AC 2 ms
5,712 KB
testcase_17 AC 2 ms
5,696 KB
testcase_18 AC 11 ms
10,032 KB
testcase_19 AC 5 ms
7,888 KB
testcase_20 AC 11 ms
9,980 KB
testcase_21 TLE -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------


int N,M;
int A[4040],B[4040];
int in[4040],out[4040];
int st,en;
ll fact[8080];
int self[4040];

ll mo=1000000007;
int id[4040],rid[4040];
int edge[4040][4040];
ll mat[4040][4040];

ll modpow(ll a, ll n = mo-2) {
	ll r=1;
	while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
	return r;
}

ll det_mo(int N) {
	int x,y,z;
	ll ret=1;
	FOR(y,N) FOR(z,N) mat[y][z]=((mat[y][z]%mo)+mo)%mo;
	FOR(x,N) {
		if(mat[x][x]==0) {
			for(y=x+1;y<N;y++) if(mat[y][x]) break;
			if(y==N) return 0;
			FOR(z,N) swap(mat[x][z],mat[y][z]);
		}
		ret=ret*mat[x][x]%mo;
		ll rev=modpow(mat[x][x]);
		FOR(z,N) mat[x][z]=rev*mat[x][z]%mo;
		for(y=x+1;y<N;y++) if(mat[y][x]) {
			rev=mat[y][x];
			for(z=x;z<N;z++) mat[y][z]=((mat[y][z]-mat[x][z]*rev)%mo+mo)%mo;
		}
	}
	return ret;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	fact[0]=1;
	FOR(i,8000) fact[i+1]=fact[i]*(i+1)%mo;
	
	cin>>N>>M;
	FOR(i,N) in[i]=out[i]=N-1;
	FOR(i,N) {
		FOR(j,N) edge[i][j]=-1;
		edge[i][i]=N-1;
		self[i]=1;
	}
	
	FOR(i,M) {
		cin>>A[i]>>B[i];
		A[i]--,B[i]--;
		
		if(A[i]==B[i]) {
			self[A[i]]=0;
		}
		else {
			out[A[i]]--;
			in[B[i]]--;
			edge[A[i]][B[i]]=0;
			edge[B[i]][B[i]]--;
		}
	}
	
	x=0;
	st=en=-1;
	
	FOR(i,N) {
		if(in[i]==0 && out[i]==0 && self[i]==0) id[i]=-1;
		else id[i]=x, rid[x]=i, x++;
		if(abs(in[i]-out[i])>1) return _P("0\n");
		if(in[i]==out[i]+1) {
			if(en!=-1) return _P("0\n");
			en=i;
		}
		if(out[i]==in[i]+1){
			if(st!=-1) return _P("0\n");
			st=i;
		}
	}
	if(x==0 || x==1) return _P("1\n");
	
	
	ll pat=1;
	N=x;
	if(st>=0) { // odd
		FOR(i,N) if(self[rid[i]]) pat = pat*max(in[rid[i]],out[rid[i]]) % mo;
		in[st]++;
		out[en]++;
		edge[en][st]=-1;
		edge[st][st]++;
	}
	else {
		ll tot=1;
		FOR(i,N) if(self[rid[i]]) tot = tot*in[rid[i]] % mo;
		pat=0;
		FOR(i,N) {
			if(self[rid[i]]) (pat += tot*(in[rid[i]]+1)%mo) %= mo;
			else (pat += tot*in[rid[i]]) %= mo;
		}
	}
	
	ll deg=1;
	FOR(i,N) deg = deg*fact[in[rid[i]]-1]%mo;
	
	FOR(i,N) FOR(j,N) mat[i][j]=edge[rid[i]][rid[j]];
	/*
	FOR(i,N) {
		FOR(j,N) _P("%lld ",mat[i][j]);
		_P("\n");
	}
	
	cout<<pat<<" "<<deg<<" "<<det_mo(N-1)<<endl;
	FOR(i,N) FOR(j,N) mat[i][j]=edge[rid[i]][rid[j]];
	*/
	cout<<(pat*deg%mo*det_mo(N-1)%mo)<<endl;
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0