#include <cstdio> #include "poszukiwania.h" #include "message.h" using namespace std; typedef unsigned long long ULL; const int N=1000*1000+7; const ULL P = 1000000009; const ULL Q = 2000000033; ULL* sig_hash; ULL pow (ULL a, ULL b) { a%=Q; ULL w = 1; for (; b; b>>=1, a=(a*a)%Q) if (b&1) w = (w*a)%Q; return w; } int main () { ULL instance_no = MyNodeId(); ULL instance_c = NumberOfNodes(); instance_c--; //bo zero nie pracuje ULL n = SignalLength(); //wzorzec ULL m = SeqLength(); //tekst if (instance_no == 0) { ULL hash = 0; for (int i=1; i<=instance_c; i++) { ULL beg = (n*(i-1))/instance_c; ULL end = (n*i)/instance_c; Receive(i); hash = (hash*pow(P, end-beg)+GetLL(i))%Q; } for (int i=1; i<=instance_c; i++) { PutLL(i, hash); Send(i); } ULL hash2 = 0; for (int i=1; i<=instance_c; i++) { PutLL(i, hash2); Send(i); ULL beg = (m*(i-1))/instance_c; ULL end = (m*i)/instance_c; Receive(i); hash2 = (hash2*pow(P, end-beg)+GetLL(i))%Q; } int wyn = 0; for (int i=1; i<=instance_c; i++) { Receive(i); wyn += GetLL(i); } printf("%d\n", wyn); } else { ULL beg = (n*(instance_no-1))/instance_c; ULL end = (n*instance_no)/instance_c; ULL frag_hash = 0; for (int i=beg; i<end; i++) frag_hash = (frag_hash*P+SignalAt(i+1))%Q; PutLL(0, frag_hash); Send(0); Receive(0); ULL hash = GetLL(0); beg = (m*(instance_no-1))/instance_c; end = (m*instance_no)/instance_c; sig_hash = new ULL[end-beg+7]; sig_hash[0] = 0; for (int i=beg; i<end; i++) sig_hash[i-beg+1] = (sig_hash[i-beg]*P+SeqAt(i+1))%Q; PutLL(0, sig_hash[end-beg]); Send(0); Receive(0); ULL prev = GetLL(0); sig_hash[0] = prev; for (int i=beg; i<end; i++) { prev = (prev*P)%Q; sig_hash[i-beg+1] = (sig_hash[i-beg+1]+prev)%Q; } for (int i=1; i<=instance_c; i++) { ULL b = (m*(i-1))/instance_c + n; if (beg < b && b <= end) { PutLL(i, sig_hash[b-beg]); Send(i); } } ULL w = 0; if (beg+n <= m) { ULL end_hash = GetLL(Receive(-1)); ULL mul = pow(P, n); if (beg+n <= m) { for (int i=beg; i+n <= m && i < end; i++) { //my mamy przedpoczątek, koniec dostajemy //przedpoczątek to sig_hash[i-beg] //dostajemy dobry koniec, po ruchu należy do niego dopisać element if ( ((Q - (mul*sig_hash[i-beg])%Q) + end_hash)%Q == hash) if (SeqAt(i+1) == SignalAt(1) && SeqAt(i+1+n/2) == SignalAt(1+n/2) && SeqAt(i+n) == SignalAt(n)) w++; end_hash = (end_hash*P+SeqAt(i+n+1))%Q; } } } PutLL(0, w); Send(0); delete[] sig_hash; } 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 | #include <cstdio> #include "poszukiwania.h" #include "message.h" using namespace std; typedef unsigned long long ULL; const int N=1000*1000+7; const ULL P = 1000000009; const ULL Q = 2000000033; ULL* sig_hash; ULL pow (ULL a, ULL b) { a%=Q; ULL w = 1; for (; b; b>>=1, a=(a*a)%Q) if (b&1) w = (w*a)%Q; return w; } int main () { ULL instance_no = MyNodeId(); ULL instance_c = NumberOfNodes(); instance_c--; //bo zero nie pracuje ULL n = SignalLength(); //wzorzec ULL m = SeqLength(); //tekst if (instance_no == 0) { ULL hash = 0; for (int i=1; i<=instance_c; i++) { ULL beg = (n*(i-1))/instance_c; ULL end = (n*i)/instance_c; Receive(i); hash = (hash*pow(P, end-beg)+GetLL(i))%Q; } for (int i=1; i<=instance_c; i++) { PutLL(i, hash); Send(i); } ULL hash2 = 0; for (int i=1; i<=instance_c; i++) { PutLL(i, hash2); Send(i); ULL beg = (m*(i-1))/instance_c; ULL end = (m*i)/instance_c; Receive(i); hash2 = (hash2*pow(P, end-beg)+GetLL(i))%Q; } int wyn = 0; for (int i=1; i<=instance_c; i++) { Receive(i); wyn += GetLL(i); } printf("%d\n", wyn); } else { ULL beg = (n*(instance_no-1))/instance_c; ULL end = (n*instance_no)/instance_c; ULL frag_hash = 0; for (int i=beg; i<end; i++) frag_hash = (frag_hash*P+SignalAt(i+1))%Q; PutLL(0, frag_hash); Send(0); Receive(0); ULL hash = GetLL(0); beg = (m*(instance_no-1))/instance_c; end = (m*instance_no)/instance_c; sig_hash = new ULL[end-beg+7]; sig_hash[0] = 0; for (int i=beg; i<end; i++) sig_hash[i-beg+1] = (sig_hash[i-beg]*P+SeqAt(i+1))%Q; PutLL(0, sig_hash[end-beg]); Send(0); Receive(0); ULL prev = GetLL(0); sig_hash[0] = prev; for (int i=beg; i<end; i++) { prev = (prev*P)%Q; sig_hash[i-beg+1] = (sig_hash[i-beg+1]+prev)%Q; } for (int i=1; i<=instance_c; i++) { ULL b = (m*(i-1))/instance_c + n; if (beg < b && b <= end) { PutLL(i, sig_hash[b-beg]); Send(i); } } ULL w = 0; if (beg+n <= m) { ULL end_hash = GetLL(Receive(-1)); ULL mul = pow(P, n); if (beg+n <= m) { for (int i=beg; i+n <= m && i < end; i++) { //my mamy przedpoczątek, koniec dostajemy //przedpoczątek to sig_hash[i-beg] //dostajemy dobry koniec, po ruchu należy do niego dopisać element if ( ((Q - (mul*sig_hash[i-beg])%Q) + end_hash)%Q == hash) if (SeqAt(i+1) == SignalAt(1) && SeqAt(i+1+n/2) == SignalAt(1+n/2) && SeqAt(i+n) == SignalAt(n)) w++; end_hash = (end_hash*P+SeqAt(i+n+1))%Q; } } } PutLL(0, w); Send(0); delete[] sig_hash; } return 0; } |