結果

問題 No.1392 Don't be together
ユーザー carrot46carrot46
提出日時 2021-02-12 23:29:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,317 ms / 2,000 ms
コード長 5,220 bytes
コンパイル時間 1,667 ms
コンパイル使用メモリ 174,520 KB
実行使用メモリ 12,888 KB
最終ジャッジ日時 2024-07-20 01:12:45
合計ジャッジ時間 17,291 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
12,452 KB
testcase_01 AC 11 ms
12,456 KB
testcase_02 AC 11 ms
12,640 KB
testcase_03 AC 12 ms
12,672 KB
testcase_04 AC 11 ms
12,672 KB
testcase_05 AC 11 ms
12,592 KB
testcase_06 AC 552 ms
12,800 KB
testcase_07 AC 1,040 ms
12,736 KB
testcase_08 AC 922 ms
12,688 KB
testcase_09 AC 1,317 ms
12,756 KB
testcase_10 AC 623 ms
12,756 KB
testcase_11 AC 985 ms
12,608 KB
testcase_12 AC 584 ms
12,780 KB
testcase_13 AC 815 ms
12,832 KB
testcase_14 AC 911 ms
12,800 KB
testcase_15 AC 638 ms
12,784 KB
testcase_16 AC 1,023 ms
12,888 KB
testcase_17 AC 660 ms
12,672 KB
testcase_18 AC 904 ms
12,748 KB
testcase_19 AC 806 ms
12,800 KB
testcase_20 AC 334 ms
12,672 KB
testcase_21 AC 120 ms
12,640 KB
testcase_22 AC 534 ms
12,660 KB
testcase_23 AC 368 ms
12,672 KB
testcase_24 AC 217 ms
12,580 KB
testcase_25 AC 404 ms
12,544 KB
testcase_26 AC 100 ms
12,576 KB
testcase_27 AC 161 ms
12,672 KB
testcase_28 AC 443 ms
12,544 KB
testcase_29 AC 226 ms
12,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
const ll INF = (1LL<<60);

template<class T> bool chmin(T &a, const T b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T b){
    if(a < b) {a = b; return true;}
    else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
    if(!v.empty()){
        for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
        std::cout<<v.back();
    }
    if(endline) std::cout<<std::endl;
}

template <unsigned long long mod > class modint{
public:
    ll x;
    constexpr modint(){x = 0;}
    constexpr modint(ll _x) : x((_x < 0 ? ((_x += (LLONG_MAX / mod) * mod) < 0 ? _x + (LLONG_MAX / mod) * mod : _x) : _x)%mod){}
    constexpr modint set_raw(ll _x){
        //_x in [0, mod)
        x = _x;
        return *this;
    }
    constexpr modint operator-(){
        return x == 0 ? 0 : mod - x;
    }
    constexpr modint& operator+=(const modint& a){
        if((x += a.x) >= mod) x -= mod;
        return *this;
    }
    constexpr modint operator+(const modint& a) const{
        return modint(*this) += a;
    }
    constexpr modint& operator-=(const modint& a){
        if((x -= a.x) < 0) x += mod;
        return *this;
    }
    constexpr modint operator-(const modint& a) const{
        return modint(*this) -= a;
    }
    constexpr modint& operator*=(const modint& a){
        (x *= a.x)%=mod;
        return *this;
    }
    constexpr modint operator*(const modint& a) const{
        return modint(*this) *= a;
    }
    constexpr modint pow(unsigned long long pw) const{
        modint res(1), comp(*this);
        while(pw){
            if(pw&1) res *= comp;
            comp *= comp;
            pw >>= 1;
        }
        return res;
    }
    //以下、modが素数のときのみ
    constexpr modint inv() const{
        if(x == 2) return (mod + 1) >> 1;
        return modint(*this).pow(mod - 2);
    }
    constexpr modint& operator/=(const modint &a){
        (x *= a.inv().x)%=mod;
        return *this;
    }
    constexpr modint operator/(const modint &a) const{
        return modint(*this) /= a;
    }
    constexpr bool sqrt(bool find_mini = false) {
        if(x == 0) return true;
        modint jge = this->pow((mod - 1)>>1);
        if(jge.x + 1 == mod) return false;
        if((mod&3) == 3){
            *this = this->pow((mod + 1)>>2);
        }else{
            int m = 0;
            modint c, t;
            if(mod == 998244353){
                m = 23; c = 15311432; t = this->pow(119);
                *this = this->pow(60);
            }else{
                ll q = mod - 1;
                modint z = 2;
                while(!(q&1)){q>>=1; ++m;}
                while(z.pow((mod-1)>>1).x == 1) z += 1;
                c = z.pow(q); t = this->pow(q);
                *this = this->pow((q+1)>>1);
            }
            while(t.x != 1){
                modint cpy_t = t;
                int pw = m;
                while(cpy_t.x != 1){--pw; cpy_t *= cpy_t;}
                rep(i, pw-1) c *= c;
                (*this) *= c;
                c *= c;
                t *= c;
                m -= pw;
            }
        }
        if(find_mini) this->x = min(this->x, (ll)mod - this->x);
        return true;
    }
};
#define mod1 998244353
using mint = modint<mod1>;

ostream& operator<<(ostream& os, const mint& a){
    os << a.x;
    return os;
}
using vm = vector<mint>;

const ll MAX_N = ll(4e+5) + 10;
vm fact(MAX_N, mint(1)), fact_inv(MAX_N, mint(1)), n_inv(MAX_N, mint(1));
void makefact(){
    mint tmp;
    reps(i,2,MAX_N) fact[i] = fact[i-1] * tmp.set_raw(i);
    fact_inv[MAX_N - 1] = fact[MAX_N - 1].inv();
    Rreps(i, MAX_N - 1, 1){
        fact_inv[i] = fact_inv[i + 1] * tmp.set_raw(i + 1);
        n_inv[i + 1] = fact[i] * fact_inv[i + 1];
    }
}
mint nCm(ll n, ll m){
    return fact[n] * fact_inv[n-m] * fact_inv[m];
}
mint nCm_inv(ll n, ll m){
    return fact[n-m] * fact[m] * fact_inv[n];
}

mint solve(int m, vec &gs){
    if(m == 1) return 0;
    vm diff(N + 1), same(N + 1);
    diff[0] = diff[1] = 0;
    reps(i, 2, N + 1) diff[i] = mint(m - 1).pow(i - 1) * m - diff[i-1];
    mint res(1);
    for(ll a : gs) res *= diff[a];
    return res;
}

int main(){
    makefact();
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin>>N>>M;
    vec a(N), used(N, 0);
    rep(i, N) {cin>>a[i]; --a[i];}
    vec gs;
    rep(i, N) {
        int sz = 0, v = i;
        while (!used[v]) {
            used[v] = 1;
            v = a[v];
            ++sz;
        }
        if(sz) gs.push_back(sz);
    }
    //my_printv(gs);
    mint res(0);
    Rreps(i, M + 1, 1){
        if((M - i)&1) res -= solve(i, gs) * nCm(M, i);
        else res += solve(i, gs) * nCm(M, i);
        //cout<<solve(i, gs)<<endl;
    }
    cout<<res * fact_inv[M]<<endl;
}
0