#include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> //#include "pos-test/poszukiwania-test.h" #include "poszukiwania.h" #include "message.h" #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define SC(n) scanf("%d ", &n) #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf("%d: ", id); dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<int> si; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef vector<vi> vvi; typedef vector<vpi> vvpi; const int HH = 2; int id, nd, nn, mm; int h, ans, subtract, shiftS, shiftM; int hashQ[HH] = {1000003351, 1000008041}; int hashP[HH] = {2801, 3002231}; int hashR[HH] = {531597640, 455940268}; class seq { public: virtual int get(int i) {return 0;} }; class seqN : public seq { public: int get(int i) { return (int) SeqAt(i+1); }}; class seqM : public seq { public: int get(int i) { return (int) SignalAt(i+1); }}; vi getHashOfMyPart(seq *ss, int len, int shift, int offset) { int start = shift * id + offset, stop = min(len, shift * (id + 1) + offset); vi hashes(h, 0), xs(h, 1); for(int pos = start; pos < stop; pos++) { int a = ss->get(pos); REP(j, h) { int a1 = ((ll) a * xs[j]) % hashQ[j]; hashes[j] = (hashes[j] + a1) % hashQ[j]; xs[j] = ((ll) hashP[j] * xs[j]) % hashQ[j]; } } return hashes; } int pow(int x, int k, int mod) { if(k == 0) return 1; int tmp = pow(x, k/2, mod); tmp = ((ll) tmp * tmp) % mod; if(k % 2) tmp = ((ll) tmp * x) % mod; return tmp; } int check(vi & h1, vi & h2) { REP(j, h) if(h1[j] != h2[j]) return 0; return 1; } void printAll(seq *ss, int len) { REP(i, len) printf("%d ", ss->get(i)); printf("\n"); } int main() { id = MyNodeId(); nd = NumberOfNodes(); mm = SignalLength(); nn = SeqLength(); h = HH; subtract = nn - mm + 1; shiftM = (mm + nd - 1) / nd; shiftS = (subtract + nd - 1) / nd; seqN sn; seqM sm; if(!id) dbg { printf("SM: "); printAll(&sm, mm); printf("SN: "); printAll(&sn, nn); } prd("shiftM = %d shiftS = %d sub = %d\n", shiftM, shiftS, subtract); vi hashM = getHashOfMyPart(&sm, mm, shiftM, 0); vi hashN = getHashOfMyPart(&sn, mm, shiftM, 0); vi hashS1 = getHashOfMyPart(&sn, subtract, shiftS, 0); vi hashS2 = getHashOfMyPart(&sn, nn, shiftS, mm); prd("hashes M %d N %d S %d S %d\n", hashM[0], hashN[0], hashS1[0], hashS2[0]); if(id > 0) { REP(j, h) { PutInt(0, hashM[j]); PutInt(0, hashN[j]); } Send(0); Receive(0); REP(j, h) { hashM[j] = GetInt(0); hashN[j] = GetInt(0); } } else { for(int pi = 1; pi < nd; pi++) { Receive(pi); REP(j, h) { int hm = GetInt(pi), hn = GetInt(pi); int pot = pow(hashP[j], shiftM * pi, hashQ[j]); hm = ((ll) hm * pot) % hashQ[j]; hn = ((ll) hn * pot) % hashQ[j]; hashM[j] = (hashM[j] + hm) % hashQ[j]; hashN[j] = (hashN[j] + hn) % hashQ[j]; } } for(int pi = 1; pi < nd; pi++) { REP(j, h) { PutInt(pi, hashM[j]); PutInt(pi, hashN[j]); } Send(pi); } } prd("full hashes M %d N %d\n", hashM[0], hashN[0]); prd("check3\n"); // chcemy hashN przysunac do miejsca id * shiftS if(id > 0) { Receive(id - 1); REP(j, h) { int pot1 = pow(hashP[j], shiftS * id, hashQ[j]); int s1 = GetInt(id - 1), s2 = GetInt(id - 1); hashS1[j] = ((ll) hashS1[j] * pot1) % hashQ[j]; hashS2[j] = ((ll) hashS2[j] * pot1) % hashQ[j]; hashS1[j] = (hashS1[j] + s1) % hashQ[j]; hashS2[j] = (hashS2[j] + s2) % hashQ[j]; s2 = ((ll) s2 * pow(hashP[j], mm, hashQ[j])) % hashQ[j]; hashN[j] = (hashN[j] + s2) % hashQ[j]; hashN[j] = (hashN[j] + hashQ[j] - s1) % hashQ[j]; hashN[j] = ((ll) hashN[j] * pow(hashR[j], shiftS * id, hashQ[j])) % hashQ[j]; } } if(id < nd - 1) { REP(j, h) { PutInt(id + 1, hashS1[j]); PutInt(id + 1, hashS2[j]); } Send(id + 1); } prd("check4\n"); int start = shiftS * id, stop = min(subtract, shiftS * (id + 1)); vi pots(h); REP(j, h) pots[j] = pow(hashP[j], mm - 1, hashQ[j]); // ok... for(int pos = start; pos < stop; pos++) { prd("my hash %d: N %d\n", pos, hashN[0]); if (check(hashN, hashM)) { prd("FOUND %d\n", pos); ans++; } int a1 = (&sn)->get(pos), a2 = (&sn)->get(pos + mm), aj; REP(j, h) { hashN[j] = (hashN[j] + hashQ[j] - a1) % hashQ[j]; //prd("sanity %d\n", hashN[j]); hashN[j] = ((ll) hashN[j] * hashR[j]) % hashQ[j]; //prd("sanity %d\n", hashN[j]); aj = ((ll) a2 * pots[j]) % hashQ[j]; hashN[j] = (hashN[j] + aj) % hashQ[j]; } prd("sanity %d %d %d\n", a1, a2, aj); } if(id > 0) { PutInt(0, ans); Send(0); } else { for(int pi = 1; pi < nd; pi++) { Receive(pi); ans += GetInt(pi); } printf("%d\n", ans); } 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 | #include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> //#include "pos-test/poszukiwania-test.h" #include "poszukiwania.h" #include "message.h" #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define SC(n) scanf("%d ", &n) #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf("%d: ", id); dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<int> si; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef vector<vi> vvi; typedef vector<vpi> vvpi; const int HH = 2; int id, nd, nn, mm; int h, ans, subtract, shiftS, shiftM; int hashQ[HH] = {1000003351, 1000008041}; int hashP[HH] = {2801, 3002231}; int hashR[HH] = {531597640, 455940268}; class seq { public: virtual int get(int i) {return 0;} }; class seqN : public seq { public: int get(int i) { return (int) SeqAt(i+1); }}; class seqM : public seq { public: int get(int i) { return (int) SignalAt(i+1); }}; vi getHashOfMyPart(seq *ss, int len, int shift, int offset) { int start = shift * id + offset, stop = min(len, shift * (id + 1) + offset); vi hashes(h, 0), xs(h, 1); for(int pos = start; pos < stop; pos++) { int a = ss->get(pos); REP(j, h) { int a1 = ((ll) a * xs[j]) % hashQ[j]; hashes[j] = (hashes[j] + a1) % hashQ[j]; xs[j] = ((ll) hashP[j] * xs[j]) % hashQ[j]; } } return hashes; } int pow(int x, int k, int mod) { if(k == 0) return 1; int tmp = pow(x, k/2, mod); tmp = ((ll) tmp * tmp) % mod; if(k % 2) tmp = ((ll) tmp * x) % mod; return tmp; } int check(vi & h1, vi & h2) { REP(j, h) if(h1[j] != h2[j]) return 0; return 1; } void printAll(seq *ss, int len) { REP(i, len) printf("%d ", ss->get(i)); printf("\n"); } int main() { id = MyNodeId(); nd = NumberOfNodes(); mm = SignalLength(); nn = SeqLength(); h = HH; subtract = nn - mm + 1; shiftM = (mm + nd - 1) / nd; shiftS = (subtract + nd - 1) / nd; seqN sn; seqM sm; if(!id) dbg { printf("SM: "); printAll(&sm, mm); printf("SN: "); printAll(&sn, nn); } prd("shiftM = %d shiftS = %d sub = %d\n", shiftM, shiftS, subtract); vi hashM = getHashOfMyPart(&sm, mm, shiftM, 0); vi hashN = getHashOfMyPart(&sn, mm, shiftM, 0); vi hashS1 = getHashOfMyPart(&sn, subtract, shiftS, 0); vi hashS2 = getHashOfMyPart(&sn, nn, shiftS, mm); prd("hashes M %d N %d S %d S %d\n", hashM[0], hashN[0], hashS1[0], hashS2[0]); if(id > 0) { REP(j, h) { PutInt(0, hashM[j]); PutInt(0, hashN[j]); } Send(0); Receive(0); REP(j, h) { hashM[j] = GetInt(0); hashN[j] = GetInt(0); } } else { for(int pi = 1; pi < nd; pi++) { Receive(pi); REP(j, h) { int hm = GetInt(pi), hn = GetInt(pi); int pot = pow(hashP[j], shiftM * pi, hashQ[j]); hm = ((ll) hm * pot) % hashQ[j]; hn = ((ll) hn * pot) % hashQ[j]; hashM[j] = (hashM[j] + hm) % hashQ[j]; hashN[j] = (hashN[j] + hn) % hashQ[j]; } } for(int pi = 1; pi < nd; pi++) { REP(j, h) { PutInt(pi, hashM[j]); PutInt(pi, hashN[j]); } Send(pi); } } prd("full hashes M %d N %d\n", hashM[0], hashN[0]); prd("check3\n"); // chcemy hashN przysunac do miejsca id * shiftS if(id > 0) { Receive(id - 1); REP(j, h) { int pot1 = pow(hashP[j], shiftS * id, hashQ[j]); int s1 = GetInt(id - 1), s2 = GetInt(id - 1); hashS1[j] = ((ll) hashS1[j] * pot1) % hashQ[j]; hashS2[j] = ((ll) hashS2[j] * pot1) % hashQ[j]; hashS1[j] = (hashS1[j] + s1) % hashQ[j]; hashS2[j] = (hashS2[j] + s2) % hashQ[j]; s2 = ((ll) s2 * pow(hashP[j], mm, hashQ[j])) % hashQ[j]; hashN[j] = (hashN[j] + s2) % hashQ[j]; hashN[j] = (hashN[j] + hashQ[j] - s1) % hashQ[j]; hashN[j] = ((ll) hashN[j] * pow(hashR[j], shiftS * id, hashQ[j])) % hashQ[j]; } } if(id < nd - 1) { REP(j, h) { PutInt(id + 1, hashS1[j]); PutInt(id + 1, hashS2[j]); } Send(id + 1); } prd("check4\n"); int start = shiftS * id, stop = min(subtract, shiftS * (id + 1)); vi pots(h); REP(j, h) pots[j] = pow(hashP[j], mm - 1, hashQ[j]); // ok... for(int pos = start; pos < stop; pos++) { prd("my hash %d: N %d\n", pos, hashN[0]); if (check(hashN, hashM)) { prd("FOUND %d\n", pos); ans++; } int a1 = (&sn)->get(pos), a2 = (&sn)->get(pos + mm), aj; REP(j, h) { hashN[j] = (hashN[j] + hashQ[j] - a1) % hashQ[j]; //prd("sanity %d\n", hashN[j]); hashN[j] = ((ll) hashN[j] * hashR[j]) % hashQ[j]; //prd("sanity %d\n", hashN[j]); aj = ((ll) a2 * pots[j]) % hashQ[j]; hashN[j] = (hashN[j] + aj) % hashQ[j]; } prd("sanity %d %d %d\n", a1, a2, aj); } if(id > 0) { PutInt(0, ans); Send(0); } else { for(int pi = 1; pi < nd; pi++) { Receive(pi); ans += GetInt(pi); } printf("%d\n", ans); } return 0; } |