結果

問題 No.329 全射
ユーザー asdasd
提出日時 2016-02-27 00:40:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 3,216 bytes
コンパイル時間 1,112 ms
コンパイル使用メモリ 108,296 KB
実行使用メモリ 12,544 KB
最終ジャッジ日時 2023-10-24 18:05:12
合計ジャッジ時間 3,180 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
12,344 KB
testcase_01 AC 6 ms
12,344 KB
testcase_02 AC 5 ms
12,344 KB
testcase_03 AC 5 ms
12,344 KB
testcase_04 AC 6 ms
12,344 KB
testcase_05 AC 6 ms
12,344 KB
testcase_06 AC 5 ms
12,344 KB
testcase_07 AC 5 ms
12,344 KB
testcase_08 AC 6 ms
12,344 KB
testcase_09 AC 6 ms
12,344 KB
testcase_10 AC 6 ms
12,344 KB
testcase_11 AC 5 ms
12,344 KB
testcase_12 AC 6 ms
12,344 KB
testcase_13 AC 25 ms
12,468 KB
testcase_14 AC 14 ms
12,388 KB
testcase_15 AC 16 ms
12,444 KB
testcase_16 AC 20 ms
12,544 KB
testcase_17 AC 19 ms
12,456 KB
testcase_18 AC 25 ms
12,544 KB
testcase_19 AC 26 ms
12,472 KB
testcase_20 AC 24 ms
12,464 KB
testcase_21 AC 21 ms
12,456 KB
testcase_22 AC 23 ms
12,420 KB
testcase_23 AC 11 ms
12,416 KB
testcase_24 AC 11 ms
12,412 KB
testcase_25 AC 15 ms
12,440 KB
testcase_26 AC 11 ms
12,396 KB
testcase_27 AC 7 ms
12,364 KB
testcase_28 AC 6 ms
12,348 KB
testcase_29 AC 6 ms
12,356 KB
testcase_30 AC 7 ms
12,356 KB
testcase_31 AC 6 ms
12,344 KB
testcase_32 AC 6 ms
12,360 KB
testcase_33 AC 11 ms
12,352 KB
testcase_34 AC 11 ms
12,352 KB
testcase_35 AC 14 ms
12,352 KB
testcase_36 AC 12 ms
12,352 KB
testcase_37 AC 11 ms
12,352 KB
testcase_38 AC 12 ms
12,352 KB
testcase_39 AC 12 ms
12,352 KB
testcase_40 AC 14 ms
12,352 KB
testcase_41 AC 11 ms
12,352 KB
testcase_42 AC 14 ms
12,352 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:119:42: warning: iteration 1009 invokes undefined behavior [-Waggressive-loop-optimizations]
  119 |   rep(i, MATH_COMBINATION_MAX) fact[i+1] = fact[i] * (i+1) % MOD;
      |                                ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:24:33: note: within this loop
   24 | #define REP(i,a,b) for(int i=a;i<(int)b;i++)
      |                                 ^
main.cpp:25:18: note: in expansion of macro ‘REP’
   25 | #define rep(i,n) REP(i,0,n)
      |                  ^~~
main.cpp:119:3: note: in expansion of macro ‘rep’
  119 |   rep(i, MATH_COMBINATION_MAX) fact[i+1] = fact[i] * (i+1) % MOD;
      |   ^~~

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>
#include <bitset>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define minimize(a, x) a = std::min(a, x)
#define maximize(a, x) a = std::max(a, x)

constexpr int inf = 1<<29;

int const MOD = 1e9+7;
typedef long long ll;


constexpr int MATH_COMBINATION_MAX = 1010;

/***********************************************************************************/
#define DEF_MAX_BOUND(assignment) enum { assignment };
#define MEMO_BEGIN(Identifier)  \
namespace memorized { namespace Identifier {  \
  bool __computed = false; inline bool computed() {if(__computed) { return true; } else { __computed = true; return false; }}
#define MEMO_END }}

#define USE_MEMO(Identifier)        using memorized::Identifier::
#define COMPUTED_RESULT(Identifier) if(memorized::Identifier::computed()) return
#define USE_CONSTANT(Identifier)    using memorized::Identifier::
/***********************************************************************************/


/*
  スターリング数
  異なるN個をちょうどKグループに分けるときの方法の数
  高々Kグループに分ける場合は、グループi(1<=i<=K)をN回選ぶ方法の数となり、これは単なる重複順列でK^Nとなる
  Verified: http://yukicoder.me/problems/663
 */

namespace math { namespace combination {

MEMO_BEGIN(stirling_number)
  DEF_MAX_BOUND(max_value = MATH_COMBINATION_MAX)
  ll dp[max_value][max_value];
MEMO_END

int stirling_number(int N, int K) {
  USE_MEMO(stirling_number)         dp;
  COMPUTED_RESULT(stirling_number)  dp[N][K];

  USE_CONSTANT(stirling_number) max_value;

  REP(i, 1, max_value) {  // undefined when i = 0
    dp[i][1] = dp[i][i] = 1;
    REP(j, 2, i) {
      dp[i][j] = dp[i-1][j-1] + j * dp[i-1][j];
      dp[i][j] %= MOD;
    }
  }

  return dp[N][K];
}

}}

int N, M;
vector<int> g[222];
ll fact[MATH_COMBINATION_MAX];
ll dp[300][300];

int main() {

  cin >> N >> M;

  vector<ll> ws(N);
  rep(i, N) {
    cin >> ws[i];
  }

  rep(i, M) {
    int a, b; cin >> a >> b; a--, b--;
    g[a].push_back(b);
  }

  zero(dp);
  rep(i, N) {
    dp[i][i] = ws[i];
    rep(j, g[i].size()) {
      int tar = g[i][j];
      dp[i][tar] = min(dp[i][i], ws[tar]);
    }
  }

  rep(k, N) rep(i, N) rep(j, N) {
    maximize(dp[i][j], min(dp[i][k], dp[k][j]));
  }

  fact[0] = 1;
  rep(i, MATH_COMBINATION_MAX) fact[i+1] = fact[i] * (i+1) % MOD;

  // 異なるN個を異なるK個に振り分ける方法の数(全射の数) := S(N, K) * K!
  using math::combination::stirling_number;

  ll ans = 0;

  rep(i, N) rep(j, N) {
    if(dp[i][j] < ws[j]) { continue; }
    ans += stirling_number(ws[i], ws[j]) * fact[ws[j]] % MOD;
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}
0