답안 #405519

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
405519 2021-05-16T13:52:08 Z Falcon Amusement Park (CEOI19_amusementpark) C++17
63 / 100
3000 ms 22204 KB
#include <vector>
#include <array>
#include <iostream>

#ifdef DEBUG
#include "debug.hpp"
#endif

using namespace std;

#define all(c)              (c).begin(), (c).end()
#define rall(c)             (c).rbegin(), (c).rend()
#define traverse(c, it)     for(auto it = (c).begin(); it != (c).end(); ++it)
#define rep(i, N)           for(int i = 0; i < (N); ++i)
#define rrep(i, N)          for(int i = (N) - 1; i >= 0; --i)
#define rep1(i, N)          for(int i = 1; i <= (N); ++i)
#define rep2(i, s, e)       for(int i = (s); i <= (e); ++i)

#ifdef DEBUG
#define debug(x...)         { \
                            ++dbg::depth; \
                            string dbg_vals = dbg::to_string(x); \
                            --dbg::depth; \
                            dbg::fprint(__func__, __LINE__, #x, dbg_vals); \
                            }

#define light_debug(x)      { \
                            dbg::light = true; \
                            dbg::dout << __func__ << ":" << __LINE__; \
                            dbg::dout << "  " << #x << " = " << x << endl; \
                            dbg::light = false; \
                            }

#else
#define debug(x...)         42
#define light_debug(x)      42
#endif


using ll = long long;

template<typename T>
inline T& ckmin(T& a, T b) { return a = a > b ? b : a; }

template<typename T>
inline T& ckmax(T& a, T b) { return a = a < b ? b : a; }

class Modular {
    int value;
    
public:
    constexpr static int mod{998244353};

    Modular(long long x = 0) {
        value = (int)((x % mod + mod) % mod);
    }

    inline Modular& operator +=(Modular x) {
        value += x.value;
        if(value >= mod) value -= mod;
        return *this;
    }

    inline Modular& operator -=(Modular x) {
        value -= x.value;
        if(value < 0) value += mod;
        return *this;
    }

    inline Modular& operator *=(Modular x) {
        value = int((long long)x.value * value % mod);
        return *this;
    }
    
    // TODO : Make this Extended Euclid to handle composite moduli.
    inline Modular& operator /=(Modular x) {
        return *this *= x.pow(-1);
    }

    inline Modular operator -() const {
        return Modular(-value);
    }

    inline Modular& operator ++() {
        return *this += 1;
    }

    inline Modular& operator --() {
        return *this -= 1;
    }

    inline Modular operator ++(int) {
        Modular t{*this};
        *this += 1;
        return t;
    }

    inline Modular operator --(int) {
        Modular t{*this};
        *this -= 1;
        return t;
    }

    Modular pow(long long n) const { 
        while(n < 0) n += mod - 1;
        Modular v(1), a(value);
        for(; n; a *= a, n >>= 1)
            if(n & 1) v *= a;
        return v;
    }

    inline Modular operator +(Modular x) const {
        return x += *this;
    }

    inline Modular operator -(Modular x) const {
        return *this + (-x);
    }

    inline Modular operator *(Modular x) const {
        return x *= *this;
    }

    inline Modular operator /(Modular x) const {
        return (*this) * x.pow(-1);
    } 

    inline bool operator ==(Modular x) const {
        return value == x.value;
    }

    inline bool operator !=(Modular x) const {
        return value != x.value;
    }

    inline explicit operator int() const {
        return value;
    }

    Modular fact() const { 
        Modular x(1);
        for(int i = 1; i <= value; ++i) x *= i;
        return x;
    }

    friend ostream& operator <<(ostream& os, Modular x) {
        return os << x.value;
    }

    friend istream& operator >>(istream& is, Modular& x) {
        is >> x.value; x.value %= mod; return is;
    }

