#include <algorithm> #include <cmath> #include <iostream> #include <vector> const int MOD = 1000000007; long long inv(long long q) { long long p = 1000000007; long long res = 1; long long exp = p - 2; while (exp > 0) { if (exp % 2 == 1) { res = (res * q) % p; } q = (q * q) % p; exp /= 2; } return res; } int main() { int n; std::cin >> n; std::vector<int> l(n), r(n); for (int i = 0; i < n; i++) { std::cin >> l[i] >> r[i]; } std::vector<std::vector<int>> zaleznosc(n); for (int i = 0; i < n-1; i++) { for (int j = i+1; j < n; j++) { if (l[i] >= l[j] && l[i] <= r[j]) { zaleznosc[i].push_back(j); break; } } for (int j = i+1; j < n; j++) { if (r[i] >= l[j] && r[i] <= r[j]) { zaleznosc[i].push_back(j); break; } } } std::vector<bool> visited(n, false); long long sum = 0; std::vector<int> nums(n); for (int i = 0; i < n; i++) { nums[i] = i; } int coutner = 0; int coun =0; do { visited.assign(n, false); coun++; for (int i = 0; i < nums.size(); i++) { if (!visited[nums[i]]) { coutner++; } for (int j = 0; j < zaleznosc[nums[i]].size(); j++) { visited[zaleznosc[nums[i]][j]] = true; } } sum += coutner; coutner = 0; } while (std::next_permutation(nums.begin(), nums.end())); long long p = sum % MOD; long long q = coun % MOD; long long result = (p * inv(q)) % MOD; std::cout << result << std::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 | #include <algorithm> #include <cmath> #include <iostream> #include <vector> const int MOD = 1000000007; long long inv(long long q) { long long p = 1000000007; long long res = 1; long long exp = p - 2; while (exp > 0) { if (exp % 2 == 1) { res = (res * q) % p; } q = (q * q) % p; exp /= 2; } return res; } int main() { int n; std::cin >> n; std::vector<int> l(n), r(n); for (int i = 0; i < n; i++) { std::cin >> l[i] >> r[i]; } std::vector<std::vector<int>> zaleznosc(n); for (int i = 0; i < n-1; i++) { for (int j = i+1; j < n; j++) { if (l[i] >= l[j] && l[i] <= r[j]) { zaleznosc[i].push_back(j); break; } } for (int j = i+1; j < n; j++) { if (r[i] >= l[j] && r[i] <= r[j]) { zaleznosc[i].push_back(j); break; } } } std::vector<bool> visited(n, false); long long sum = 0; std::vector<int> nums(n); for (int i = 0; i < n; i++) { nums[i] = i; } int coutner = 0; int coun =0; do { visited.assign(n, false); coun++; for (int i = 0; i < nums.size(); i++) { if (!visited[nums[i]]) { coutner++; } for (int j = 0; j < zaleznosc[nums[i]].size(); j++) { visited[zaleznosc[nums[i]][j]] = true; } } sum += coutner; coutner = 0; } while (std::next_permutation(nums.begin(), nums.end())); long long p = sum % MOD; long long q = coun % MOD; long long result = (p * inv(q)) % MOD; std::cout << result << std::endl; return 0; } |