Submission #486716

#TimeUsernameProblemLanguageResultExecution timeMemory
486716XIIAmusement Park (CEOI19_amusementpark)C++17
100 / 100
302 ms39388 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define fi first
#define se second
#define mp make_pair
#define eb emplace_back
#define ALL(x) (x).begin(), (x).end()

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORU(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)

#define IOS cin.tie(0)->sync_with_stdio(false);
#define PROB "CEOI19_amusementpark"
void Fi(){
    if(fopen(PROB".inp", "r")){
        freopen(PROB".inp", "r", stdin);
        freopen(PROB".out", "w", stdout);
    }
}

const int N = 18;
const int MOD = 998244353;
int n, m;
int edge[N * N];

int f[1 << N][N + 1]; /// number of not connected graph
int dp[1 << N][N + 1]; /// total number of graph

bool noEdge(const int &mask){
    FOR(i, 0, m) if((mask & edge[i]) == edge[i]) return false;
    return true;
}

void addto(int &a, const int &b){
    a = (a + b >= MOD ? a + b - MOD : a + b);
}

void subto(int &a, const int &b){
    a = (a - b < 0 ? a - b + MOD : a - b);
}

int mul(const int &a, const int &b){
    return (1LL * a * b) % MOD;
}

int main(){
    IOS;
    Fi();
    cin >> n >> m;
    FOR(i, 0, m){
        int u, v; cin >> u >> v;
        --u, --v;
        edge[i] = (1 << u) | (1 << v);
    }

    FOR(mask, 0, 1 << n) if(noEdge(mask)){
        int nbit = __builtin_popcount(mask);
        f[mask][nbit] = ((nbit & 1) ? 1 : MOD - 1);
    } /// init the no edge mask

    for(int i = 1; i < (1 << n); i <<= 1){ /// can remove bit 0...(log2(i))
        for(int j = 0; j < (1 << n); j += (i << 1)){
            FOR(k, 0, i) FORU(p, 0, n) addto(f[j | i | k][p], f[j | k][p]);
        }
    }
    /// calculate f[mask][i]: number of distinct subset of [mask] have [i] bit and not connected
    /// use -1 with odd and even +1 to use inclusive and exclusive for calculate dp later

    FOR(mask, 0, 1 << n){
        dp[mask][0] = 1;
        FORU(i, 1, n) FOR(j, 0, i){
            addto(dp[mask][i], mul(dp[mask][j], f[mask][i - j]));
        }
    }
    /// calculate dp[mask][i]: total number of distinct subset of [mask] have [i] bit

    for(int i = 1; i < (1 << n); i <<= 1){
        for(int j = 0; j < (1 << n); j += (i << 1)){
            FOR(k, 0, i) FORU(p, 0, n) subto(dp[j | i | k][p], dp[j | k][p]);
        }
    }
    /// subtract to get the answer for each distinct set

    int ans = dp[(1 << n) - 1][n];
    ans = mul(ans, m); /// there are m edges
    ans = mul(ans, (MOD + 1) / 2); /// devide by 2 (the not reverse edges)
    cout << ans;

    return 0;
}
/// Idea stolen

Compilation message (stderr)

amusementpark.cpp: In function 'void Fi()':
amusementpark.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |         freopen(PROB".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
amusementpark.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         freopen(PROB".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...