結果

問題 No.1428 PeRmutation Question
ユーザー SSRSSSRS
提出日時 2021-03-12 22:59:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,720 bytes
コンパイル時間 2,304 ms
コンパイル使用メモリ 176,844 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-22 14:27:26
合計ジャッジ時間 4,145 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 WA -
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 10 ms
5,376 KB
testcase_13 AC 38 ms
5,564 KB
testcase_14 AC 33 ms
5,488 KB
testcase_15 AC 24 ms
5,376 KB
testcase_16 AC 46 ms
5,696 KB
testcase_17 AC 44 ms
5,684 KB
testcase_18 AC 18 ms
5,376 KB
testcase_19 AC 14 ms
5,376 KB
testcase_20 AC 14 ms
5,376 KB
testcase_21 AC 44 ms
5,780 KB
testcase_22 AC 48 ms
5,852 KB
testcase_23 AC 49 ms
5,864 KB
testcase_24 AC 48 ms
5,728 KB
testcase_25 AC 43 ms
5,772 KB
testcase_26 AC 42 ms
5,636 KB
testcase_27 AC 49 ms
6,004 KB
testcase_28 AC 44 ms
5,656 KB
testcase_29 AC 47 ms
5,728 KB
testcase_30 AC 49 ms
5,872 KB
testcase_31 AC 48 ms
5,744 KB
testcase_32 AC 48 ms
5,744 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 {
		return modinv(modfact(n));
	}
}
int main(){
  int N;
  cin >> N;
  vector<int> P(N);
  for (int i = 0; i < N; i++){
    cin >> P[i];
    P[i]--;
  }
  vector<int> sz;
  vector<bool> used(N, false);
  for (int i = 0; i < N; i++){
    if (!used[i]){
      int c = i;
      used[i] = true;
      sz.push_back(1);
      while (!used[P[c]]){
        c = P[c];
        used[c] = true;
        sz.back()++;
      }
    }
  }
  sort(sz.begin(), sz.end());
  int M = sz.size();
  vector<int> cnt(N + 1, 0);
  for (int i = 0; i < M; i++){
    cnt[sz[i]]++;
  }
  int sum = 0;
  long long ans = modfact(N);
  for (int i = 0; i < M; i++){
    ans *= modinv(sz[i]);
    ans %= MOD;
  }
  for (int i = 1; i <= N; i++){
    ans *= modfactinv(cnt[i]);
    ans %= MOD;
  }
  bool ok = true;
  for (int i = 0; i < M; i++){
    if (sz[i] % 2 == 0){
      ok = false;
    }
  }
  for (int i = 0; i < M - 1; i++){
    if (sz[i] == sz[i + 1]){
      ok = false;
    }
  }
  if (ok){
    ans *= modinv(2);
    ans %= MOD;
  }
  cout << ans << endl;
}
0