#include<iostream> #include<vector> #include<map> #include<algorithm> using namespace std; const unsigned long long MOD = 1'000'000'007; struct level { int a, b; }; bool intersect(level &x, level &y) { if (y.a < x.a && y.b > x.a) return true; if (y.a < x.b && y.b > x.b) return true; return false; } bool intersect(level &x, int p) { return x.a < p && x.b > p; } void dfs(vector<vector<int>> &graph, vector<bool> &visited, int u) { visited[u] = true; for (int v : graph[u]) { if (! visited[v]) { dfs(graph, visited, v); } } } unsigned long long gcd(unsigned long long a, unsigned long long b) { if (a == 0) return b; return gcd(b % a, a); } unsigned long long inv(unsigned long long a) { return a <= 1 ? a : MOD - (MOD / a) * inv(MOD % a) % MOD; } unsigned long long fact(int n) { unsigned long long res = 1; for (int i = 2; i <= n; i++) { res *= i; res %= MOD; } return res; } unsigned long long tobits(vector<int> &nodes) { unsigned long long res = 0; for (int e : nodes) { res |= 1LL << e; } return res; } unsigned long long div(unsigned long long a, unsigned long long b) { unsigned long long r = gcd(a, b); a /= r; b /= r; return a * inv(b) % MOD; } unsigned long long calc(vector<vector<int>> &graph, vector<int> nodes, map<unsigned long long, unsigned long long> &m) { unsigned long long key = tobits(nodes); if (m.count(key)) { return m[key]; } unsigned long long res = 0; if (nodes.size() < 2) { res += nodes.size(); } else { for (int u : nodes) { // cout << "processing vertex " << u << " from nodes "; // for (int node : nodes) { // cout << node << ", "; // } // cout << endl; vector<bool> visited(graph.size()); dfs(graph, visited, u); vector<int> free; for (int node : nodes) { if (! visited[node]) { free.push_back(node); } } unsigned long long s = calc(graph, free, m); unsigned long long t = fact(nodes.size() - 1); unsigned long long w = fact(free.size()); unsigned long long accs = t + div(t, w) * s; // cout << "starting from " << u << " checking nodes: "; // for (int e : free) { // cout << e << ", "; // } // cout << " result: " << accs << endl; res += accs; res %= MOD; } } m[key] = res; return res; } int main() { int n; cin >> n; vector<level> levels(n); for (int i = 0; i < n; i++) { cin >> levels[i].a >> levels[i].b; } vector<vector<int>> graph(n); for (int i = 1; i < n; i++) { for (int j = i - 1; j >= 0; j--) { if (intersect(levels[j], levels[i].a)) { graph[i].push_back(j); break; } } for (int j = i - 1; j >= 0; j--) { if (intersect(levels[j], levels[i].b)) { if (graph[i].empty() || graph[i].back() != j) { graph[i].push_back(j); } break; } } } vector<int> perm(n); for (int i = 0; i < n; i++) { perm[i] = i; } unsigned long long all = 1; for (int i = 2; i <= n; i++) { all = (all * i) % MOD; } map<unsigned long long, unsigned long long> m; unsigned long long cnt = calc(graph, perm, m); if (cnt % all == 0) { cout << cnt / all << endl; } else { cout << div(cnt, all) << endl; } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | #include<iostream> #include<vector> #include<map> #include<algorithm> using namespace std; const unsigned long long MOD = 1'000'000'007; struct level { int a, b; }; bool intersect(level &x, level &y) { if (y.a < x.a && y.b > x.a) return true; if (y.a < x.b && y.b > x.b) return true; return false; } bool intersect(level &x, int p) { return x.a < p && x.b > p; } void dfs(vector<vector<int>> &graph, vector<bool> &visited, int u) { visited[u] = true; for (int v : graph[u]) { if (! visited[v]) { dfs(graph, visited, v); } } } unsigned long long gcd(unsigned long long a, unsigned long long b) { if (a == 0) return b; return gcd(b % a, a); } unsigned long long inv(unsigned long long a) { return a <= 1 ? a : MOD - (MOD / a) * inv(MOD % a) % MOD; } unsigned long long fact(int n) { unsigned long long res = 1; for (int i = 2; i <= n; i++) { res *= i; res %= MOD; } return res; } unsigned long long tobits(vector<int> &nodes) { unsigned long long res = 0; for (int e : nodes) { res |= 1LL << e; } return res; } unsigned long long div(unsigned long long a, unsigned long long b) { unsigned long long r = gcd(a, b); a /= r; b /= r; return a * inv(b) % MOD; } unsigned long long calc(vector<vector<int>> &graph, vector<int> nodes, map<unsigned long long, unsigned long long> &m) { unsigned long long key = tobits(nodes); if (m.count(key)) { return m[key]; } unsigned long long res = 0; if (nodes.size() < 2) { res += nodes.size(); } else { for (int u : nodes) { // cout << "processing vertex " << u << " from nodes "; // for (int node : nodes) { // cout << node << ", "; // } // cout << endl; vector<bool> visited(graph.size()); dfs(graph, visited, u); vector<int> free; for (int node : nodes) { if (! visited[node]) { free.push_back(node); } } unsigned long long s = calc(graph, free, m); unsigned long long t = fact(nodes.size() - 1); unsigned long long w = fact(free.size()); unsigned long long accs = t + div(t, w) * s; // cout << "starting from " << u << " checking nodes: "; // for (int e : free) { // cout << e << ", "; // } // cout << " result: " << accs << endl; res += accs; res %= MOD; } } m[key] = res; return res; } int main() { int n; cin >> n; vector<level> levels(n); for (int i = 0; i < n; i++) { cin >> levels[i].a >> levels[i].b; } vector<vector<int>> graph(n); for (int i = 1; i < n; i++) { for (int j = i - 1; j >= 0; j--) { if (intersect(levels[j], levels[i].a)) { graph[i].push_back(j); break; } } for (int j = i - 1; j >= 0; j--) { if (intersect(levels[j], levels[i].b)) { if (graph[i].empty() || graph[i].back() != j) { graph[i].push_back(j); } break; } } } vector<int> perm(n); for (int i = 0; i < n; i++) { perm[i] = i; } unsigned long long all = 1; for (int i = 2; i <= n; i++) { all = (all * i) % MOD; } map<unsigned long long, unsigned long long> m; unsigned long long cnt = calc(graph, perm, m); if (cnt % all == 0) { cout << cnt / all << endl; } else { cout << div(cnt, all) << endl; } return 0; } |