#include "poszukiwania.h" #include "message.h" #include <vector> #include <cstdio> using namespace std; const long long p1 = 901133; const long long p2 = 176347; const long long q = 1000000009; vector<long long> hs1, hs2, hm1, hm2; vector<long long> dhm1, dhm2; long long obliczL(long long caly, long long n, long long i) { long long r = caly % n; if (i < r) return caly / n + 1; return caly / n; } long long start(long long caly, long long n, long long i) { long long r = caly % n; if (i < r) return i * (caly / n + 1); return i * (caly / n) + r; } long long potega(long long a, long long b) { long long i; long long result = 1; long long x = a % q; for (i = 1; i <= b; i <<= 1) { x %= q; if ((b & i) != 0) { result *= x; result %= q; } x *= x; } return result; } int main() { long long n = NumberOfNodes(); long long i = MyNodeId(); long long s = SignalLength(); long long m = SeqLength(); long long ls = obliczL(s, n, i); long long startS = start(s, n, i); long long lm = obliczL(m, n, i); long long startM = start(m, n ,i); hs1.push_back(0); hs2.push_back(0); hs1.push_back(SignalAt(1 + startS)); hs2.push_back(SignalAt(1 + startS)); for (long long j = 1; j < ls; j++) { hs1.push_back((p1 * hs1[j] + SignalAt(1 + startS + j)) % q); hs2.push_back((p2 * hs2[j] + SignalAt(1 + startS + j)) % q); } hm1.push_back(0); hm2.push_back(0); hm1.push_back(SignalAt(1 + startM)); hm2.push_back(SignalAt(1 + startM)); if (1 + startM + s <= m) { dhm1.push_back(0); dhm2.push_back(0); dhm1.push_back(SignalAt(1 + startM + s)); dhm2.push_back(SignalAt(1 + startM + s)); } for (long long j = 1; j < lm; j++) { hm1.push_back((p1 * hm1[j] + SignalAt(1 + startM + j)) % q); hm2.push_back((p2 * hm2[j] + SignalAt(1 + startM + j)) % q); if (1 + startM + j + s <= m) { dhm1.push_back((p1 * dhm1[j] + SignalAt(1 + startM + j + s)) % q); dhm2.push_back((p2 * dhm2[j] + SignalAt(1 + startM + j + s)) % q); } } long long pHs1 = 0, pHs2 = 0, pHm1 = 0, pHm2 = 0, dpHm1, dpHm2; if (i > 0) { Receive(i - 1); pHs1 = GetLL(i - 1); pHs2 = GetLL(i - 1); pHm1 = GetLL(i - 1); pHm2 = GetLL(i - 1); } long long fHs1, fHs2, fHm1, fHm2; fHs1 = (potega(p1, ls) * pHs1 + hs1[ls]) % q; fHs2 = (potega(p2, ls) * pHs2 + hs2[ls]) % q; fHm1 = (potega(p1, lm) * pHm1 + hm1[lm]) % q; fHm2 = (potega(p2, lm) * pHm2 + hm2[lm]) % q; if (i < n - 1) { PutLL(i + 1, fHs1); PutLL(i + 1, fHs2); PutLL(i + 1, fHm1); PutLL(i + 1, fHm2); Send(i + 1); } if (i == n - 1) { PutLL(0, fHs1); PutLL(0, fHs2); Send(0); } long long aHs1, aHs2; Receive((i - 1 + n) % n); aHs1 = GetLL((i - 1 + n) % n); aHs2 = GetLL((i - 1 + n) % n); if (i < n - 1) { PutLL(i + 1, aHs1); PutLL(i + 1, aHs2); Send(i + 1); } if (startM <= s && s < startM + lm) { PutLL(0, (potega(p1, s - startM + 1) * pHm1 + hm1[s - startM + 1]) % q); PutLL(0, (potega(p2, s - startM + 1) * pHm2 + hm2[s - startM + 1]) % q); Send(0); } if (i == 0) { long long k = m % n; long long j; if (s < (m / n + 1) * k) j = s / (m / n + 1); else j = (s - k) / (m / n); Receive(j); dpHm1 = GetLL(j); dpHm2 = GetLL(j); } if (1 + startM + s <= m && i > 0) { dpHm1 = GetLL(i - 1); dpHm2 = GetLL(i - 1); } if (1 + startM + lm + s <= m) { PutLL(i + 1, dpHm1); PutLL(i + 1, dpHm2); } long long suma = 0; for (int j = 0; j < lm; j++) { if (1 + startM + j + s > m) break; long long a, b, c, d; a = (potega(p1, j) * pHm1 + hm1[j]) % q; b = (potega(p1, j) * dpHm1 + dhm1[j]) % q; c = (potega(p2, j) * pHm2 + hm2[j]) % q; d = (potega(p2, j) * dpHm2 + dhm2[j]) % q; if ((b - a + q) % q == aHs1 && (d - c + q) % q == aHs2) suma++; } if (i != 0) { PutLL(0, suma); Send(0); } else { Receive(-1); for (int w = 1; w < n; w++) suma += GetLL(w); printf("%lld\n", suma); } }
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 | #include "poszukiwania.h" #include "message.h" #include <vector> #include <cstdio> using namespace std; const long long p1 = 901133; const long long p2 = 176347; const long long q = 1000000009; vector<long long> hs1, hs2, hm1, hm2; vector<long long> dhm1, dhm2; long long obliczL(long long caly, long long n, long long i) { long long r = caly % n; if (i < r) return caly / n + 1; return caly / n; } long long start(long long caly, long long n, long long i) { long long r = caly % n; if (i < r) return i * (caly / n + 1); return i * (caly / n) + r; } long long potega(long long a, long long b) { long long i; long long result = 1; long long x = a % q; for (i = 1; i <= b; i <<= 1) { x %= q; if ((b & i) != 0) { result *= x; result %= q; } x *= x; } return result; } int main() { long long n = NumberOfNodes(); long long i = MyNodeId(); long long s = SignalLength(); long long m = SeqLength(); long long ls = obliczL(s, n, i); long long startS = start(s, n, i); long long lm = obliczL(m, n, i); long long startM = start(m, n ,i); hs1.push_back(0); hs2.push_back(0); hs1.push_back(SignalAt(1 + startS)); hs2.push_back(SignalAt(1 + startS)); for (long long j = 1; j < ls; j++) { hs1.push_back((p1 * hs1[j] + SignalAt(1 + startS + j)) % q); hs2.push_back((p2 * hs2[j] + SignalAt(1 + startS + j)) % q); } hm1.push_back(0); hm2.push_back(0); hm1.push_back(SignalAt(1 + startM)); hm2.push_back(SignalAt(1 + startM)); if (1 + startM + s <= m) { dhm1.push_back(0); dhm2.push_back(0); dhm1.push_back(SignalAt(1 + startM + s)); dhm2.push_back(SignalAt(1 + startM + s)); } for (long long j = 1; j < lm; j++) { hm1.push_back((p1 * hm1[j] + SignalAt(1 + startM + j)) % q); hm2.push_back((p2 * hm2[j] + SignalAt(1 + startM + j)) % q); if (1 + startM + j + s <= m) { dhm1.push_back((p1 * dhm1[j] + SignalAt(1 + startM + j + s)) % q); dhm2.push_back((p2 * dhm2[j] + SignalAt(1 + startM + j + s)) % q); } } long long pHs1 = 0, pHs2 = 0, pHm1 = 0, pHm2 = 0, dpHm1, dpHm2; if (i > 0) { Receive(i - 1); pHs1 = GetLL(i - 1); pHs2 = GetLL(i - 1); pHm1 = GetLL(i - 1); pHm2 = GetLL(i - 1); } long long fHs1, fHs2, fHm1, fHm2; fHs1 = (potega(p1, ls) * pHs1 + hs1[ls]) % q; fHs2 = (potega(p2, ls) * pHs2 + hs2[ls]) % q; fHm1 = (potega(p1, lm) * pHm1 + hm1[lm]) % q; fHm2 = (potega(p2, lm) * pHm2 + hm2[lm]) % q; if (i < n - 1) { PutLL(i + 1, fHs1); PutLL(i + 1, fHs2); PutLL(i + 1, fHm1); PutLL(i + 1, fHm2); Send(i + 1); } if (i == n - 1) { PutLL(0, fHs1); PutLL(0, fHs2); Send(0); } long long aHs1, aHs2; Receive((i - 1 + n) % n); aHs1 = GetLL((i - 1 + n) % n); aHs2 = GetLL((i - 1 + n) % n); if (i < n - 1) { PutLL(i + 1, aHs1); PutLL(i + 1, aHs2); Send(i + 1); } if (startM <= s && s < startM + lm) { PutLL(0, (potega(p1, s - startM + 1) * pHm1 + hm1[s - startM + 1]) % q); PutLL(0, (potega(p2, s - startM + 1) * pHm2 + hm2[s - startM + 1]) % q); Send(0); } if (i == 0) { long long k = m % n; long long j; if (s < (m / n + 1) * k) j = s / (m / n + 1); else j = (s - k) / (m / n); Receive(j); dpHm1 = GetLL(j); dpHm2 = GetLL(j); } if (1 + startM + s <= m && i > 0) { dpHm1 = GetLL(i - 1); dpHm2 = GetLL(i - 1); } if (1 + startM + lm + s <= m) { PutLL(i + 1, dpHm1); PutLL(i + 1, dpHm2); } long long suma = 0; for (int j = 0; j < lm; j++) { if (1 + startM + j + s > m) break; long long a, b, c, d; a = (potega(p1, j) * pHm1 + hm1[j]) % q; b = (potega(p1, j) * dpHm1 + dhm1[j]) % q; c = (potega(p2, j) * pHm2 + hm2[j]) % q; d = (potega(p2, j) * dpHm2 + dhm2[j]) % q; if ((b - a + q) % q == aHs1 && (d - c + q) % q == aHs2) suma++; } if (i != 0) { PutLL(0, suma); Send(0); } else { Receive(-1); for (int w = 1; w < n; w++) suma += GetLL(w); printf("%lld\n", suma); } } |