結果

問題 No.1978 Permutation Repetition
ユーザー magstamagsta
提出日時 2022-06-10 12:37:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 2,335 bytes
コンパイル時間 7,476 ms
コンパイル使用メモリ 279,200 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 04:38:54
合計ジャッジ時間 8,357 ms
ジャッジサーバーID
(参考情報)
judge9 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 3 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 3 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 3 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 3 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 3 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 9 ms
4,348 KB
testcase_43 AC 3 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>
#include "testlib.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 = 1020;

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;
	registerValidation();
	N = inf.readInt(1, 1000, "N");
	inf.readSpace();
	M = inf.readInt(1, 1000000000, "M");
	vector<long long> A(N);
	inf.readEoln();
	A[0] = inf.readInt(1, N, "A");
	for (int i = 1; i < N; i++) {
	    inf.readSpace();
	    A[i] = inf.readInt(1, N, "A");
	}
	inf.readEoln();
	inf.readEof();
	
	vector<bool> flagtest(N, false);
	for (int i = 0; i < N; i++){
	    if (flagtest[A[i] - 1]) cout << -1 << endl;
	    flagtest[A[i] - 1] = true;
	}

	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