#include <cstdio> #include <queue> using namespace std; int child[500001][2]; // left and right children of x int intervals[2000001]; // >1'000'000 are single nodes int parents[500001]; int totaldrips[500001]; int factorials[500001]; int visited[500001]; const int MAX_IDX = 1048575; // 2^20 - 1 const long long MODULO = 1'000'000'007LL; void all_factorials(int n) { long long res = 1; factorials[0] = 1; factorials[1] = 1; for (int i = 2; i <= n; ++i) { res *= i; res %= 1'000'000'007LL; factorials[i] = int(res); } } long long odw_modulo(long long k) { long long r = MODULO; long long newr = k; long long t = 0; long long newt = 1; long long temp; while (newr != 0) { long long quotient = r / newr; temp = newt; newt = t - (quotient * newt); t = temp; temp = newr; newr = r - (quotient * newr); r = temp; } while (t < 0) { t += MODULO; } return t; } void setInterval(int l, int r, int cl, int cr, int x, int n) { // printf("SETTING INTERVAL %8d %8d | %8d %8d | to %d\n", cl, cr, l, r, n); // l, r - full required interval // cl, cr - interval considered in this iteration // x - tree index // n - value to set if (cl >= l && cr <= r) { // printf(" FULL INTERVAL COVERED\n"); intervals[x] = n; } else if (cl > r || cr < l) { // printf(" NO COMMON POINTS\n"); // no common points return; } else { // printf(" SPLITTING\n"); // must set right and left int lhalf_end = (cl + cr) / 2; int lhalf_idx = x * 2 + 1; if (intervals[x] != -2) { intervals[lhalf_idx] = intervals[x]; intervals[lhalf_idx+1] = intervals[x]; intervals[x] = -2; } setInterval(l, r, cl, lhalf_end, lhalf_idx, n); setInterval(l, r, lhalf_end+1, cr, lhalf_idx+1, n); } } void setIntervalStart(int l, int r, int n) { setInterval(l, r, 0, MAX_IDX, 0, n); } int getValue(int l, int r, int x, int k) { // printf("CHECKING INTERVAL %8d %8d (val: %d) | %d | \n", l, r, intervals[x], k); // l, r - current searched interval // x - tree index // k - required key if (intervals[x] != -2) { // printf("WHOLE INTERVAL HAS VALUE %d\n", intervals[x]); return intervals[x]; } // else go deeper int lhalf_end = (l + r) / 2; int lhalf_idx = x * 2 + 1; if (k <= lhalf_end) { return getValue(l, lhalf_end, lhalf_idx, k); } else { return getValue(lhalf_end+1, r, lhalf_idx+1, k); } } int getValueStart(int k) { return getValue(0, MAX_IDX, 0, k); } int main() { int n; scanf("%d", &n); all_factorials(n); intervals[0] = -1; // -1 = floor, -2 = split for (int i = 0; i < n; ++i) { int l, r; scanf("%d %d", &l, &r); child[i][0] = getValueStart(l-1); child[i][1] = getValueStart(r-1); if (child[i][0] == child[i][1]) { child[i][1] = -1; } // parents[child[i][0]] ++; // parents[child[i][1]] ++; setIntervalStart(l-1, r-1, i); // printf("PLATFORM %d: L: %d, R: %d\n", i, child[i][0], child[i][1]); } // TODO: FIGURE OUT HOW TO DO LINEAR TOTALDRIP COMPUTATION queue<int> bfs; for (int i = 0; i < n; ++i) { for (int j = 0; j < i; ++j) { visited[j] = 0; } int mc1 = child[i][0]; int mc2 = child[i][1]; if (mc1 != -1 && !visited[mc1]) { visited[mc1] = true; bfs.push(mc1); } if (mc2 != -1 && !visited[mc2]) { visited[mc2] = true; bfs.push(mc2); } while (!bfs.empty()) { int c = bfs.front(); bfs.pop(); if (c == -1) { continue; } // printf("%d gets a drip from %d\n", c, i); totaldrips[c]++; int c1 = child[c][0]; int c2 = child[c][1]; if (c1 != -1 && !visited[c1]) { visited[c1] = true; bfs.push(c1); } if (c2 != -1 && !visited[c2]) { visited[c2] = true; bfs.push(c2); } } } /* TODO: FIGURE OUT HOW TO DO LINEAR TOTALDRIP COMPUTATION queue<int> starting_points; for(int i = 0; i < n; ++i) { if(parents[i] == 0) { starting_points.push(i); } } while (!starting_points.empty()) { int t = starting_points.front(); starting_points.pop(); int c = child[t][0]; if (c != -1) { totaldrips[c] = 1 + totaldrips[t]; } } */ // for (int i = 0; i < n; ++i) { // printf("TOTALDRIPS %d: %d\n", i, totaldrips[i]); // } long long totalresult = 0; // each kranik will be turned on 1/((1+totaldrips)!) times on average // so multiplying by all permutations // n! / (k - n + 1)! for (int i = 0; i < n; ++i) { long long revq = 1+totaldrips[i]; // actual contribution is 1/revq long long q = odw_modulo(revq); totalresult += q; totalresult %= MODULO; /* int conq = factorials[1+totaldrips[i]]; int contributes = factorials[totaldrips[i]]; printf("%d will contribute %d out of %d times\n", i, contributes, conq); */ } printf("%lld\n", totalresult); 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | #include <cstdio> #include <queue> using namespace std; int child[500001][2]; // left and right children of x int intervals[2000001]; // >1'000'000 are single nodes int parents[500001]; int totaldrips[500001]; int factorials[500001]; int visited[500001]; const int MAX_IDX = 1048575; // 2^20 - 1 const long long MODULO = 1'000'000'007LL; void all_factorials(int n) { long long res = 1; factorials[0] = 1; factorials[1] = 1; for (int i = 2; i <= n; ++i) { res *= i; res %= 1'000'000'007LL; factorials[i] = int(res); } } long long odw_modulo(long long k) { long long r = MODULO; long long newr = k; long long t = 0; long long newt = 1; long long temp; while (newr != 0) { long long quotient = r / newr; temp = newt; newt = t - (quotient * newt); t = temp; temp = newr; newr = r - (quotient * newr); r = temp; } while (t < 0) { t += MODULO; } return t; } void setInterval(int l, int r, int cl, int cr, int x, int n) { // printf("SETTING INTERVAL %8d %8d | %8d %8d | to %d\n", cl, cr, l, r, n); // l, r - full required interval // cl, cr - interval considered in this iteration // x - tree index // n - value to set if (cl >= l && cr <= r) { // printf(" FULL INTERVAL COVERED\n"); intervals[x] = n; } else if (cl > r || cr < l) { // printf(" NO COMMON POINTS\n"); // no common points return; } else { // printf(" SPLITTING\n"); // must set right and left int lhalf_end = (cl + cr) / 2; int lhalf_idx = x * 2 + 1; if (intervals[x] != -2) { intervals[lhalf_idx] = intervals[x]; intervals[lhalf_idx+1] = intervals[x]; intervals[x] = -2; } setInterval(l, r, cl, lhalf_end, lhalf_idx, n); setInterval(l, r, lhalf_end+1, cr, lhalf_idx+1, n); } } void setIntervalStart(int l, int r, int n) { setInterval(l, r, 0, MAX_IDX, 0, n); } int getValue(int l, int r, int x, int k) { // printf("CHECKING INTERVAL %8d %8d (val: %d) | %d | \n", l, r, intervals[x], k); // l, r - current searched interval // x - tree index // k - required key if (intervals[x] != -2) { // printf("WHOLE INTERVAL HAS VALUE %d\n", intervals[x]); return intervals[x]; } // else go deeper int lhalf_end = (l + r) / 2; int lhalf_idx = x * 2 + 1; if (k <= lhalf_end) { return getValue(l, lhalf_end, lhalf_idx, k); } else { return getValue(lhalf_end+1, r, lhalf_idx+1, k); } } int getValueStart(int k) { return getValue(0, MAX_IDX, 0, k); } int main() { int n; scanf("%d", &n); all_factorials(n); intervals[0] = -1; // -1 = floor, -2 = split for (int i = 0; i < n; ++i) { int l, r; scanf("%d %d", &l, &r); child[i][0] = getValueStart(l-1); child[i][1] = getValueStart(r-1); if (child[i][0] == child[i][1]) { child[i][1] = -1; } // parents[child[i][0]] ++; // parents[child[i][1]] ++; setIntervalStart(l-1, r-1, i); // printf("PLATFORM %d: L: %d, R: %d\n", i, child[i][0], child[i][1]); } // TODO: FIGURE OUT HOW TO DO LINEAR TOTALDRIP COMPUTATION queue<int> bfs; for (int i = 0; i < n; ++i) { for (int j = 0; j < i; ++j) { visited[j] = 0; } int mc1 = child[i][0]; int mc2 = child[i][1]; if (mc1 != -1 && !visited[mc1]) { visited[mc1] = true; bfs.push(mc1); } if (mc2 != -1 && !visited[mc2]) { visited[mc2] = true; bfs.push(mc2); } while (!bfs.empty()) { int c = bfs.front(); bfs.pop(); if (c == -1) { continue; } // printf("%d gets a drip from %d\n", c, i); totaldrips[c]++; int c1 = child[c][0]; int c2 = child[c][1]; if (c1 != -1 && !visited[c1]) { visited[c1] = true; bfs.push(c1); } if (c2 != -1 && !visited[c2]) { visited[c2] = true; bfs.push(c2); } } } /* TODO: FIGURE OUT HOW TO DO LINEAR TOTALDRIP COMPUTATION queue<int> starting_points; for(int i = 0; i < n; ++i) { if(parents[i] == 0) { starting_points.push(i); } } while (!starting_points.empty()) { int t = starting_points.front(); starting_points.pop(); int c = child[t][0]; if (c != -1) { totaldrips[c] = 1 + totaldrips[t]; } } */ // for (int i = 0; i < n; ++i) { // printf("TOTALDRIPS %d: %d\n", i, totaldrips[i]); // } long long totalresult = 0; // each kranik will be turned on 1/((1+totaldrips)!) times on average // so multiplying by all permutations // n! / (k - n + 1)! for (int i = 0; i < n; ++i) { long long revq = 1+totaldrips[i]; // actual contribution is 1/revq long long q = odw_modulo(revq); totalresult += q; totalresult %= MODULO; /* int conq = factorials[1+totaldrips[i]]; int contributes = factorials[totaldrips[i]]; printf("%d will contribute %d out of %d times\n", i, contributes, conq); */ } printf("%lld\n", totalresult); return 0; } |