#include <algorithm> #include <cstdio> // #include "pos/poszukiwania.h" #include "poszukiwania.h" #include "message.h" using namespace std; typedef unsigned long long LL; const LL P1 = 1000000009; const LL P2 = 1000000007; const LL MOD = 2000000137; const int MAX_NODES = 100; int n, k; int nodes, id; pair<LL, LL> pattern_hash; LL text_hash1[MAX_NODES]; LL text_hash2[MAX_NODES]; LL p_text1, p_text2; pair<LL, LL> compute_pattern() { k = SignalLength(); LL len = (k + nodes - 1) / nodes; LL p1 = 1, p2 = 1; LL h1 = 0, h2 = 0; for (LL i=len*id;i<min((LL)k, len*id+len);i++) { LL s = SignalAt(i+1); h1 += p1 * s; p1 *= P1; h2 = (h2 + p2 * s) % MOD; p2 = (p2 * P2) % MOD; } if (id > 0) { PutLL(0, h1); PutLL(0, h2); Send(0); Receive(0); h1 = GetLL(0); h2 = GetLL(0); return {h1, h2}; } LL pp1 = p1; LL pp2 = p2; for (int i=1;i<nodes;i++) { Receive(i); LL hh1 = GetLL(i); h1 += pp1 * hh1; pp1 *= p1; LL hh2 = GetLL(i); h2 = (h2 + pp2 * hh2) % MOD; pp2 = (pp2 * p2) % MOD; } for (int i=1;i<nodes;i++) { PutLL(i, h1); PutLL(i, h2); Send(i); } return {h1, h2}; } void compute_text() { n = SeqLength(); LL len = (n + nodes - 1) / nodes; LL p1 = 1, p2 = 1; LL h1 = 0, h2 = 0; for (LL i=len*id;i<min((LL)n, len*id+len);i++) { LL s = SeqAt(i+1); h1 += p1 * s; p1 *= P1; h2 = (h2 + p2 * s) % MOD; p2 = (p2 * P2) % MOD; } p_text1 = p1; p_text2 = p2; text_hash1[id] = h1; text_hash2[id] = h2; for (int i=0;i<nodes;i++) if (i != id) { PutLL(i, h1); PutLL(i, h2); Send(i); } for (int i=0;i<nodes;i++) if (i != id) { Receive(i); text_hash1[i] = GetLL(i); text_hash2[i] = GetLL(i); } } int solve() { LL len = (n + nodes - 1) / nodes; LL start = len * id; if (start > n-k) return 0; LL end = min((LL)n - k, start + len); int ret = 0; LL p1 = 1, p2 = 1; LL h1 = 0, h2 = 0; for (int i=0;i<k/len;i++) { h1 += p1 * text_hash1[id+i]; p1 *= p_text1; h2 = (h2 + p2 * text_hash2[id+i]) % MOD; p2 = (p2 * p_text2) % MOD; } for (int i=k/len*len;i<k;i++) { LL s = SeqAt(start+i+1); h1 += p1 * s; p1 *= P1; h2 = (h2 + p2 * s) % MOD; p2 = (p2 * P2) % MOD; } ret += pair<LL, LL>{h1, h2} == pattern_hash; LL first_p1 = 1, first_p2 = 1; for (LL i=start+1;i<end;i++) { LL si = SeqAt(i); LL sk = SeqAt(i+k); h1 -= first_p1 * si; first_p1 *= P1; pattern_hash.first *= P1; h1 += p1 * sk; p1 *= P1; h2 = (h2 + MOD - (first_p2 * si % MOD)) % MOD; first_p2 = (first_p2 * P2) % MOD; pattern_hash.second = (pattern_hash.second * P2) % MOD; h2 = (h2 + p2 * sk) % MOD; p2 = (p2 * P2) % MOD; ret += pair<LL, LL>{h1, h2} == pattern_hash; } return ret; } int main() { nodes = NumberOfNodes(); id = MyNodeId(); pattern_hash = compute_pattern(); compute_text(); int res = solve(); if (id == 0) { for (int foo=1;foo<nodes;foo++) { int s = Receive(-1); res += GetInt(s); } printf("%d\n", res); } else { PutInt(0, res); Send(0); } 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 | #include <algorithm> #include <cstdio> // #include "pos/poszukiwania.h" #include "poszukiwania.h" #include "message.h" using namespace std; typedef unsigned long long LL; const LL P1 = 1000000009; const LL P2 = 1000000007; const LL MOD = 2000000137; const int MAX_NODES = 100; int n, k; int nodes, id; pair<LL, LL> pattern_hash; LL text_hash1[MAX_NODES]; LL text_hash2[MAX_NODES]; LL p_text1, p_text2; pair<LL, LL> compute_pattern() { k = SignalLength(); LL len = (k + nodes - 1) / nodes; LL p1 = 1, p2 = 1; LL h1 = 0, h2 = 0; for (LL i=len*id;i<min((LL)k, len*id+len);i++) { LL s = SignalAt(i+1); h1 += p1 * s; p1 *= P1; h2 = (h2 + p2 * s) % MOD; p2 = (p2 * P2) % MOD; } if (id > 0) { PutLL(0, h1); PutLL(0, h2); Send(0); Receive(0); h1 = GetLL(0); h2 = GetLL(0); return {h1, h2}; } LL pp1 = p1; LL pp2 = p2; for (int i=1;i<nodes;i++) { Receive(i); LL hh1 = GetLL(i); h1 += pp1 * hh1; pp1 *= p1; LL hh2 = GetLL(i); h2 = (h2 + pp2 * hh2) % MOD; pp2 = (pp2 * p2) % MOD; } for (int i=1;i<nodes;i++) { PutLL(i, h1); PutLL(i, h2); Send(i); } return {h1, h2}; } void compute_text() { n = SeqLength(); LL len = (n + nodes - 1) / nodes; LL p1 = 1, p2 = 1; LL h1 = 0, h2 = 0; for (LL i=len*id;i<min((LL)n, len*id+len);i++) { LL s = SeqAt(i+1); h1 += p1 * s; p1 *= P1; h2 = (h2 + p2 * s) % MOD; p2 = (p2 * P2) % MOD; } p_text1 = p1; p_text2 = p2; text_hash1[id] = h1; text_hash2[id] = h2; for (int i=0;i<nodes;i++) if (i != id) { PutLL(i, h1); PutLL(i, h2); Send(i); } for (int i=0;i<nodes;i++) if (i != id) { Receive(i); text_hash1[i] = GetLL(i); text_hash2[i] = GetLL(i); } } int solve() { LL len = (n + nodes - 1) / nodes; LL start = len * id; if (start > n-k) return 0; LL end = min((LL)n - k, start + len); int ret = 0; LL p1 = 1, p2 = 1; LL h1 = 0, h2 = 0; for (int i=0;i<k/len;i++) { h1 += p1 * text_hash1[id+i]; p1 *= p_text1; h2 = (h2 + p2 * text_hash2[id+i]) % MOD; p2 = (p2 * p_text2) % MOD; } for (int i=k/len*len;i<k;i++) { LL s = SeqAt(start+i+1); h1 += p1 * s; p1 *= P1; h2 = (h2 + p2 * s) % MOD; p2 = (p2 * P2) % MOD; } ret += pair<LL, LL>{h1, h2} == pattern_hash; LL first_p1 = 1, first_p2 = 1; for (LL i=start+1;i<end;i++) { LL si = SeqAt(i); LL sk = SeqAt(i+k); h1 -= first_p1 * si; first_p1 *= P1; pattern_hash.first *= P1; h1 += p1 * sk; p1 *= P1; h2 = (h2 + MOD - (first_p2 * si % MOD)) % MOD; first_p2 = (first_p2 * P2) % MOD; pattern_hash.second = (pattern_hash.second * P2) % MOD; h2 = (h2 + p2 * sk) % MOD; p2 = (p2 * P2) % MOD; ret += pair<LL, LL>{h1, h2} == pattern_hash; } return ret; } int main() { nodes = NumberOfNodes(); id = MyNodeId(); pattern_hash = compute_pattern(); compute_text(); int res = solve(); if (id == 0) { for (int foo=1;foo<nodes;foo++) { int s = Receive(-1); res += GetInt(s); } printf("%d\n", res); } else { PutInt(0, res); Send(0); } return 0; } |