結果

問題 No.1428 PeRmutation Question
ユーザー butsurizukibutsurizuki
提出日時 2021-01-04 09:54:17
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 1,768 bytes
コンパイル時間 2,317 ms
コンパイル使用メモリ 198,748 KB
実行使用メモリ 12,672 KB
最終ジャッジ日時 2024-04-22 11:21:17
合計ジャッジ時間 4,295 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
11,648 KB
testcase_01 AC 13 ms
11,648 KB
testcase_02 AC 13 ms
11,776 KB
testcase_03 AC 13 ms
11,648 KB
testcase_04 AC 13 ms
11,776 KB
testcase_05 AC 13 ms
11,648 KB
testcase_06 AC 12 ms
11,776 KB
testcase_07 AC 13 ms
11,648 KB
testcase_08 AC 12 ms
11,776 KB
testcase_09 AC 13 ms
11,648 KB
testcase_10 AC 13 ms
11,904 KB
testcase_11 AC 13 ms
11,776 KB
testcase_12 AC 18 ms
12,032 KB
testcase_13 AC 39 ms
12,416 KB
testcase_14 AC 34 ms
12,160 KB
testcase_15 AC 28 ms
12,032 KB
testcase_16 AC 42 ms
12,416 KB
testcase_17 AC 42 ms
12,416 KB
testcase_18 AC 23 ms
11,776 KB
testcase_19 AC 21 ms
11,904 KB
testcase_20 AC 21 ms
12,032 KB
testcase_21 AC 40 ms
12,544 KB
testcase_22 AC 44 ms
12,672 KB
testcase_23 AC 44 ms
12,544 KB
testcase_24 AC 44 ms
12,544 KB
testcase_25 AC 40 ms
12,416 KB
testcase_26 AC 40 ms
12,544 KB
testcase_27 AC 44 ms
12,544 KB
testcase_28 AC 41 ms
12,416 KB
testcase_29 AC 44 ms
12,544 KB
testcase_30 AC 45 ms
12,672 KB
testcase_31 AC 42 ms
12,544 KB
testcase_32 AC 43 ms
12,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define mod 1000000007

using namespace std;

long long power(long long a,long long b){
  long long x=1,y=a;
  while(b>0){
    if(b&1ll){
      x=(x*y)%mod;
    }
    y=(y*y)%mod;
    b>>=1;
  }
  return x%mod;
}

long long modular_inverse(long long n){
  return power(n,mod-2);
}

long long factorial[524288];
long long invfact[524288];

void cfact(){
  long long i;
  factorial[0]=1;
  factorial[1]=1;
  for(i=2;i<524288;i++){
    factorial[i]=factorial[i-1]*i;
    factorial[i]%=mod;
  }
  invfact[524287]=modular_inverse(factorial[524287]);
  for(i=524286;i>=0;i--){
    invfact[i]=invfact[i+1]*(i+1);
    invfact[i]%=mod;
  }
}

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) { }
  bool unionSet(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) {
    return root(x) == root(y);
  }
  int root(int x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x) {
    return -data[root(x)];
  }
};

int main(){
  cfact();
  int n;
  cin >> n;
  if(n==1){cout << "1\n";return 0;}
  vector<int> a(n+1);
  vector<int> mp(n+1);
  UnionFind uf(n+1);
  for(int i=1;i<=n;i++){
    cin >> a[i];
    uf.unionSet(i,a[i]);
  }
  for(int i=1;i<=n;i++){
    if(uf.root(i)==i){mp[uf.size(i)]++;}
  }
  long long res=factorial[n];
  if(mp[1]==n){cout << "1\n";return 0;}
  int fl=1;
  for(int i=1;i<=n;i++){
    if(mp[i]!=0){
      if(i%2==0 || mp[i]>=2){fl=0;}
      res*=power((invfact[i]*factorial[i-1])%mod,mp[i]);res%=mod;
      res*=invfact[mp[i]];res%=mod;
    }
  }
  if(fl){res*=modular_inverse(2);res%=mod;}
  cout << res << '\n';
  return 0;
}
0