답안 #198114

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
198114 2020-01-24T18:15:39 Z model_code Amusement Park (CEOI19_amusementpark) C++17
100 / 100
2139 ms 308976 KB
#include <vector>
#include <algorithm>
#include <array>
#include <iostream>
#include <string>
using namespace std;

#define x first
#define y second
typedef std::pair<int,int> pii; 
template <unsigned int N> class Field {
    typedef unsigned int ui;
    typedef unsigned long long ull;
	inline ui pow(ui a, ui p){ui r=1,e=a;while(p){if(p&1){r=((ull)r*e)%N;}e=((ull)e*e)%N;p>>=1;}return r;}
	/*extended GCD(slow):ll t=0,nt=1,r=N,nr=a;while(nr){ll q=r/nr;t-=q*nt;swap(t,nt);r-=q*nr;swap(r,nr);}assert(r<=1);return(t<0)?t+N:t;*/
	inline ui inv(ui a){return pow(a,N-2);}
public:
    inline Field(int x = 0) : v(x) {}
	inline Field<N> pow(int p){return (*this)^p; }
	inline Field<N> operator^(int p){return {(int)pow(v,(ui)p)};}
    inline Field<N>&operator+=(const Field<N>&o) {if (v+o.v >= N) v += o.v - N; else v += o.v; return *this; }
    inline Field<N>&operator-=(const Field<N>&o) {if (v<o.v) v -= o.v-N; else v-=o.v; return *this; }
    inline Field<N>&operator*=(const Field<N>&o) {v=(ull)v*o.v % N; return *this; }
    inline Field<N>&operator/=(const Field<N>&o) { return *this*=inv(o.v); }
    inline Field<N> operator+(const Field<N>&o) const {Field<N>r{*this};return r+=o;}
    inline Field<N> operator-(const Field<N>&o) const {Field<N>r{*this};return r-=o;}
    inline Field<N> operator*(const Field<N>&o) const {Field<N>r{*this};return r*=o;}
    inline Field<N> operator/(const Field<N>&o) const {Field<N>r{*this};return r/=o;}
    inline Field<N> operator-() {if(v) return {(int)(N-v)}; else return {0};};
    inline Field<N>& operator++() { ++v; if (v==N) v=0; return *this; }
    inline Field<N> operator++(int) { Field<N>r{*this}; ++*this; return r; }
    inline Field<N>& operator--() { --v; if (v==-1) v=N-1; return *this; }
    inline Field<N> operator--(int) { Field<N>r{*this}; --*this; return r; }
    inline bool operator==(const Field<N>&o) const { return o.v==v; }
	inline bool operator!=(const Field<N>&o) const { return o.v!=v; }
	inline explicit operator ui() const { return v; }
	inline static vector<Field<N>>fact(int t){vector<Field<N>>F(t+1,1);for(int i=2;i<=t;++i){F[i]=F[i-1]*i;}return F;}
	inline static vector<Field<N>>invfact(int t){vector<Field<N>>F(t+1,1);Field<N> X{1};for(int i=2;i<=t;++i){X=X*i;}F[t]=1/X;for(int i=t-1;i>=2;--i){F[i]=F[i+1]*(i+1);}return F;}
private: ui v;
};
template<unsigned int N>istream &operator>>(std::istream&is,Field<N>&f){unsigned int v;is>>v;f=v;return is;}
template<unsigned int N>ostream &operator<<(std::ostream&os,const Field<N>&f){return os<<(unsigned int)f;}
template<unsigned int N>Field<N> operator+(int i,const Field<N>&f){return Field<N>(i)+f;}
template<unsigned int N>Field<N> operator-(int i,const Field<N>&f){return Field<N>(i)-f;}
template<unsigned int N>Field<N> operator*(int i,const Field<N>&f){return Field<N>(i)*f;}
template<unsigned int N>Field<N> operator/(int i,const Field<N>&f){return Field<N>(i)/f;}

