結果

問題 No.1978 Permutation Repetition
ユーザー KudeKude
提出日時 2022-06-11 01:21:43
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,614 bytes
コンパイル時間 2,689 ms
コンパイル使用メモリ 237,208 KB
実行使用メモリ 11,428 KB
最終ジャッジ日時 2023-10-21 07:28:29
合計ジャッジ時間 4,865 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
11,404 KB
testcase_01 AC 14 ms
11,404 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 14 ms
11,412 KB
testcase_23 AC 14 ms
11,412 KB
testcase_24 WA -
testcase_25 AC 14 ms
11,412 KB
testcase_26 AC 14 ms
11,412 KB
testcase_27 WA -
testcase_28 AC 14 ms
11,416 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 14 ms
11,412 KB
testcase_33 AC 14 ms
11,412 KB
testcase_34 AC 14 ms
11,412 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 14 ms
11,408 KB
testcase_40 AC 14 ms
11,412 KB
testcase_41 AC 14 ms
11,416 KB
testcase_42 AC 14 ms
11,428 KB
testcase_43 AC 14 ms
11,428 KB
testcase_44 AC 14 ms
11,404 KB
testcase_45 AC 14 ms
11,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic pop
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint1000000007;

VI cycle_decomposition(const vector<int>& a) {
  const int n = a.size();
  vector<char> visited(n);
  VI res;
  for(int i = 0; i < n; i++) if (!visited[i]) {
    int cnt = 0;
    int v = i;
    do {
      visited[v] = true;
      cnt++;
      v = a[v];
    } while (v != i);
    res.emplace_back(cnt);
  }
  return res;
}


constexpr int FACT_SIZE = 1000000;
mint Fact[FACT_SIZE + 1];
mint iFact[FACT_SIZE + 1];
const auto fact_init = [] {
    Fact[0] = mint::raw(1);
    for(int i = 1; i <= FACT_SIZE; ++i) {
        Fact[i] = Fact[i-1] * i;
    }
    iFact[FACT_SIZE] = Fact[FACT_SIZE].inv();
    for(int i = FACT_SIZE; i; --i) {
        iFact[i-1] = iFact[i] * i;
    }
    return false;
}();

mint comb(int n, int k) {
    if (k == 0) return mint::raw(1);
    assert(n >= 0 && k >= 0);
    if (k > n) return mint::raw(0);
    return Fact[n] * iFact[n - k] * iFact[k];
}

mint icomb(int n, int k) {
    return iFact[n] * Fact[n - k] * Fact[k];
}

mint fact(int n) {return Fact[n];}
mint perm(int n, int k) {
    assert(0 <= n);
    return Fact[n] * iFact[n - k];
}

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  VI a(n);
  rep(i, n) cin >> a[i], a[i]--;
  auto cs = cycle_decomposition(a);
  sort(all(cs));
  mint ans = 1;
  int sz = cs.size();
  for(int i = 0; i < sz;) {
    int j = i + 1;
    while(j < sz && cs[i] == cs[j]) j++;
    int cnt = j - i;
    int len = cs[i];
    vector<pair<int, mint>> d;
    for(int i = 1; i <= cnt; i++) if (m % (i * len) == i % (i * len)) {
      d.emplace_back(i, mint(len).pow(i - 1));
    }
    vector<mint> dp(cnt + 1);
    dp[0] = Fact[cnt];
    rep(i, cnt) {
      dp[i] /= cnt - i;
      for(auto [j, v]: d) {
        if (i + j > cnt) break;
        dp[i + j] += dp[i] * v;
      }
    }
    ans *= dp[cnt];
    i = j;
  }
  cout << ans.val() << '\n';
}
0