結果

問題 No.1978 Permutation Repetition
ユーザー magstamagsta
提出日時 2022-04-23 22:03:36
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,556 bytes
コンパイル時間 1,750 ms
コンパイル使用メモリ 175,872 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 04:38:07
合計ジャッジ時間 3,621 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 WA -
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 3 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 3 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 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;

#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);
}

long long fac[1010];

void FACinit(long long mod) {
	fac[0] = fac[1] = 1;
	for (int i = 2; i < 1010; i++) {
		fac[i] = fac[i - 1] * i % 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() {
	FACinit(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] * 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