typedef Field<998244353> FieldMod;
typedef pair<int,FieldMod> FF;

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> E(N, 0), F(N, 0);
    for (int i = 0; i < M; ++i) {
        int u, v; cin >> u >> v;
        --u; --v;
        E[u] |= 1<<v;
        F[v] |= 1<<u;
    } 
    
    int g = 1<<N;
    int f = g-1;
    
    vector<vector<FF>> D(g);
    D[0].push_back({f, 1});
    vector<FF> X;
    X.reserve(20000);
    for (int sorted = 0; sorted < g; ++sorted) {
        sort(D[sorted].begin(),D[sorted].end(), [](const FF& a, const FF& b) { return a.x < b.x; });
        for (auto d: D[sorted]) {
            if (X.empty() || X.back().x != d.x) {
                X.push_back(d);
            } else {
                X.back().y += d.y;
            }
        }

        for (auto d : X) {
            int cands = d.x;
            for (int j = 0; j < N; ++j) {
                if (cands&(1<<j)) {
                    int newCands = (cands & (~((1<<(j+1))-1))) | ((E[j]|F[j]) &~sorted);
                    int newSorted = sorted | (1<<j);
                    D[newSorted].push_back({newCands, d.y});
                }
            }
        }
        
        X.clear();
    }

    FieldMod ans = 0;
    for (auto f: D[f]) ans += f.y;
    cout << ans * M / 2 << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 296 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 2 ms 380 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 296 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 2 ms 380 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 0 ms 380 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 296 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 2 ms 380 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 0 ms 380 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 420 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 3 ms 348 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 3 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 2 ms 348 KB Output is correct
