#include "poszukiwania.h" #include "message.h" #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define sz(x) int((x).size()) #define dbg(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (__typeof(t.begin()) i = t.begin(); i != t.end(); ++i) #define fi first #define se second typedef pair<int,int> pii; typedef vector<int> vi; typedef long long ll; const int P1 = 999999929, P2 = 998999999, P3 = 989999993; // primes ll chunksN[1100]; // fast exponentiation modulo MOD ll powe (ll x, ll p) { if (p == 0) return 1; ll w = powe((x*x)%P1, p/2); if (p & 1) w = (w*x)%P1; return w; } ll mulMod(ll a, ll b) { ll w=0; ll m=1; while(m) { if (b&m) w = (w+a)%P1; a = (a<<1)%P1; m<<=1; } return w; } int main() { long long N = SeqLength(); long long M = SignalLength(); int X = NumberOfNodes(); int node = MyNodeId(); //compute HASH and send to 0: long long chunkMsize = M/X; long long chunkA,chunkB; chunkA = node*chunkMsize; chunkB = chunkA+chunkMsize-1; if (node == X-1) chunkB = M-1; long long myHash = 0; ll mHash = 0; ll baseP2 = powe(P2,M-chunkB-1); for(ll i=chunkA;i<=chunkB;++i) { myHash = (myHash*P2 + SignalAt(i+1)) %P1; } myHash = (myHash*baseP2)%P1; //send to 0 if (node>0) { PutLL(0, myHash); Send(0); Receive(0); mHash = GetLL(0); } else { FOR(i,X-1) { Receive(i+1); myHash += GetLL(i+1); myHash %= P1; } FOR(i,X-1) { mHash = myHash; PutLL(i+1,mHash); Send(i+1); } } //compute long long chunkNsize = N/X; long long chunkNA,chunkNB; chunkNA = node*chunkNsize; chunkNB = chunkNA+chunkNsize-1; if (node == X-1) chunkNB = N-1; long long chunkHash = 0; for(ll i=chunkNA;i<=chunkNB;++i) { chunkHash = (chunkHash*P2 + SeqAt(i+1)) %P1; } chunksN[node] = (chunkHash) % P1; FOR(i,X) { if (i==node) continue; PutLL(i,chunksN[node]); Send(i); } FOR(i,X) { if (i==node) continue; Receive(i); chunksN[i] = GetLL(i); } //final construction //compute first hash: ll start = chunkNA; ll endd = chunkNA+M-1; ll res = 0; if (endd<N && chunkNB != -1) { //compute where endd is: ll endIn = -1; ll endoff = -1; FOR(i,X) { ll ca = i*chunkNsize; ll cb = ca+chunkNsize-1; if (i == X-1) cb = N-1; if (ca<=endd && endd<=cb) { endIn = i; endoff = endd-ca+1; } } ll cumulate = 0; for (int i=node;i<endIn;++i){ ll base = powe(P2, endoff+chunkNsize*(endIn-i-1)); cumulate += (base*chunksN[i])%P1; cumulate %= P1; } //compute first ll curHash = 0; for (ll i=endd-endoff+1; i<=endd;++i) { curHash = (curHash*P2 + SeqAt(i+1)) %P1; } curHash+=cumulate; curHash = curHash % P1; if (curHash == mHash) res+=1; ll baseM = powe(P2,M-1); for (ll i=chunkNA; i<chunkNB; ++i) { if (i+M >= N) break; curHash = (curHash - (baseM*SeqAt(i+1))%P1); if (curHash < 0) curHash += P1; curHash = curHash*P2 + SeqAt(i+M + 1); curHash %= P1; if (curHash == mHash) res+=1; } } //send to 0 if (node>0) { PutLL(0, res); Send(0); } if (node == 0) { //dbg(mHash); FOR(i,X-1) { Receive(i+1); res += GetLL(i+1); } printf("%lld\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 | #include "poszukiwania.h" #include "message.h" #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define sz(x) int((x).size()) #define dbg(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (__typeof(t.begin()) i = t.begin(); i != t.end(); ++i) #define fi first #define se second typedef pair<int,int> pii; typedef vector<int> vi; typedef long long ll; const int P1 = 999999929, P2 = 998999999, P3 = 989999993; // primes ll chunksN[1100]; // fast exponentiation modulo MOD ll powe (ll x, ll p) { if (p == 0) return 1; ll w = powe((x*x)%P1, p/2); if (p & 1) w = (w*x)%P1; return w; } ll mulMod(ll a, ll b) { ll w=0; ll m=1; while(m) { if (b&m) w = (w+a)%P1; a = (a<<1)%P1; m<<=1; } return w; } int main() { long long N = SeqLength(); long long M = SignalLength(); int X = NumberOfNodes(); int node = MyNodeId(); //compute HASH and send to 0: long long chunkMsize = M/X; long long chunkA,chunkB; chunkA = node*chunkMsize; chunkB = chunkA+chunkMsize-1; if (node == X-1) chunkB = M-1; long long myHash = 0; ll mHash = 0; ll baseP2 = powe(P2,M-chunkB-1); for(ll i=chunkA;i<=chunkB;++i) { myHash = (myHash*P2 + SignalAt(i+1)) %P1; } myHash = (myHash*baseP2)%P1; //send to 0 if (node>0) { PutLL(0, myHash); Send(0); Receive(0); mHash = GetLL(0); } else { FOR(i,X-1) { Receive(i+1); myHash += GetLL(i+1); myHash %= P1; } FOR(i,X-1) { mHash = myHash; PutLL(i+1,mHash); Send(i+1); } } //compute long long chunkNsize = N/X; long long chunkNA,chunkNB; chunkNA = node*chunkNsize; chunkNB = chunkNA+chunkNsize-1; if (node == X-1) chunkNB = N-1; long long chunkHash = 0; for(ll i=chunkNA;i<=chunkNB;++i) { chunkHash = (chunkHash*P2 + SeqAt(i+1)) %P1; } chunksN[node] = (chunkHash) % P1; FOR(i,X) { if (i==node) continue; PutLL(i,chunksN[node]); Send(i); } FOR(i,X) { if (i==node) continue; Receive(i); chunksN[i] = GetLL(i); } //final construction //compute first hash: ll start = chunkNA; ll endd = chunkNA+M-1; ll res = 0; if (endd<N && chunkNB != -1) { //compute where endd is: ll endIn = -1; ll endoff = -1; FOR(i,X) { ll ca = i*chunkNsize; ll cb = ca+chunkNsize-1; if (i == X-1) cb = N-1; if (ca<=endd && endd<=cb) { endIn = i; endoff = endd-ca+1; } } ll cumulate = 0; for (int i=node;i<endIn;++i){ ll base = powe(P2, endoff+chunkNsize*(endIn-i-1)); cumulate += (base*chunksN[i])%P1; cumulate %= P1; } //compute first ll curHash = 0; for (ll i=endd-endoff+1; i<=endd;++i) { curHash = (curHash*P2 + SeqAt(i+1)) %P1; } curHash+=cumulate; curHash = curHash % P1; if (curHash == mHash) res+=1; ll baseM = powe(P2,M-1); for (ll i=chunkNA; i<chunkNB; ++i) { if (i+M >= N) break; curHash = (curHash - (baseM*SeqAt(i+1))%P1); if (curHash < 0) curHash += P1; curHash = curHash*P2 + SeqAt(i+M + 1); curHash %= P1; if (curHash == mHash) res+=1; } } //send to 0 if (node>0) { PutLL(0, res); Send(0); } if (node == 0) { //dbg(mHash); FOR(i,X-1) { Receive(i+1); res += GetLL(i+1); } printf("%lld\n", res); } return 0; } |