// Krzysztof Małysa #include <bits/stdc++.h> #include <unistd.h> using namespace std; #define FOR(i,a,n) for (int i = (a), __n ## i = n; i < __n ## i; ++i) #define REP(i,n) FOR(i,0,n) #define FORD(i,a,n) for (int i = (a), __n ## i = n; i >= __n ## i; --i) #define LET(x,a) __typeof(a) x = (a) #define FOREACH(i,x) for (LET(i, x.begin()), __n##i = x.end(); i != __n##i; ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) (int(x.size())) #define eprintf(...) fprintf(stderr, __VA_ARGS__) #define ST first #define ND second #define MP make_pair #define PB push_back #define O(...) ostream& operator <<(ostream& os, const __VA_ARGS__& x) #define OO(...) O(__VA_ARGS__) { return __out(os, ALL(x)); } #define T template #define CL class typedef unsigned uint; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<LL> VLL; typedef pair<int, int> PII; typedef vector<PII> VPII; typedef vector<VPII> VVPII; typedef pair<LL, LL> PLLLL; typedef vector<PLLLL> VPLLLL; typedef vector<bool> VB; typedef vector<char> VC; typedef vector<string> VS; T<CL A> inline A abs(const A& a) { return a < A() ? -a : a; } T<CL A, CL B> inline void mini(A& a, const B& b) { if (b < a) a = b; } T<CL A, CL B> inline void maxi(A& a, const B& b) { if (b > a) a = b; } T<CL Iter> ostream& __out(ostream& os, Iter a, Iter b, const string& s = ", "); T<CL A, CL B> O(pair<A, B>); T<CL A> OO(vector<A>) T<CL A> OO(deque<A>) T<CL A> OO(list<A>) T<CL A, CL B> OO(set<A, B>) T<CL A, CL B, CL C> OO(map<A, B, C>) T<CL A, CL B> OO(multiset<A, B>) T<CL A, CL B, CL C> OO(multimap<A, B, C>) T<CL Iter> ostream& __out(ostream& os, Iter a, Iter b, const string& s) { os << "{"; if (a != b) { os << *a; while (++a != b) os << s << *a; } return os << "}"; } T<CL A, CL B> O(pair<A, B>) { return os << "(" << x.ST << ", " << x.ND << ")"; } CL Input { static const int BUFF_SIZE = 1 << 16; unsigned char buff[BUFF_SIZE], *pos, *end; void grabBuffer() { pos = buff; end = buff + read(0, buff, BUFF_SIZE); } public: Input() : pos(buff), end(buff) {} int peek() { if (pos == end) grabBuffer(); return pos != end ? *pos : -1; } int getChar() { if (pos == end) grabBuffer(); return pos != end ? *pos++ : -1; } void skipWhiteSpaces() { while (isspace(peek())) ++pos; } T<CL A> A get(); void operator()(char* s) { skipWhiteSpaces(); while (!isspace(peek())) *s++ = *pos++; *s = '\0'; } T<CL A> void operator()(A& x) { x = get<A>(); } T<CL A, CL B> void operator()(A& a, B& b) { operator()(a), operator()(b); } T<CL A, CL B, CL C> void operator()(A& a, B& b, C& c) { operator()(a, b), operator()(c); } T<CL A, CL B, CL C, CL D> void operator()(A& a, B& b, C& c, D& d) { operator()(a, b, c); operator()(d); } T<CL A, CL B, CL C, CL D, CL E> void operator()(A& a, B& b, C& c, D& d, E& e) { operator()(a, b, c, d); operator()(e); } T<CL A, CL B, CL C, CL D, CL E, CL F> void operator()(A& a, B& b, C& c, D& d, E& e, F& f) { operator()(a, b, c, d, e); operator()(f); } Input& operator>>(char *s) { operator()(s); return *this; } T<CL A> Input& operator>>(A& a) { operator()(a); return *this; } } fin; T<> char Input::get<char>() { skipWhiteSpaces(); return *pos++; } T<> uint Input::get<uint>() { skipWhiteSpaces(); uint x = 0; while (isdigit(peek())) x = x * 10 + *pos++ - '0'; return x; } T<> int Input::get<int>() { skipWhiteSpaces(); return peek() == '-' ? (++pos, -get<uint>()) : get<uint>(); } T<> ULL Input::get<ULL>() { skipWhiteSpaces(); ULL x = 0; while (isdigit(peek())) x = x * 10 + *pos++ - '0'; return x; } T<> LL Input::get<LL>() { skipWhiteSpaces(); return peek() == '-' ? (++pos, -get<ULL>()) : get<ULL>(); } T<> string Input::get<string>() { skipWhiteSpaces(); string x; while (!isspace(peek())) x += *pos++; return x; } int ceil2(int x) { return 1 << (sizeof(x) * 8 -__builtin_clz(x - 1)); } #undef T #undef CL #ifdef DEBUG # define D(...) __VA_ARGS__ # define E(...) eprintf(__VA_ARGS__) # define OUT(a,b) cerr << #a ":", __out(cerr, a, b), E("\n") # define LOG(x) cerr << #x ": " << (x) # define LOG2(x, y) cerr << x ": " << (y) # define LOGN(x) LOG(x) << endl # define LOGN2(x, y) LOG2(x, y) << endl #else # define D(...) # define E(...) # define OUT(...) # define LOG(...) # define LOG2(...) # define LOGN(...) # define LOGN2(...) #endif /// End of templates #include "message.h" #include "poszukiwania.h" int main() { int s = SignalLength(), m = SeqLength(), nid = MyNodeId(); int nofinst = NumberOfNodes(); // assert(m >= s); int P[s], k = 0; P[0] = 0; FOR (i, 1, s) { while (k && SignalAt(i + 1) != SignalAt(k + 1)) k = P[k - 1]; if (SignalAt(i + 1) == SignalAt(k + 1)) ++k; P[i] = k; } k = 0; int res = 0; int beg = max(LL(m) * nid / nofinst - s + 1, 0LL); int end = LL(m) * (nid + 1) / nofinst; FOR (i, beg, end) { while (k && SeqAt(i + 1) != SignalAt(k + 1)) k = P[k - 1]; if (SeqAt(i + 1) == SignalAt(k + 1) && ++k == s) { ++res; k = P[k - 1]; } } // eprintf("%i: <%i, %i) -> %i\n", nid, beg, end, res); if (nid != 0) { PutInt(0, res); Send(0); return 0; } // assert(MyNodeId() == 0) REP (i, nofinst - 1) { int inst = Receive(-1); res += GetInt(inst); } printf("%i\n", res); 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | // Krzysztof Małysa #include <bits/stdc++.h> #include <unistd.h> using namespace std; #define FOR(i,a,n) for (int i = (a), __n ## i = n; i < __n ## i; ++i) #define REP(i,n) FOR(i,0,n) #define FORD(i,a,n) for (int i = (a), __n ## i = n; i >= __n ## i; --i) #define LET(x,a) __typeof(a) x = (a) #define FOREACH(i,x) for (LET(i, x.begin()), __n##i = x.end(); i != __n##i; ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) (int(x.size())) #define eprintf(...) fprintf(stderr, __VA_ARGS__) #define ST first #define ND second #define MP make_pair #define PB push_back #define O(...) ostream& operator <<(ostream& os, const __VA_ARGS__& x) #define OO(...) O(__VA_ARGS__) { return __out(os, ALL(x)); } #define T template #define CL class typedef unsigned uint; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<LL> VLL; typedef pair<int, int> PII; typedef vector<PII> VPII; typedef vector<VPII> VVPII; typedef pair<LL, LL> PLLLL; typedef vector<PLLLL> VPLLLL; typedef vector<bool> VB; typedef vector<char> VC; typedef vector<string> VS; T<CL A> inline A abs(const A& a) { return a < A() ? -a : a; } T<CL A, CL B> inline void mini(A& a, const B& b) { if (b < a) a = b; } T<CL A, CL B> inline void maxi(A& a, const B& b) { if (b > a) a = b; } T<CL Iter> ostream& __out(ostream& os, Iter a, Iter b, const string& s = ", "); T<CL A, CL B> O(pair<A, B>); T<CL A> OO(vector<A>) T<CL A> OO(deque<A>) T<CL A> OO(list<A>) T<CL A, CL B> OO(set<A, B>) T<CL A, CL B, CL C> OO(map<A, B, C>) T<CL A, CL B> OO(multiset<A, B>) T<CL A, CL B, CL C> OO(multimap<A, B, C>) T<CL Iter> ostream& __out(ostream& os, Iter a, Iter b, const string& s) { os << "{"; if (a != b) { os << *a; while (++a != b) os << s << *a; } return os << "}"; } T<CL A, CL B> O(pair<A, B>) { return os << "(" << x.ST << ", " << x.ND << ")"; } CL Input { static const int BUFF_SIZE = 1 << 16; unsigned char buff[BUFF_SIZE], *pos, *end; void grabBuffer() { pos = buff; end = buff + read(0, buff, BUFF_SIZE); } public: Input() : pos(buff), end(buff) {} int peek() { if (pos == end) grabBuffer(); return pos != end ? *pos : -1; } int getChar() { if (pos == end) grabBuffer(); return pos != end ? *pos++ : -1; } void skipWhiteSpaces() { while (isspace(peek())) ++pos; } T<CL A> A get(); void operator()(char* s) { skipWhiteSpaces(); while (!isspace(peek())) *s++ = *pos++; *s = '\0'; } T<CL A> void operator()(A& x) { x = get<A>(); } T<CL A, CL B> void operator()(A& a, B& b) { operator()(a), operator()(b); } T<CL A, CL B, CL C> void operator()(A& a, B& b, C& c) { operator()(a, b), operator()(c); } T<CL A, CL B, CL C, CL D> void operator()(A& a, B& b, C& c, D& d) { operator()(a, b, c); operator()(d); } T<CL A, CL B, CL C, CL D, CL E> void operator()(A& a, B& b, C& c, D& d, E& e) { operator()(a, b, c, d); operator()(e); } T<CL A, CL B, CL C, CL D, CL E, CL F> void operator()(A& a, B& b, C& c, D& d, E& e, F& f) { operator()(a, b, c, d, e); operator()(f); } Input& operator>>(char *s) { operator()(s); return *this; } T<CL A> Input& operator>>(A& a) { operator()(a); return *this; } } fin; T<> char Input::get<char>() { skipWhiteSpaces(); return *pos++; } T<> uint Input::get<uint>() { skipWhiteSpaces(); uint x = 0; while (isdigit(peek())) x = x * 10 + *pos++ - '0'; return x; } T<> int Input::get<int>() { skipWhiteSpaces(); return peek() == '-' ? (++pos, -get<uint>()) : get<uint>(); } T<> ULL Input::get<ULL>() { skipWhiteSpaces(); ULL x = 0; while (isdigit(peek())) x = x * 10 + *pos++ - '0'; return x; } T<> LL Input::get<LL>() { skipWhiteSpaces(); return peek() == '-' ? (++pos, -get<ULL>()) : get<ULL>(); } T<> string Input::get<string>() { skipWhiteSpaces(); string x; while (!isspace(peek())) x += *pos++; return x; } int ceil2(int x) { return 1 << (sizeof(x) * 8 -__builtin_clz(x - 1)); } #undef T #undef CL #ifdef DEBUG # define D(...) __VA_ARGS__ # define E(...) eprintf(__VA_ARGS__) # define OUT(a,b) cerr << #a ":", __out(cerr, a, b), E("\n") # define LOG(x) cerr << #x ": " << (x) # define LOG2(x, y) cerr << x ": " << (y) # define LOGN(x) LOG(x) << endl # define LOGN2(x, y) LOG2(x, y) << endl #else # define D(...) # define E(...) # define OUT(...) # define LOG(...) # define LOG2(...) # define LOGN(...) # define LOGN2(...) #endif /// End of templates #include "message.h" #include "poszukiwania.h" int main() { int s = SignalLength(), m = SeqLength(), nid = MyNodeId(); int nofinst = NumberOfNodes(); // assert(m >= s); int P[s], k = 0; P[0] = 0; FOR (i, 1, s) { while (k && SignalAt(i + 1) != SignalAt(k + 1)) k = P[k - 1]; if (SignalAt(i + 1) == SignalAt(k + 1)) ++k; P[i] = k; } k = 0; int res = 0; int beg = max(LL(m) * nid / nofinst - s + 1, 0LL); int end = LL(m) * (nid + 1) / nofinst; FOR (i, beg, end) { while (k && SeqAt(i + 1) != SignalAt(k + 1)) k = P[k - 1]; if (SeqAt(i + 1) == SignalAt(k + 1) && ++k == s) { ++res; k = P[k - 1]; } } // eprintf("%i: <%i, %i) -> %i\n", nid, beg, end, res); if (nid != 0) { PutInt(0, res); Send(0); return 0; } // assert(MyNodeId() == 0) REP (i, nofinst - 1) { int inst = Receive(-1); res += GetInt(inst); } printf("%i\n", res); return 0; } /* */ |