結果

問題 No.329 全射
ユーザー minamiminami
提出日時 2019-03-30 10:32:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 251 ms / 2,000 ms
コード長 4,020 bytes
コンパイル時間 1,973 ms
コンパイル使用メモリ 179,112 KB
実行使用メモリ 7,552 KB
最終ジャッジ日時 2024-11-08 04:10:28
合計ジャッジ時間 6,316 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 1 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 2 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 229 ms
7,552 KB
testcase_14 AC 94 ms
7,296 KB
testcase_15 AC 121 ms
7,424 KB
testcase_16 AC 130 ms
7,552 KB
testcase_17 AC 168 ms
7,424 KB
testcase_18 AC 210 ms
7,552 KB
testcase_19 AC 251 ms
7,552 KB
testcase_20 AC 218 ms
7,552 KB
testcase_21 AC 174 ms
7,424 KB
testcase_22 AC 207 ms
7,552 KB
testcase_23 AC 36 ms
7,040 KB
testcase_24 AC 37 ms
7,040 KB
testcase_25 AC 99 ms
7,168 KB
testcase_26 AC 50 ms
7,040 KB
testcase_27 AC 11 ms
6,912 KB
testcase_28 AC 6 ms
6,144 KB
testcase_29 AC 9 ms
7,040 KB
testcase_30 AC 7 ms
6,144 KB
testcase_31 AC 3 ms
5,248 KB
testcase_32 AC 10 ms
6,656 KB
testcase_33 AC 66 ms
7,168 KB
testcase_34 AC 58 ms
7,040 KB
testcase_35 AC 83 ms
7,168 KB
testcase_36 AC 72 ms
7,040 KB
testcase_37 AC 55 ms
7,040 KB
testcase_38 AC 66 ms
7,168 KB
testcase_39 AC 67 ms
7,040 KB
testcase_40 AC 87 ms
7,040 KB
testcase_41 AC 61 ms
7,040 KB
testcase_42 AC 87 ms
7,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1'000'000'007;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

template<int MOD>
struct ModInt {
	static const int kMod = MOD;
	unsigned x;
	ModInt() :x(0) {}
	ModInt(signed x_) { x_ %= MOD; if (x_ < 0)x_ += MOD; x = x_; }
	ModInt(signed long long x_) { x_ %= MOD; if (x_ < 0)x_ += MOD; x = x_; }
	int get()const { return (int)x; }
	ModInt &operator+=(ModInt m) { if ((x += m.x) >= MOD)x -= MOD; return *this; }
	ModInt &operator-=(ModInt m) { if ((x += MOD - m.x) >= MOD)x -= MOD; return *this; }
	ModInt &operator*=(ModInt m) { x = (unsigned long long)x*m.x%MOD; return *this; }
	ModInt &operator/=(ModInt m) { return *this *= m.inverse(); }
	ModInt operator+(ModInt m)const { return ModInt(*this) += m; }
	ModInt operator-(ModInt m)const { return ModInt(*this) -= m; }
	ModInt operator*(ModInt m)const { return ModInt(*this) *= m; }
	ModInt operator/(ModInt m)const { return ModInt(*this) /= m; }
	ModInt operator-()const { return ModInt(kMod - x); }
	bool operator==(ModInt m)const { return x == m.x; }
	bool operator!=(ModInt m)const { return x != m.x; }
	ModInt inverse()const {
		signed a = x, b = MOD, u = 1, v = 0;
		while (b) {
			signed t = a / b;
			a -= t * b; swap(a, b);
			u -= t * v; swap(u, v);
		}
		if (u < 0)u += MOD;
		return ModInt(u);
	}
};
template<int MOD>
ostream &operator<<(ostream &os, const ModInt<MOD> &m) { return os << m.x; }
template<int MOD>
istream &operator>>(istream &is, ModInt<MOD> &m) { signed long long s; is >> s; m = ModInt<MOD>(s); return is; };

using mint = ModInt<MOD>;

template<int MOD>
ModInt<MOD> pow(ModInt<MOD> a, unsigned long long k) {
	ModInt<MOD> r = 1;
	while (k) {
		if (k & 1)r *= a;
		a *= a;
		k >>= 1;
	}
	return r;
}

// n < 10^7
// 前計算 O(n)
// 計算 O(1)
vector<mint> fact, factinv;
void precomputeFact(int n) {
	n = min(n, mint::kMod - 1); //  N >= kMod  =>  N! = 0 (mod kMod)
	fact.resize(n + 1); factinv.resize(n + 1);
	fact[0] = 1;
	for (int i = 1; i < n + 1; i++)
		fact[i] = fact[i - 1] * i;
	factinv[n] = fact[n].inverse();
	for (int i = n; i >= 1; i--)
		factinv[i - 1] = factinv[i] * i; // ((i-1)!)^(-1) = (i!)^(-1) * i
}
mint binom(int n, int r) {
	if (n >= mint::kMod)
		return binom(n % mint::kMod, r % mint::kMod) * binom(n / mint::kMod, r / mint::kMod);
	return r > n ? 0 : fact[n] * factinv[n - r] * factinv[r];
}

// スターリング数
vector<vector<mint>> stirlingNumbers(int n) {
	vector<vector<mint>> S(n + 1, vector<mint>(n + 1, 0));
	S[0][0] = 1;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j <= i; j++) {
			S[i + 1][j] += S[i][j] * j;
			S[i + 1][j + 1] += S[i][j];
		}
	}
	return S;
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, M; cin >> N >> M;

	vector<int> w(N); rep(i, 0, N) {
		cin >> w[i];
	}
	vector<int> I(M), J(M); rep(i, 0, M) {
		cin >> I[i] >> J[i];
		I[i]--, J[i]--;
	}


	vector<vector<int>> wf(N, vector<int>(N, INF));
	rep(i, 0, N)wf[i][i] = 0;

	rep(i, 0, M) {
		int s = I[i], d = J[i], w = 1;
		wf[s][d] = min(wf[s][d], w); // 有向
	}

	int L = ceil(log2(N));
	rep(l, 0, L)rep(k, 0, N)rep(i, 0, N)rep(j, 0, N) {
		if (wf[i][k] != INF && wf[k][j] != INF && w[i] >= w[j] && w[k] >= w[j])
			wf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j]);
	}

	dump(wf);

	int maxi = *max_element(all(w));
	precomputeFact(maxi);

	auto S = stirlingNumbers(maxi);

	mint ans = 0;
	rep(i, 0, N)rep(j, 0, N) {
		if (w[i] >= w[j]) {
			if (wf[i][j] == INF)continue;
			mint s = fact[w[j]] * S[w[i]][w[j]];
			dump(i, j, s);
			ans += s;
		}
	}
	cout << ans << endl;
	return 0;
}
0