    friend string to_string(Modular x) {
        return std::to_string(int(x));
    }
};


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, m; cin >> n >> m;
    vector<vector<bool>> adj(n, vector<bool>(n));
    rep(i, m) {
        int u, v; cin >> u >> v; --u, --v;
        adj[u][v] = true;
    }

    vector<bool> is_independent(1 << n);
    rep(mask, 1 << n) {
        is_independent[mask] = true;        
        rep(i, n) if(mask & (1 << i))
            rep(j, n)
                if(adj[i][j] && (mask & (1 << j)))
                    is_independent[mask] = false;
    }
    
    vector<vector<int>> single_cost(n, vector<int>(1 << n));
    rep(i, n)
        rep1(mask, (1 << n) - 1) {
             single_cost[i][mask] = single_cost[i][mask - (1 << __lg(mask))]
                 + adj[__lg(mask)][i];  
        }
            
    /* auto partial_cost = [&](int mask, int sources) {
        int c{};
        rep(i, n)
            if(sources & (1 << i))
                c += single_cost[i][mask];
        return c;
    };*/

    auto parity = []{
        array<bool, 1 << 18> a{};
        rep(i, 1 << 18) a[i] = __builtin_popcount(i) % 2 == 0;
        return a;
    }();

    vector<Modular> dp_s(1 << n), dp_c(1 << n); dp_c[0] = 1;
    vector<int> partial_cost(1 << n);
    rep1(mask, (1 << n) - 1) {
        for(int sources{mask}; ; sources = (sources - 1) & mask) {
            debug(sources);
            if(sources == mask) continue;
            sources = mask - sources;
            int j = __lg(sources);
            partial_cost[sources] =
                partial_cost[sources - (1 << j)] + single_cost[j][mask];
            if(is_independent[sources]) {
                Modular t_s{dp_s[mask ^ sources] + 
                    dp_c[mask ^ sources] * partial_cost[sources]};
                dp_s[mask] += parity[sources] ? -t_s : t_s;

                Modular t_c{dp_c[mask ^ sources]};
                dp_c[mask] += parity[sources] ? -t_c : t_c;
            }
            sources = mask - sources;
            debug(sources);
            if(sources == 0) break;
        }
        for(int sources{mask}; sources > 0; sources = (sources - 1) & mask)
            partial_cost[sources] = 0;

    }

    cout << dp_s.back() << '\n';
    
    
    #ifdef DEBUG
    dbg::dout << "\nExecution time: "
              << clock() * 1000 / CLOCKS_PER_SEC 
              << "ms" << endl;
    #endif
    return 0;
}


Compilation message

amusementpark.cpp: In function 'int main()':
amusementpark.cpp:35:29: warning: statement has no effect [-Wunused-value]
   35 | #define debug(x...)         42
      |                             ^~
amusementpark.cpp:205:13: note: in expansion of macro 'debug'
  205 |             debug(sources);
      |             ^~~~~
amusementpark.cpp:35:29: warning: statement has no effect [-Wunused-value]
   35 | #define debug(x...)         42
      |                             ^~
amusementpark.cpp:220:13: note: in expansion of macro 'debug'
  220 |             debug(sources);
      |             ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 572 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 572 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 572 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 1 ms 576 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 572 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 572 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 572 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 1 ms 576 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 572 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 1 ms 460 KB Output is correct
