結果

問題 No.1428 PeRmutation Question
ユーザー chocoruskchocorusk
提出日時 2021-03-12 22:37:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 1,827 bytes
コンパイル時間 3,898 ms
コンパイル使用メモリ 192,656 KB
実行使用メモリ 19,784 KB
最終ジャッジ日時 2024-04-22 13:47:44
合計ジャッジ時間 5,810 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
19,200 KB
testcase_01 AC 7 ms
19,148 KB
testcase_02 AC 6 ms
19,148 KB
testcase_03 AC 7 ms
19,148 KB
testcase_04 AC 6 ms
19,276 KB
testcase_05 AC 7 ms
19,200 KB
testcase_06 AC 7 ms
19,272 KB
testcase_07 AC 7 ms
19,148 KB
testcase_08 AC 7 ms
19,328 KB
testcase_09 AC 6 ms
19,280 KB
testcase_10 AC 6 ms
19,280 KB
testcase_11 AC 7 ms
19,328 KB
testcase_12 AC 12 ms
19,328 KB
testcase_13 AC 30 ms
19,456 KB
testcase_14 AC 26 ms
19,584 KB
testcase_15 AC 21 ms
19,328 KB
testcase_16 AC 35 ms
19,660 KB
testcase_17 AC 35 ms
19,528 KB
testcase_18 AC 18 ms
19,456 KB
testcase_19 AC 14 ms
19,404 KB
testcase_20 AC 15 ms
19,456 KB
testcase_21 AC 34 ms
19,660 KB
testcase_22 AC 38 ms
19,536 KB
testcase_23 AC 38 ms
19,784 KB
testcase_24 AC 37 ms
19,712 KB
testcase_25 AC 33 ms
19,528 KB
testcase_26 AC 34 ms
19,656 KB
testcase_27 AC 38 ms
19,660 KB
testcase_28 AC 34 ms
19,532 KB
testcase_29 AC 37 ms
19,532 KB
testcase_30 AC 38 ms
19,532 KB
testcase_31 AC 36 ms
19,536 KB
testcase_32 AC 37 ms
19,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
using mint=modint1000000007;
mint f[2000010], invf[2000010];
void fac(int n){
    f[0]=1;
    for(ll i=1; i<=n; i++) f[i]=f[i-1]*i;
    invf[n]=f[n].inv();
    for(ll i=n-1; i>=0; i--) invf[i]=invf[i+1]*(i+1);
}
mint comb(int x, int y){
    if(!(0<=y && y<=x)) return 0;
    return f[x]*invf[y]*invf[x-y];
}
int main()
{
    int n; cin>>n;
    int p[100010];
    for(int i=0; i<n; i++){
        cin>>p[i];
        p[i]--;
    }
    vector<int> v;
    bool used[100010]={};
    for(int i=0; i<n; i++){
        if(used[i]) continue;
        int c=0, x=i;
        do{
            used[x]=1;
            x=p[x];
            c++;
        }while(!used[x]);
        v.push_back(c);
    }
    bool allodd=1;
    for(auto c:v) if(c%2==0) allodd=0;
    fac(n);
    map<int, int> mp;
    for(auto c:v) mp[c]++;
    if(!allodd){
        mint ans=f[n];
        for(auto c:v) ans/=c;
        for(auto p:mp) ans*=invf[p.second];
        cout<<ans.val()<<endl;
    }else{
        mint ans=f[n];
        bool all1=1;
        for(auto c:v){
            if(c>1) all1=0;
        }
        if(all1) cout<<1<<endl;
        else{
            for(auto c:v) ans/=c;
            ans/=2;
            cout<<ans.val()<<endl;
        }
    }
    return 0;
}
0