結果

問題 No.1978 Permutation Repetition
ユーザー SSRSSSRS
提出日時 2022-06-10 23:31:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 199 ms / 2,000 ms
コード長 2,236 bytes
コンパイル時間 2,411 ms
コンパイル使用メモリ 211,920 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 05:49:33
合計ジャッジ時間 3,623 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
long long modpow(long long a, long long b){
	long long ans = 1;
	while (b > 0){
		if (b % 2 == 1){
			ans *= a;
			ans %= MOD;
		}
		a *= a;
		a %= MOD;
		b /= 2;
	}
	return ans;
}
long long modinv(long long a){
	return modpow(a, MOD - 2);
}
vector<long long> mf = {1};
vector<long long> mfi = {1};
long long modfact(int n){
	if (mf.size() > n){
		return mf[n];
	} else {
		for (int i = mf.size(); i <= n; i++){
			long long next = mf.back() * i % MOD;
			mf.push_back(next);
			mfi.push_back(modinv(next));
		}
		return mf[n];
	}
}
long long modfactinv(int n){
	if (mfi.size() > n){
		return mfi[n];
	} else {
		modfact(n);
		return mfi[n];
	}
}
int main(){
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
    A[i]--;
  }
  vector<bool> used(N, false);
  vector<int> cnt(N + 1, 0);
  for (int i = 0; i < N; i++){
    if (!used[i]){
      used[i] = true;
      int c = 0;
      int v = i;
      while (true){
        c++;
        v = A[v];
        if (used[v]){
          break;
        }
        used[v] = true;
      }
      cnt[c]++;
    }
  }
  long long ans = 1;
  for (int i = 1; i <= N; i++){
    if (cnt[i] > 0){
      vector<long long> dp(cnt[i] + 1, 0);
      dp[0] = 1;
      for (int j = 1; j <= cnt[i]; j++){
        if (M % j == 0 && gcd(M / j, i) == 1){
          vector<long long> dp2(cnt[i] + 1, 0);
          for (int k = 0; k <= cnt[i]; k++){
            for (int l = k; l <= cnt[i]; l += j){
              int c = (l - k) / j;
              long long add = dp[k];
              add *= modfact(cnt[i] - k);
              add %= MOD;
              add *= modpow(modfactinv(j), c);
              add %= MOD;
              add *= modfactinv(cnt[i] - l);
              add %= MOD;
              add *= modfactinv(c);
              add %= MOD;
              add *= modpow(modfact(j - 1), c);
              add %= MOD;
              add *= modpow(i, (j - 1) * c);
              add %= MOD;
              dp2[l] += add;
              dp2[l] %= MOD;
            }
          }
          swap(dp, dp2);
        }
      }
      ans *= dp[cnt[i]];
      ans %= MOD;
    }
  }
  cout << ans << endl;
}
0