結果

問題 No.1978 Permutation Repetition
ユーザー SSRSSSRS
提出日時 2022-06-10 23:04:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,407 bytes
コンパイル時間 2,390 ms
コンパイル使用メモリ 211,884 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 05:39:35
合計ジャッジ時間 4,058 ms
ジャッジサーバーID
(参考情報)
judge9 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
testcase_05 AC 2 ms
4,348 KB
testcase_06 WA -
testcase_07 WA -
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 WA -
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 WA -
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 WA -
testcase_20 AC 2 ms
4,348 KB
testcase_21 WA -
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 2 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 3 ms
4,348 KB
testcase_42 WA -
testcase_43 WA -
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){
      if (gcd(M / gcd(M, cnt[i]), i) != 1){
        ans = 0;
      } else {
        int T = gcd(M, cnt[i]);
        vector<long long> dp(cnt[i] + 1, 0);
        dp[0] = 1;
        for (int j = 1; j <= T; j++){
          if (T % 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