#include <algorithm> #include <iostream> #include <cstdio> #include <cstdlib> #include <vector> int c; #define SKIP_WHITESPACE \ { \ while (1) { \ c = fgetc(stdin); \ if (c != ' ' && c != '\n' && c != '\r') \ break; \ } \ } \ #define READ_INT \ ({ \ SKIP_WHITESPACE \ int ret = c - '0'; \ while (1) { \ c = fgetc(stdin); \ if (c < '0' || c > '9') \ break; \ ret = ret * 10 + c - '0'; \ } \ ret; \ }) \ #define MOD 1000000007ULL struct bulb { int init; int witches; int kol; }; struct witch { int par; int next; }; uint64_t powers[20]; uint64_t power(int pn) { int pi = 0; uint64_t pret = 1ULL; while (pn > 0) { if (pn & 1) { pret = (pret * powers[pi]) % MOD; } pi++; pn = pn >> 1; } return pret; } uint64_t inv(uint64_t inva) { return inva <= 1 ? inva : MOD - (uint64_t)(MOD / inva) * inv(MOD % inva) % MOD; } uint64_t newton(int nn, int nk) { if (nk > (nn - nk)) nk = nn - nk; if (nk == 0) return 1ULL; if (nk == 1) return uint64_t(nn); uint64_t top = nn; uint64_t bottom = nk; while ( nk > 1 ) { nk--; top = (top * (nn - nk)) % MOD; bottom = (bottom * nk) % MOD; } return (top * inv(bottom)) % MOD; } int main(int argc, char* argv[]) { std::ios_base::sync_with_stdio (false); int i, t, a, b, w, n, m; powers[0] = 2ULL; for (i = 1; i < 20; ++i) { powers[i] = (powers[i - 1] * powers[i - 1]) % MOD; } n = READ_INT; m = READ_INT; std::vector<bulb> bulbs(n); std::vector<witch> witches(m * 2); int wi = 0; std::vector<int> stack(n); int si = 0; for (i = 0; i < n; ++i) { t = READ_INT; bulbs[i] = {t, -1, -1}; } for (i = 0; i < m; ++i) { a = READ_INT - 1; b = READ_INT - 1; witches[wi] = {b, bulbs[a].witches}; bulbs[a].witches = wi; wi++; witches[wi] = {a, bulbs[b].witches}; bulbs[b].witches = wi; wi++; } // for (i = 0; i < n; ++i) { // std::cout << "bulb: " << i << "; init=" << bulbs[i].init << "; switches={"; // w = bulbs[i].witches; // while (w != -1) { // std::cout << witches[w].par << ", "; // w = witches[w].next; // } // std::cout << "}\n"; // } int kol, z[2], s[2], idx, par, oddcycle; uint64_t ret = 1ULL, klik; for (i = 0; i < n; ++i) { if (bulbs[i].kol != -1) continue; bulbs[i].kol = 0; z[0] = 1; z[1] = 0; s[0] = bulbs[i].init; s[1] = 0; oddcycle = 0; stack[0] = i; si = 1; while (si > 0) { idx = stack[--si]; w = bulbs[idx].witches; kol = bulbs[idx].kol ^ 1; while (w != -1) { par = witches[w].par; if (bulbs[par].kol == -1) { bulbs[par].kol = kol; z[kol]++; s[kol] += bulbs[par].init; stack[si++] = par; } else if (bulbs[par].kol == bulbs[idx].kol) { oddcycle = 1; } w = witches[w].next; } } if (oddcycle) { klik = power(z[0] + z[1] - 1); } else { klik = newton(z[0] + z[1], z[0] - s[0] + s[1]); } ret = (ret * klik) % MOD; // std::cout << "oc=" << oddcycle << "; z=" << z[0] << "; c=" << z[1] << "; s=" << s[0] << "; p=" << s[1] << "; klik=" << klik << "\n"; } std::cout << ret; std::cout << "\n"; return EXIT_SUCCESS; }
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 | #include <algorithm> #include <iostream> #include <cstdio> #include <cstdlib> #include <vector> int c; #define SKIP_WHITESPACE \ { \ while (1) { \ c = fgetc(stdin); \ if (c != ' ' && c != '\n' && c != '\r') \ break; \ } \ } \ #define READ_INT \ ({ \ SKIP_WHITESPACE \ int ret = c - '0'; \ while (1) { \ c = fgetc(stdin); \ if (c < '0' || c > '9') \ break; \ ret = ret * 10 + c - '0'; \ } \ ret; \ }) \ #define MOD 1000000007ULL struct bulb { int init; int witches; int kol; }; struct witch { int par; int next; }; uint64_t powers[20]; uint64_t power(int pn) { int pi = 0; uint64_t pret = 1ULL; while (pn > 0) { if (pn & 1) { pret = (pret * powers[pi]) % MOD; } pi++; pn = pn >> 1; } return pret; } uint64_t inv(uint64_t inva) { return inva <= 1 ? inva : MOD - (uint64_t)(MOD / inva) * inv(MOD % inva) % MOD; } uint64_t newton(int nn, int nk) { if (nk > (nn - nk)) nk = nn - nk; if (nk == 0) return 1ULL; if (nk == 1) return uint64_t(nn); uint64_t top = nn; uint64_t bottom = nk; while ( nk > 1 ) { nk--; top = (top * (nn - nk)) % MOD; bottom = (bottom * nk) % MOD; } return (top * inv(bottom)) % MOD; } int main(int argc, char* argv[]) { std::ios_base::sync_with_stdio (false); int i, t, a, b, w, n, m; powers[0] = 2ULL; for (i = 1; i < 20; ++i) { powers[i] = (powers[i - 1] * powers[i - 1]) % MOD; } n = READ_INT; m = READ_INT; std::vector<bulb> bulbs(n); std::vector<witch> witches(m * 2); int wi = 0; std::vector<int> stack(n); int si = 0; for (i = 0; i < n; ++i) { t = READ_INT; bulbs[i] = {t, -1, -1}; } for (i = 0; i < m; ++i) { a = READ_INT - 1; b = READ_INT - 1; witches[wi] = {b, bulbs[a].witches}; bulbs[a].witches = wi; wi++; witches[wi] = {a, bulbs[b].witches}; bulbs[b].witches = wi; wi++; } // for (i = 0; i < n; ++i) { // std::cout << "bulb: " << i << "; init=" << bulbs[i].init << "; switches={"; // w = bulbs[i].witches; // while (w != -1) { // std::cout << witches[w].par << ", "; // w = witches[w].next; // } // std::cout << "}\n"; // } int kol, z[2], s[2], idx, par, oddcycle; uint64_t ret = 1ULL, klik; for (i = 0; i < n; ++i) { if (bulbs[i].kol != -1) continue; bulbs[i].kol = 0; z[0] = 1; z[1] = 0; s[0] = bulbs[i].init; s[1] = 0; oddcycle = 0; stack[0] = i; si = 1; while (si > 0) { idx = stack[--si]; w = bulbs[idx].witches; kol = bulbs[idx].kol ^ 1; while (w != -1) { par = witches[w].par; if (bulbs[par].kol == -1) { bulbs[par].kol = kol; z[kol]++; s[kol] += bulbs[par].init; stack[si++] = par; } else if (bulbs[par].kol == bulbs[idx].kol) { oddcycle = 1; } w = witches[w].next; } } if (oddcycle) { klik = power(z[0] + z[1] - 1); } else { klik = newton(z[0] + z[1], z[0] - s[0] + s[1]); } ret = (ret * klik) % MOD; // std::cout << "oc=" << oddcycle << "; z=" << z[0] << "; c=" << z[1] << "; s=" << s[0] << "; p=" << s[1] << "; klik=" << klik << "\n"; } std::cout << ret; std::cout << "\n"; return EXIT_SUCCESS; } |