27 Correct 1 ms 460 KB Output is correct
28 Correct 1 ms 460 KB Output is correct
29 Correct 1 ms 460 KB Output is correct
30 Correct 1 ms 460 KB Output is correct
31 Correct 1 ms 460 KB Output is correct
32 Correct 1 ms 460 KB Output is correct
33 Correct 1 ms 460 KB Output is correct
34 Correct 1 ms 460 KB Output is correct
35 Correct 1 ms 460 KB Output is correct
36 Correct 1 ms 460 KB Output is correct
37 Correct 1 ms 588 KB Output is correct
38 Correct 1 ms 588 KB Output is correct
39 Correct 2 ms 588 KB Output is correct
40 Correct 1 ms 588 KB Output is correct
41 Correct 1 ms 588 KB Output is correct
42 Correct 1 ms 572 KB Output is correct
43 Correct 1 ms 588 KB Output is correct
44 Correct 1 ms 588 KB Output is correct
45 Correct 1 ms 588 KB Output is correct
46 Correct 1 ms 588 KB Output is correct
47 Correct 1 ms 588 KB Output is correct
48 Correct 2 ms 572 KB Output is correct
49 Correct 2 ms 560 KB Output is correct
50 Correct 2 ms 576 KB Output is correct
51 Correct 1 ms 588 KB Output is correct
52 Correct 2 ms 588 KB Output is correct
53 Correct 2 ms 576 KB Output is correct
54 Correct 2 ms 588 KB Output is correct
55 Correct 2 ms 588 KB Output is correct
56 Correct 2 ms 580 KB Output is correct
57 Correct 2 ms 588 KB Output is correct
58 Correct 2 ms 588 KB Output is correct
59 Correct 2 ms 588 KB Output is correct
60 Correct 2 ms 624 KB Output is correct
61 Correct 2 ms 588 KB Output is correct
62 Correct 2 ms 588 KB Output is correct
63 Correct 3 ms 588 KB Output is correct
64 Correct 2 ms 588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 572 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 572 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 1 ms 576 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 572 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 1 ms 460 KB Output is correct
27 Correct 1 ms 460 KB Output is correct
28 Correct 1 ms 460 KB Output is correct
29 Correct 1 ms 460 KB Output is correct
30 Correct 1 ms 460 KB Output is correct
31 Correct 1 ms 460 KB Output is correct
32 Correct 1 ms 460 KB Output is correct
33 Correct 1 ms 460 KB Output is correct
34 Correct 1 ms 460 KB Output is correct
35 Correct 1 ms 460 KB Output is correct
36 Correct 1 ms 460 KB Output is correct
37 Correct 1 ms 588 KB Output is correct
38 Correct 1 ms 588 KB Output is correct
39 Correct 2 ms 588 KB Output is correct
40 Correct 1 ms 588 KB Output is correct
41 Correct 1 ms 588 KB Output is correct
42 Correct 1 ms 572 KB Output is correct
43 Correct 1 ms 588 KB Output is correct
44 Correct 1 ms 588 KB Output is correct
45 Correct 1 ms 588 KB Output is correct
46 Correct 1 ms 588 KB Output is correct
47 Correct 1 ms 588 KB Output is correct
48 Correct 2 ms 572 KB Output is correct
49 Correct 2 ms 560 KB Output is correct
50 Correct 2 ms 576 KB Output is correct
51 Correct 1 ms 588 KB Output is correct
52 Correct 2 ms 588 KB Output is correct
53 Correct 2 ms 576 KB Output is correct
54 Correct 2 ms 588 KB Output is correct
55 Correct 2 ms 588 KB Output is correct
56 Correct 2 ms 580 KB Output is correct
57 Correct 2 ms 588 KB Output is correct
58 Correct 2 ms 588 KB Output is correct
59 Correct 2 ms 588 KB Output is correct
60 Correct 2 ms 624 KB Output is correct
61 Correct 2 ms 588 KB Output is correct
62 Correct 2 ms 588 KB Output is correct
63 Correct 3 ms 588 KB Output is correct
64 Correct 2 ms 588 KB Output is correct
65 Correct 54 ms 1604 KB Output is correct
66 Correct 53 ms 1592 KB Output is correct
67 Correct 51 ms 1640 KB Output is correct
68 Correct 52 ms 1612 KB Output is correct
69 Correct 92 ms 1732 KB Output is correct
70 Correct 74 ms 1640 KB Output is correct
71 Correct 75 ms 1612 KB Output is correct
72 Correct 90 ms 1612 KB Output is correct
73 Correct 66 ms 1612 KB Output is correct
74 Correct 68 ms 1612 KB Output is correct
75 Correct 69 ms 1772 KB Output is correct
76 Correct 65 ms 1612 KB Output is correct
77 Correct 161 ms 2948 KB Output is correct
78 Correct 156 ms 3008 KB Output is correct
79 Correct 151 ms 2936 KB Output is correct
80 Correct 148 ms 2936 KB Output is correct
81 Correct 147 ms 2936 KB Output is correct
82 Correct 145 ms 2928 KB Output is correct
83 Correct 140 ms 2876 KB Output is correct
84 Correct 144 ms 2936 KB Output is correct
85 Correct 272 ms 2936 KB Output is correct
86 Correct 245 ms 2888 KB Output is correct
87 Correct 222 ms 2948 KB Output is correct
88 Correct 220 ms 2932 KB Output is correct
89 Correct 191 ms 2932 KB Output is correct
90 Correct 189 ms 2948 KB Output is correct
91 Correct 191 ms 2936 KB Output is correct
92 Correct 205 ms 2888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 572 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 572 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 1 ms 576 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 572 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 1 ms 460 KB Output is correct
27 Correct 1 ms 460 KB Output is correct
28 Correct 1 ms 460 KB Output is correct
29 Correct 1 ms 460 KB Output is correct
30 Correct 1 ms 460 KB Output is correct
31 Correct 1 ms 460 KB Output is correct
32 Correct 1 ms 460 KB Output is correct
33 Correct 1 ms 460 KB Output is correct
34 Correct 1 ms 460 KB Output is correct
35 Correct 1 ms 460 KB Output is correct
36 Correct 1 ms 460 KB Output is correct
37 Correct 1 ms 588 KB Output is correct
38 Correct 1 ms 588 KB Output is correct
39 Correct 2 ms 588 KB Output is correct
40 Correct 1 ms 588 KB Output is correct
41 Correct 1 ms 588 KB Output is correct
42 Correct 1 ms 572 KB Output is correct
43 Correct 1 ms 588 KB Output is correct
44 Correct 1 ms 588 KB Output is correct
45 Correct 1 ms 588 KB Output is correct
46 Correct 1 ms 588 KB Output is correct
47 Correct 1 ms 588 KB Output is correct
48 Correct 2 ms 572 KB Output is correct
49 Correct 2 ms 560 KB Output is correct
50 Correct 2 ms 576 KB Output is correct
51 Correct 1 ms 588 KB Output is correct
52 Correct 2 ms 588 KB Output is correct
53 Correct 2 ms 576 KB Output is correct
54 Correct 2 ms 588 KB Output is correct
55 Correct 2 ms 588 KB Output is correct
56 Correct 2 ms 580 KB Output is correct
57 Correct 2 ms 588 KB Output is correct
58 Correct 2 ms 588 KB Output is correct
59 Correct 2 ms 588 KB Output is correct
60 Correct 2 ms 624 KB Output is correct
61 Correct 2 ms 588 KB Output is correct
62 Correct 2 ms 588 KB Output is correct
63 Correct 3 ms 588 KB Output is correct
64 Correct 2 ms 588 KB Output is correct
65 Correct 54 ms 1604 KB Output is correct
66 Correct 53 ms 1592 KB Output is correct
67 Correct 51 ms 1640 KB Output is correct
68 Correct 52 ms 1612 KB Output is correct
69 Correct 92 ms 1732 KB Output is correct
70 Correct 74 ms 1640 KB Output is correct
71 Correct 75 ms 1612 KB Output is correct
72 Correct 90 ms 1612 KB Output is correct
73 Correct 66 ms 1612 KB Output is correct
74 Correct 68 ms 1612 KB Output is correct
75 Correct 69 ms 1772 KB Output is correct
76 Correct 65 ms 1612 KB Output is correct
77 Correct 161 ms 2948 KB Output is correct
78 Correct 156 ms 3008 KB Output is correct
79 Correct 151 ms 2936 KB Output is correct
80 Correct 148 ms 2936 KB Output is correct
81 Correct 147 ms 2936 KB Output is correct
82 Correct 145 ms 2928 KB Output is correct
83 Correct 140 ms 2876 KB Output is correct
84 Correct 144 ms 2936 KB Output is correct
85 Correct 272 ms 2936 KB Output is correct
86 Correct 245 ms 2888 KB Output is correct
87 Correct 222 ms 2948 KB Output is correct
88 Correct 220 ms 2932 KB Output is correct
89 Correct 191 ms 2932 KB Output is correct
90 Correct 189 ms 2948 KB Output is correct
91 Correct 191 ms 2936 KB Output is correct
92 Correct 205 ms 2888 KB Output is correct
93 Execution timed out 3060 ms 22204 KB Time limit exceeded
94 Halted 0 ms 0 KB -