結果

問題 No.1428 PeRmutation Question
ユーザー butsurizukibutsurizuki
提出日時 2021-01-04 02:34:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,809 bytes
コンパイル時間 2,358 ms
コンパイル使用メモリ 198,616 KB
実行使用メモリ 12,800 KB
最終ジャッジ日時 2024-04-21 21:59:15
合計ジャッジ時間 4,433 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
11,776 KB
testcase_01 AC 10 ms
11,648 KB
testcase_02 WA -
testcase_03 AC 11 ms
11,816 KB
testcase_04 AC 10 ms
11,776 KB
testcase_05 AC 11 ms
11,776 KB
testcase_06 WA -
testcase_07 AC 10 ms
11,752 KB
testcase_08 AC 10 ms
11,800 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 10 ms
11,692 KB
testcase_12 AC 16 ms
11,848 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 25 ms
12,032 KB
testcase_16 AC 40 ms
12,672 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 18 ms
12,060 KB
testcase_20 WA -
testcase_21 AC 38 ms
12,416 KB
testcase_22 AC 41 ms
12,672 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 37 ms
12,376 KB
testcase_27 WA -
testcase_28 AC 39 ms
12,416 KB
testcase_29 AC 42 ms
12,664 KB
testcase_30 AC 42 ms
12,544 KB
testcase_31 AC 41 ms
12,648 KB
testcase_32 AC 41 ms
12,612 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];
  int gf=0;
  if(mp[1]==n){cout << "1\n";return 0;}
  for(int i=1;i<=n;i++){
    if(mp[i]!=0){
      if(i%2==1 && i!=1){gf+=mp[i];}
      //printf("%d %d\n",i,mp[i]);
      res*=power((invfact[i]*factorial[i-1])%mod,mp[i]);res%=mod;
      res*=invfact[mp[i]];res%=mod;
    }
  }
  if(gf%2==1){res*=modular_inverse(2);res%=mod;}
  cout << res << '\n';
  return 0;
}
0