結果

問題 No.329 全射
ユーザー koyumeishikoyumeishi
提出日時 2015-12-22 12:46:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 219 ms / 2,000 ms
コード長 3,138 bytes
コンパイル時間 1,043 ms
コンパイル使用メモリ 105,828 KB
実行使用メモリ 11,372 KB
最終ジャッジ日時 2023-10-18 22:35:10
合計ジャッジ時間 4,082 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
11,108 KB
testcase_01 AC 9 ms
11,108 KB
testcase_02 AC 10 ms
11,108 KB
testcase_03 AC 9 ms
11,108 KB
testcase_04 AC 9 ms
11,108 KB
testcase_05 AC 10 ms
11,108 KB
testcase_06 AC 9 ms
11,108 KB
testcase_07 AC 9 ms
11,108 KB
testcase_08 AC 10 ms
11,108 KB
testcase_09 AC 9 ms
11,108 KB
testcase_10 AC 9 ms
11,108 KB
testcase_11 AC 9 ms
11,108 KB
testcase_12 AC 9 ms
11,108 KB
testcase_13 AC 187 ms
11,372 KB
testcase_14 AC 80 ms
11,372 KB
testcase_15 AC 85 ms
11,372 KB
testcase_16 AC 50 ms
11,372 KB
testcase_17 AC 128 ms
11,372 KB
testcase_18 AC 134 ms
11,372 KB
testcase_19 AC 219 ms
11,372 KB
testcase_20 AC 174 ms
11,372 KB
testcase_21 AC 133 ms
11,372 KB
testcase_22 AC 207 ms
11,372 KB
testcase_23 AC 17 ms
11,372 KB
testcase_24 AC 19 ms
11,372 KB
testcase_25 AC 40 ms
11,372 KB
testcase_26 AC 29 ms
11,372 KB
testcase_27 AC 11 ms
11,108 KB
testcase_28 AC 9 ms
11,108 KB
testcase_29 AC 11 ms
11,108 KB
testcase_30 AC 10 ms
11,108 KB
testcase_31 AC 9 ms
11,108 KB
testcase_32 AC 11 ms
11,108 KB
testcase_33 AC 9 ms
11,108 KB
testcase_34 AC 9 ms
11,108 KB
testcase_35 AC 9 ms
11,108 KB
testcase_36 AC 10 ms
11,108 KB
testcase_37 AC 10 ms
11,108 KB
testcase_38 AC 10 ms
11,108 KB
testcase_39 AC 10 ms
11,108 KB
testcase_40 AC 10 ms
11,108 KB
testcase_41 AC 9 ms
11,108 KB
testcase_42 AC 10 ms
11,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <functional>
#include <set>
#include <ctime>
#include <random>
using namespace std;
template<class T> istream& operator >> (istream& is, vector<T>& vec){for(T& val: vec) is >> val; return is;}
template<class T> istream& operator , (istream& is, T& val){ return is >> val;}
template<class T> ostream& operator << (ostream& os, vector<T>& vec){for(int i=0; i<vec.size(); i++) os << vec[i] << (i==vec.size()-1?"\n":" ");return os;}

#define MOD 1000000007

class combination_mod{
	const long long mod;
	const long long size;
	
	vector<long long> fact;	//n!
	vector<long long> fact_inv;	// (n!)^-1

	void make_fact(){
		fact[0] = 1;
		for(long long i=1; i<size; i++){
			fact[i] = fact[i-1]*i % mod;
		}
	}

	void make_fact_inv(){
		fact_inv[0] = fact_inv[1] = 1;
		for(long long i=2; i<size; i++){
			fact_inv[i] = fact_inv[mod%i] * (mod - mod/i) % mod;	// x ^ -1
		}
		for(int i=2; i<size; i++){
			fact_inv[i] = fact_inv[i-1] * fact_inv[i] % mod;	// x! ^ -1
		}
	}

public:
	combination_mod(long long mod_, long long size_ = 2000000) : mod(mod_), size(size_+1){
		fact.resize(size);
		fact_inv.resize(size);
		make_fact();
		make_fact_inv();
	}

	//nCk mod p O(1)
	long long comb(long long n, long long k){
		if(k==0 || n==k) return 1;
		long long ret = fact[n] * fact_inv[k] % mod * fact_inv[n-k] % mod;
		return ret;
	}

	//n! mod p O(1)
	long long factorial(long long n){
		return fact[n];
	}
};

long long mod_pow(long long x, long long y/*, long long MOD*/){	//x^y mod MOD
	long long ret=1LL;
	while(y>0LL){
		if(y&1LL) ret = (ret * x) % MOD;
		x = (x*x) % MOD;
		y >>= 1LL;
	}
	return ret;
}

#include <cassert>

int main(){
	int n,m;
	cin >> n,m;
	vector<int> w(n);
	cin >> w;
	vector<vector<int>> G(n);
	for(int i=0; i<m; i++){
		int x,y;
		cin >> x,y;
		x--; y--;
		//if(w[x] >= w[y])
		{
			G[x].push_back(y);
		}
	}

	const int len = 1010;

	combination_mod cmb(MOD, len);
/*
	vector<vector<long long>> dp(len, vector<long long>(len, 0));
	for(int i=1; i<len; i++){
		for(int j=1; j<=i; j++){
			long long tmp = 0;
			for(int k=1; k<j; k++){
				tmp = tmp + dp[i][k] * cmb.comb(j,k);
				tmp %= MOD;
			}
			dp[i][j] = (mod_pow(j,i) - tmp + MOD)%MOD;
		}
	}
*/

	vector<vector<long long>> Stirling2(len, vector<long long>(len, 0));
	Stirling2[0][0] = 1;
	for(int i=1; i<len; i++){
		for(int j=1; j<=i; j++){
			Stirling2[i][j] = (Stirling2[i-1][j-1] + j*Stirling2[i-1][j]) % MOD;
		}
	}
	

	long long ans=0;
	for(int i=0; i<n; i++){
		vector<int> sz(n, 0);
		sz[i] = w[i];

		priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
		pq.push({w[i], i});
		
		while(pq.size()){
			auto p = pq.top();
			pq.pop();

			for(int next : G[p.second] ){
				int s = min(p.first, w[next]);
				if(sz[next] >= s) continue;
				sz[next] = s;
				pq.push({s,next});
			}
		}

		for(int j=0; j<n; j++){
			if(sz[j]!=w[j]) continue;
			ans = (ans + Stirling2[w[i]][sz[j]] * cmb.factorial(sz[j]))%MOD;
		}
	}

	cout << ans << endl;


	return 0;
}
0