結果

問題 No.2522 Fall in love, Girls!
ユーザー MasKoaTSMasKoaTS
提出日時 2023-08-27 15:25:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,050 bytes
コンパイル時間 2,563 ms
コンパイル使用メモリ 219,068 KB
実行使用メモリ 11,596 KB
最終ジャッジ日時 2023-10-27 20:50:27
合計ジャッジ時間 6,201 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 40 ms
11,596 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// TLE

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

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 || graph[j][i] == 0){
            continue;
        }
        return false;
    }
    return true;
}

ll calc_dp(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)){
                continue;
            }
            dp[s] += calc_dp(graph, dp, s - (1 << i), t);
        }
        dp[s] %= MOD;
    }
    return dp[s];
}

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


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

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

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

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

    return 0;
}
0