35 Correct 2 ms 376 KB Output is correct
36 Correct 2 ms 376 KB Output is correct
37 Correct 3 ms 420 KB Output is correct
38 Correct 2 ms 376 KB Output is correct
39 Correct 2 ms 376 KB Output is correct
40 Correct 2 ms 376 KB Output is correct
41 Correct 4 ms 508 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 2 ms 348 KB Output is correct
44 Correct 2 ms 376 KB Output is correct
45 Correct 2 ms 376 KB Output is correct
46 Correct 3 ms 348 KB Output is correct
47 Correct 3 ms 376 KB Output is correct
48 Correct 2 ms 504 KB Output is correct
49 Correct 2 ms 504 KB Output is correct
50 Correct 2 ms 376 KB Output is correct
51 Correct 2 ms 376 KB Output is correct
52 Correct 2 ms 376 KB Output is correct
53 Correct 3 ms 376 KB Output is correct
54 Correct 3 ms 376 KB Output is correct
55 Correct 3 ms 504 KB Output is correct
56 Correct 3 ms 504 KB Output is correct
57 Correct 3 ms 504 KB Output is correct
58 Correct 3 ms 504 KB Output is correct
59 Correct 3 ms 504 KB Output is correct
60 Correct 3 ms 376 KB Output is correct
61 Correct 2 ms 504 KB Output is correct
62 Correct 3 ms 552 KB Output is correct
63 Correct 3 ms 476 KB Output is correct
64 Correct 3 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 296 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 2 ms 380 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 0 ms 380 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 420 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 3 ms 348 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 3 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 2 ms 348 KB Output is correct
35 Correct 2 ms 376 KB Output is correct
36 Correct 2 ms 376 KB Output is correct
37 Correct 3 ms 420 KB Output is correct
38 Correct 2 ms 376 KB Output is correct
39 Correct 2 ms 376 KB Output is correct
40 Correct 2 ms 376 KB Output is correct
41 Correct 4 ms 508 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 2 ms 348 KB Output is correct
44 Correct 2 ms 376 KB Output is correct
45 Correct 2 ms 376 KB Output is correct
46 Correct 3 ms 348 KB Output is correct
47 Correct 3 ms 376 KB Output is correct
48 Correct 2 ms 504 KB Output is correct
49 Correct 2 ms 504 KB Output is correct
50 Correct 2 ms 376 KB Output is correct
51 Correct 2 ms 376 KB Output is correct
52 Correct 2 ms 376 KB Output is correct
53 Correct 3 ms 376 KB Output is correct
54 Correct 3 ms 376 KB Output is correct
55 Correct 3 ms 504 KB Output is correct
56 Correct 3 ms 504 KB Output is correct
57 Correct 3 ms 504 KB Output is correct
58 Correct 3 ms 504 KB Output is correct
59 Correct 3 ms 504 KB Output is correct
60 Correct 3 ms 376 KB Output is correct
61 Correct 2 ms 504 KB Output is correct
62 Correct 3 ms 552 KB Output is correct
63 Correct 3 ms 476 KB Output is correct
64 Correct 3 ms 504 KB Output is correct
65 Correct 32 ms 5624 KB Output is correct
66 Correct 49 ms 7936 KB Output is correct
67 Correct 44 ms 7744 KB Output is correct
68 Correct 47 ms 8108 KB Output is correct
69 Correct 11 ms 2780 KB Output is correct
70 Correct 19 ms 3552 KB Output is correct
71 Correct 16 ms 3456 KB Output is correct
72 Correct 19 ms 3632 KB Output is correct
73 Correct 18 ms 3804 KB Output is correct
74 Correct 20 ms 3840 KB Output is correct
75 Correct 18 ms 3896 KB Output is correct
76 Correct 18 ms 3848 KB Output is correct
77 Correct 68 ms 10976 KB Output is correct
78 Correct 85 ms 14116 KB Output is correct
79 Correct 119 ms 19320 KB Output is correct
80 Correct 110 ms 17636 KB Output is correct
81 Correct 118 ms 19928 KB Output is correct
82 Correct 114 ms 19452 KB Output is correct
83 Correct 118 ms 19724 KB Output is correct
84 Correct 84 ms 14648 KB Output is correct
85 Correct 34 ms 5596 KB Output is correct
86 Correct 36 ms 7292 KB Output is correct
87 Correct 33 ms 7240 KB Output is correct
88 Correct 36 ms 7360 KB Output is correct
89 Correct 40 ms 7884 KB Output is correct
90 Correct 50 ms 8004 KB Output is correct
91 Correct 42 ms 8112 KB Output is correct
92 Correct 51 ms 8056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 296 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 2 ms 380 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 0 ms 380 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 420 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 3 ms 348 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 3 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 2 ms 348 KB Output is correct
35 Correct 2 ms 376 KB Output is correct
36 Correct 2 ms 376 KB Output is correct
37 Correct 3 ms 420 KB Output is correct
38 Correct 2 ms 376 KB Output is correct
39 Correct 2 ms 376 KB Output is correct
40 Correct 2 ms 376 KB Output is correct
41 Correct 4 ms 508 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 2 ms 348 KB Output is correct
44 Correct 2 ms 376 KB Output is correct
45 Correct 2 ms 376 KB Output is correct
46 Correct 3 ms 348 KB Output is correct
47 Correct 3 ms 376 KB Output is correct
48 Correct 2 ms 504 KB Output is correct
49 Correct 2 ms 504 KB Output is correct
50 Correct 2 ms 376 KB Output is correct
51 Correct 2 ms 376 KB Output is correct
52 Correct 2 ms 376 KB Output is correct
53 Correct 3 ms 376 KB Output is correct
54 Correct 3 ms 376 KB Output is correct
55 Correct 3 ms 504 KB Output is correct
56 Correct 3 ms 504 KB Output is correct
57 Correct 3 ms 504 KB Output is correct
58 Correct 3 ms 504 KB Output is correct
59 Correct 3 ms 504 KB Output is correct
60 Correct 3 ms 376 KB Output is correct
61 Correct 2 ms 504 KB Output is correct
62 Correct 3 ms 552 KB Output is correct
63 Correct 3 ms 476 KB Output is correct
64 Correct 3 ms 504 KB Output is correct
65 Correct 32 ms 5624 KB Output is correct
66 Correct 49 ms 7936 KB Output is correct
67 Correct 44 ms 7744 KB Output is correct
68 Correct 47 ms 8108 KB Output is correct
69 Correct 11 ms 2780 KB Output is correct
70 Correct 19 ms 3552 KB Output is correct
71 Correct 16 ms 3456 KB Output is correct
72 Correct 19 ms 3632 KB Output is correct
73 Correct 18 ms 3804 KB Output is correct
74 Correct 20 ms 3840 KB Output is correct
75 Correct 18 ms 3896 KB Output is correct
76 Correct 18 ms 3848 KB Output is correct
77 Correct 68 ms 10976 KB Output is correct
78 Correct 85 ms 14116 KB Output is correct
79 Correct 119 ms 19320 KB Output is correct
80 Correct 110 ms 17636 KB Output is correct
81 Correct 118 ms 19928 KB Output is correct
82 Correct 114 ms 19452 KB Output is correct
83 Correct 118 ms 19724 KB Output is correct
84 Correct 84 ms 14648 KB Output is correct
85 Correct 34 ms 5596 KB Output is correct
86 Correct 36 ms 7292 KB Output is correct
87 Correct 33 ms 7240 KB Output is correct
88 Correct 36 ms 7360 KB Output is correct
89 Correct 40 ms 7884 KB Output is correct
90 Correct 50 ms 8004 KB Output is correct
91 Correct 42 ms 8112 KB Output is correct
92 Correct 51 ms 8056 KB Output is correct
93 Correct 511 ms 72260 KB Output is correct
94 Correct 152 ms 27768 KB Output is correct
95 Correct 65 ms 17736 KB Output is correct
96 Correct 545 ms 80836 KB Output is correct
97 Correct 513 ms 79520 KB Output is correct
98 Correct 476 ms 70556 KB Output is correct
99 Correct 100 ms 15096 KB Output is correct
100 Correct 172 ms 26376 KB Output is correct
101 Correct 270 ms 40880 KB Output is correct
102 Correct 317 ms 46772 KB Output is correct
103 Correct 18 ms 7544 KB Output is correct
104 Correct 23 ms 7800 KB Output is correct
105 Correct 89 ms 15736 KB Output is correct
106 Correct 373 ms 55496 KB Output is correct
107 Correct 37 ms 14692 KB Output is correct
108 Correct 185 ms 33476 KB Output is correct
109 Correct 857 ms 120096 KB Output is correct
110 Correct 2007 ms 284764 KB Output is correct
111 Correct 319 ms 49764 KB Output is correct
112 Correct 289 ms 46148 KB Output is correct
113 Correct 283 ms 45720 KB Output is correct
114 Correct 58 ms 11640 KB Output is correct
115 Correct 84 ms 15336 KB Output is correct
116 Correct 85 ms 15352 KB Output is correct
117 Correct 79 ms 15736 KB Output is correct
118 Correct 92 ms 17148 KB Output is correct
119 Correct 114 ms 17088 KB Output is correct
120 Correct 119 ms 17272 KB Output is correct
121 Correct 92 ms 17272 KB Output is correct
122 Correct 505 ms 76700 KB Output is correct
123 Correct 782 ms 116740 KB Output is correct
124 Correct 790 ms 119292 KB Output is correct
125 Correct 760 ms 116196 KB Output is correct
126 Correct 700 ms 107736 KB Output is correct
127 Correct 510 ms 79796 KB Output is correct
128 Correct 352 ms 56216 KB Output is correct
129 Correct 241 ms 37956 KB Output is correct
130 Correct 103 ms 18680 KB Output is correct
131 Correct 141 ms 24740 KB Output is correct
132 Correct 199 ms 33156 KB Output is correct
133 Correct 191 ms 33144 KB Output is correct
134 Correct 181 ms 33756 KB Output is correct
135 Correct 231 ms 37004 KB Output is correct
136 Correct 227 ms 37124 KB Output is correct
137 Correct 226 ms 37424 KB Output is correct
138 Correct 221 ms 37596 KB Output is correct
139 Correct 226 ms 38508 KB Output is correct
140 Correct 242 ms 38420 KB Output is correct
141 Correct 230 ms 38940 KB Output is correct
142 Correct 1905 ms 278976 KB Output is correct
143 Correct 2045 ms 286276 KB Output is correct
144 Correct 1521 ms 224920 KB Output is correct
145 Correct 1190 ms 178712 KB Output is correct
146 Correct 651 ms 100872 KB Output is correct
147 Correct 250 ms 38688 KB Output is correct
148 Correct 1900 ms 271484 KB Output is correct
149 Correct 1955 ms 282168 KB Output is correct
150 Correct 2139 ms 308976 KB Output is correct
151 Correct 397 ms 67068 KB Output is correct
152 Correct 411 ms 69612 KB Output is correct
153 Correct 524 ms 80856 KB Output is correct
154 Correct 530 ms 80896 KB Output is correct
155 Correct 554 ms 81280 KB Output is correct
156 Correct 536 ms 82132 KB Output is correct
157 Correct 540 ms 81248 KB Output is correct
158 Correct 622 ms 81832 KB Output is correct
159 Correct 553 ms 84220 KB Output is correct
160 Correct 607 ms 84768 KB Output is correct
161 Correct 546 ms 85112 KB Output is correct
162 Correct 533 ms 84784 KB Output is correct
163 Correct 550 ms 84996 KB Output is correct
164 Correct 572 ms 85060 KB Output is correct
165 Correct 567 ms 85544 KB Output is correct
166 Correct 575 ms 85580 KB Output is correct
167 Correct 559 ms 85180 KB Output is correct