結果

問題 No.1978 Permutation Repetition
ユーザー magstamagsta
提出日時 2022-04-23 22:21:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 122 ms / 2,000 ms
コード長 1,939 bytes
コンパイル時間 1,851 ms
コンパイル使用メモリ 176,056 KB
実行使用メモリ 27,084 KB
最終ジャッジ日時 2023-10-21 04:38:16
合計ジャッジ時間 8,556 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
27,020 KB
testcase_01 AC 111 ms
27,020 KB
testcase_02 AC 108 ms
27,020 KB
testcase_03 AC 108 ms
27,020 KB
testcase_04 AC 107 ms
27,036 KB
testcase_05 AC 107 ms
27,044 KB
testcase_06 AC 111 ms
27,072 KB
testcase_07 AC 114 ms
27,036 KB
testcase_08 AC 122 ms
27,084 KB
testcase_09 AC 110 ms
27,068 KB
testcase_10 AC 112 ms
27,068 KB
testcase_11 AC 112 ms
27,060 KB
testcase_12 AC 109 ms
27,024 KB
testcase_13 AC 112 ms
27,052 KB
testcase_14 AC 113 ms
27,076 KB
testcase_15 AC 110 ms
27,056 KB
testcase_16 AC 113 ms
27,028 KB
testcase_17 AC 114 ms
27,036 KB
testcase_18 AC 118 ms
27,080 KB
testcase_19 AC 113 ms
27,052 KB
testcase_20 AC 110 ms
27,020 KB
testcase_21 AC 119 ms
27,044 KB
testcase_22 AC 119 ms
27,064 KB
testcase_23 AC 112 ms
27,052 KB
testcase_24 AC 116 ms
27,052 KB
testcase_25 AC 113 ms
27,056 KB
testcase_26 AC 116 ms
27,048 KB
testcase_27 AC 117 ms
27,068 KB
testcase_28 AC 117 ms
27,068 KB
testcase_29 AC 119 ms
27,064 KB
testcase_30 AC 114 ms
27,072 KB
testcase_31 AC 108 ms
27,052 KB
testcase_32 AC 116 ms
27,060 KB
testcase_33 AC 114 ms
27,060 KB
testcase_34 AC 115 ms
27,036 KB
testcase_35 AC 122 ms
27,068 KB
testcase_36 AC 119 ms
27,048 KB
testcase_37 AC 110 ms
27,036 KB
testcase_38 AC 116 ms
27,048 KB
testcase_39 AC 112 ms
27,020 KB
testcase_40 AC 110 ms
27,036 KB
testcase_41 AC 114 ms
27,068 KB
testcase_42 AC 121 ms
27,076 KB
testcase_43 AC 114 ms
27,076 KB
testcase_44 AC 114 ms
27,016 KB
testcase_45 AC 118 ms
27,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define MOD 1000000007

long long gcd(long long x, long long y) {
	if (x < y) swap(x, y);
	while (y > 0) {
		long long r = x % y;
		x = y;
		y = r;
	}
	return x;
}

long long lcm(long long x, long long y) {
	return x * y / gcd(x, y);
}

const int MAX = 1000000;

long long fac[MAX], finv[MAX], inv[MAX];

void COMinit(long long mod) {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % mod;
		inv[i] = mod - inv[mod % i] * (mod / i) % mod;
		finv[i] = finv[i - 1] * inv[i] % mod;
	}
}

long long COM(long long n, long long k, long long mod) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

long long modpow(long long a, long long n, long long mod) {
	long long res = 1;
	while (n > 0) {
		if (n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

int main() {
	COMinit(MOD);
	long long N, M; cin >> N >> M;
	vector<long long> A(N);
	for (int i = 0; i < N; i++) cin >> A[i];

	vector<long long> chikan(N + 1);
	vector<bool> flag(N, false);
	for (int i = 0; i < N; i++) {
		if (flag[i]) continue;
		int i_ = A[i] - 1;
		flag[i_] = true;
		int count = 1;
		while (i != i_) {
			i_ = A[i_] - 1;
			count++;
			flag[i_] = true;
		}
		chikan[count]++;
	}

	vector<vector<long long>> jun(N + 1);

	for (int i = 1; i <= N; i++) {
		jun[i / gcd(i, M)].push_back(i);
	}

	long long ans = 1;

	for (int i = 1; i <= N; i++) {
		vector<long long> dp(chikan[i] + 1, 0);
		dp[0] = 1;
		for (int j = 1; j <= chikan[i]; j++) {
			for (int k = 0; k < jun[i].size(); k++) {
				if (j < jun[i][k] / i) continue;
				dp[j] += ((dp[j - jun[i][k] / i] * COM(j - 1, jun[i][k] / i - 1, MOD) % MOD) * fac[jun[i][k] / i - 1] % MOD) * modpow(i, jun[i][k] / i - 1, MOD) % MOD;
			}
			dp[j] %= MOD;
		}
		ans = ans * dp[chikan[i]] % MOD;
	}
	
	cout << ans << endl;
}
0