#include "poszukiwania.h" #include "message.h" #include<vector> #include<cstdio> #include<cmath> using namespace std; int main() { long long S=SignalLength(), M = SeqLength(), i, ile=0, j, k, r; long long a = M-S+1; long long *sumaM, sumaS=0, suma; if (MyNodeId()==0) sumaM=new long long [a]; vector<long long> tab; int n = NumberOfNodes(); long long poczatek = MyNodeId()*S/n; long long koniec = (MyNodeId()+1)*S/n; for(i=poczatek+1; i <=koniec; i++) sumaS+=SignalAt(i); if(MyNodeId()>0) { PutLL(0, sumaS); Send(0); } else { for (i = 1; i < n; ++i) { int instancja = Receive(-1); sumaS+=GetLL(instancja); } } poczatek = MyNodeId()*a/n; koniec = (MyNodeId()+1)*a/n; suma=0; for(i=poczatek+1; i <=koniec; i++) { suma+=SeqAt(i); } if(MyNodeId()>0) { PutLL(0, suma); Send(0); } else { for (i = 1; i < n; ++i) { int instancja = Receive(-1); suma+=GetLL(instancja); } if(suma==sumaS) tab.push_back(0); for(i=1; i < a; i++) { suma = suma-SeqAt(i)+SeqAt(i+S); if (suma==sumaS) tab.push_back(i); } long long b = tab.size(); long long c = b/(n-1); for(i=1; i < n-1; i++) { PutLL(i, c); Send(i); for(j=c*(i-1); j < c*i; j++) { PutLL(i, tab[j]); Send(i); } } c=c*(n-2);r=b-c; PutLL(n-1, r); Send(n-1); for(i=0; i < r; i++) { PutLL(n-1, tab[i+c]); Send(n-1); } } if (MyNodeId()>0) { Receive(0); long long b = GetLL(0); for(i=0; i < b; i++) { long long c = GetLL(0); tab.push_back(c); } ile=0; for(i=0; i < b; i++) { j=tab[i]; k=1; while(SeqAt(j+k)==SignalAt(k)&&k<=S) { if (k==S) ile++; k++; } } PutLL(0, ile); Send(0); } if (MyNodeId()==0) { ile=0; for (i = 1; i < n; ++i) { int instancja = Receive(-1); ile+=GetLL(instancja); } printf("%lld", ile); } 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 | #include "poszukiwania.h" #include "message.h" #include<vector> #include<cstdio> #include<cmath> using namespace std; int main() { long long S=SignalLength(), M = SeqLength(), i, ile=0, j, k, r; long long a = M-S+1; long long *sumaM, sumaS=0, suma; if (MyNodeId()==0) sumaM=new long long [a]; vector<long long> tab; int n = NumberOfNodes(); long long poczatek = MyNodeId()*S/n; long long koniec = (MyNodeId()+1)*S/n; for(i=poczatek+1; i <=koniec; i++) sumaS+=SignalAt(i); if(MyNodeId()>0) { PutLL(0, sumaS); Send(0); } else { for (i = 1; i < n; ++i) { int instancja = Receive(-1); sumaS+=GetLL(instancja); } } poczatek = MyNodeId()*a/n; koniec = (MyNodeId()+1)*a/n; suma=0; for(i=poczatek+1; i <=koniec; i++) { suma+=SeqAt(i); } if(MyNodeId()>0) { PutLL(0, suma); Send(0); } else { for (i = 1; i < n; ++i) { int instancja = Receive(-1); suma+=GetLL(instancja); } if(suma==sumaS) tab.push_back(0); for(i=1; i < a; i++) { suma = suma-SeqAt(i)+SeqAt(i+S); if (suma==sumaS) tab.push_back(i); } long long b = tab.size(); long long c = b/(n-1); for(i=1; i < n-1; i++) { PutLL(i, c); Send(i); for(j=c*(i-1); j < c*i; j++) { PutLL(i, tab[j]); Send(i); } } c=c*(n-2);r=b-c; PutLL(n-1, r); Send(n-1); for(i=0; i < r; i++) { PutLL(n-1, tab[i+c]); Send(n-1); } } if (MyNodeId()>0) { Receive(0); long long b = GetLL(0); for(i=0; i < b; i++) { long long c = GetLL(0); tab.push_back(c); } ile=0; for(i=0; i < b; i++) { j=tab[i]; k=1; while(SeqAt(j+k)==SignalAt(k)&&k<=S) { if (k==S) ile++; k++; } } PutLL(0, ile); Send(0); } if (MyNodeId()==0) { ile=0; for (i = 1; i < n; ++i) { int instancja = Receive(-1); ile+=GetLL(instancja); } printf("%lld", ile); } return 0; } |