結果

問題 No.2522 Fall in love, Girls!
ユーザー MasKoaTSMasKoaTS
提出日時 2023-08-28 16:08:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 71 ms / 2,000 ms
コード長 2,110 bytes
コンパイル時間 2,697 ms
コンパイル使用メモリ 219,312 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2023-10-27 20:50:28
合計ジャッジ時間 4,039 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 39 ms
11,520 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 4 ms
4,348 KB
testcase_07 AC 7 ms
4,348 KB
testcase_08 AC 8 ms
4,348 KB
testcase_09 AC 12 ms
4,348 KB
testcase_10 AC 14 ms
11,520 KB
testcase_11 AC 15 ms
11,520 KB
testcase_12 AC 16 ms
4,348 KB
testcase_13 AC 35 ms
11,520 KB
testcase_14 AC 71 ms
11,520 KB
testcase_15 AC 50 ms
11,520 KB
testcase_16 AC 62 ms
11,520 KB
testcase_17 AC 26 ms
4,348 KB
testcase_18 AC 20 ms
4,348 KB
testcase_19 AC 14 ms
4,348 KB
testcase_20 AC 15 ms
4,348 KB
testcase_21 AC 25 ms
4,348 KB
testcase_22 AC 23 ms
4,348 KB
testcase_23 AC 21 ms
4,348 KB
testcase_24 AC 17 ms
4,348 KB
testcase_25 AC 26 ms
11,520 KB
testcase_26 AC 17 ms
11,520 KB
testcase_27 AC 6 ms
4,348 KB
testcase_28 AC 8 ms
11,520 KB
testcase_29 AC 16 ms
4,348 KB
testcase_30 AC 21 ms
4,348 KB
testcase_31 AC 16 ms
4,348 KB
testcase_32 AC 9 ms
11,520 KB
testcase_33 AC 20 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define MOD 998244353ll
using namespace std;
using ll = long long;

ll nPk(ll n, ll k){
    ll ret = 1;
    for(ll i = n; i > n - k; --i){
        ret *= i;
        ret %= MOD;
    }
    return ret;
}

bool can_set_left(vector<vector<int> >& graph, ll s, int i, ll t){
    for(int j = 0; j < t; ++j){
        if(i == j || ((s >> j) & 1) == 0){
            continue;
        }
        if(graph[j][i] == 1){
            return false;
        }
    }
    return true;
}

ll count_toposo(vector<vector<int> >& graph, vector<ll>& dp, ll s, ll t){
    if(dp[s] == -1){
        dp[s] = 0;
        for(int i = 0; i < t; ++i){
            if(((s >> i) & 1) == 0){
                continue;
            }
            if(can_set_left(graph, s, i, t)){
                dp[s] += count_toposo(graph, dp, s - (1 << i), t);
            }
        }
        dp[s] %= MOD;
    }
    return dp[s];
}


int main(){
    ll N, M, K; cin >> N >> M >> K;
    vector<pair<int, int> > edges(K, pair<int, int>({}));
    unordered_set<int> toposo_st = {};
    for(auto& [x, y] : edges){
        cin >> x >> y;
        toposo_st.insert(--x);
        toposo_st.insert(--y);
    }

    unordered_map<int, int> toposo_ids = {};
    for(auto itr = toposo_st.begin(); itr != toposo_st.end(); ++itr){
        toposo_ids[*itr] = toposo_ids.size();
    }
    ll t = toposo_ids.size();

    vector<vector<int> > graph(t, vector<int>(t));
    for(auto& [x, y] : edges){
        graph[toposo_ids[x]][toposo_ids[y]] = 1;
    }
    vector<ll> dp(1 << t, -1);
    dp[0] = 1;

    ll ans = 0;
    ll p1 = nPk(N - 1, N - t);
    ll p2 = nPk(N - 1, N - t - 1);
    for(int i = M; i < N; ++i){
        if(toposo_st.find(i) != toposo_st.end()){
            int id = toposo_ids[i];
            if(!can_set_left(graph, (1 << t) - 1, id, t)){
                continue;
            }
            ans += p1 * count_toposo(graph, dp, (1 << t) - 1 - (1 << id), t) % MOD;
        }
        else{
            ans += p2 * count_toposo(graph, dp, (1 << t) - 1, t) % MOD;
        }
    }
    ans %= MOD;
    cout << ans << endl;

    return 0;
}
0