#include "poszukiwania.h" #include "message.h" #include <cstdio> #include <algorithm> using std::min; #define ll long long int s, n, N, Id; const int m1 = 1e9 + 9; const int m2 = 1100000017; const ll p = 10; struct Hash { ll i1, i2, l; Hash(ll x = 0) : i1(x % m1), i2(x % m2), l(x) {} Hash(ll i1, ll i2, ll l) : i1(i1 % m1), i2(i2 % m2), l(l) {} void operator+=(Hash const& o) { i1 = (i1 + o.i1) % m1; i2 = (i2 + o.i2) % m2; l = (l + o.l); } void operator-=(Hash const& o) { i1 = (i1 - o.i1 + m1) % m1; i2 = (i2 - o.i2 + m2) % m2; l = (l - o.l); } void operator*=(int x) { i1 = (i1 * x) % m1; i2 = (i2 * x) % m2; l *= x; } void operator*=(Hash o) { i1 = (i1 * o.i1) % m1; i2 = (i2 * o.i2) % m2; l *= o.l; } bool operator==(Hash const& o) const { return i1 == o.i1 && i2 == o.i2 && l == o.l; } void print() { printf("%lld %lld %lld\n", i1, i2, l); } }; Hash operator*(Hash o, ll a) { return { (a * o.i1) % m1, (a * o.i2) % m2, a * o.l }; } Hash pow(ll b) { Hash res(1), a(p); while(b) { if(b & 1) res *= a; a *= a; b >>= 1; } return res; } int seq_size; int beg; Hash _s, _w; void search() { int sum = 0; Hash _pow = pow(s); if(_s == _w) sum++; for(int i = 1; i <= seq_size; ++i) { if(_s == _w) sum++; // printf("searching..."); // _w.print(); _w *= p; _w -= _pow * SeqAt(i + Id * seq_size); if(i + Id * seq_size + s <= n) _w += SeqAt(i + Id * seq_size + s); else break; } PutInt(0, sum); Send(0); if(Id) exit(0); } int main() { N = NumberOfNodes(); Id = MyNodeId(); s = SignalLength(); n = SeqLength(); /* if(n < 10000) { N = 1; if(Id) return 0; }*/ Hash tmp; if(s > 10000) { int s_seq_size = (s + N - 1) / N; // printf("%d\n", s_seq_size); for(int i = Id * s_seq_size; i < (Id + 1) * s_seq_size; ++i) { if(i >= s) break; _s *= p; _s += (SignalAt(i + 1)); // _s.print(); } for(int i = 0; i < N; ++i) { PutLL(i, _s.i1); PutLL(i, _s.i2); PutLL(i, _s.l); Send(i); } _s = 0; for(int i = 0; i < N; ++i) { Receive(i); tmp.i1 = GetLL(i); tmp.i2 = GetLL(i); tmp.l = GetLL(i); // tmp.print(); _s *= pow(min(s_seq_size, s - i * s_seq_size)); _s += tmp; } // _s.print(); } else { for(int i = 1; i <= s; ++i) { _s *= p; _s += SignalAt(i); } } seq_size = (n + N - 1) / N; beg = seq_size * Id + 1; int rest = s % seq_size, fragments = s / seq_size; // printf("s contains %d x %d and %d\n", fragments, seq_size, rest); Hash _sum; if(s > seq_size) { for(int i = 1; i <= seq_size; ++i) { if(i + Id * seq_size >= n) break; _sum *= p; _sum += SeqAt(i + Id * seq_size); if(i == rest && rest && Id - fragments >= 0 && fragments) { // printf("send %d to %d\n", rest, Id - fragments); PutLL(Id - fragments, _sum.i1); PutLL(Id - fragments, _sum.i2); PutLL(Id - fragments, _sum.l); Send(Id - fragments); } // printf("%d %d\n", fragments, rest); if(i == rest && fragments == 0) { break; } } // _sum.print(); _w = _sum; if((Id + 1) * seq_size > n) { PutInt(0, 0); Send(0); return 0; } if(fragments) { for(int i = Id - fragments + 1; i < Id; ++i) { if(i >= 0) { // printf("*send %d to %d\n", seq_size, i); PutLL(i, _sum.i1); PutLL(i, _sum.i2); PutLL(i, _sum.l); Send(i); } } } if(Id * seq_size + s > n) { PutInt(0, 0); Send(0); return 0; } // count w for(int i = 1; i < fragments; i++) { // printf("try to get %d from %d by %d\n", seq_size, Id + i, Id); Receive(Id + i); tmp.i1 = GetLL(Id + i); tmp.i2 = GetLL(Id + i); tmp.l = GetLL(Id + i); _w *= pow(seq_size); _w += tmp; } if(rest) { // printf("try to get %d from %d\n", rest, Id + fragments); Receive(Id + fragments); tmp.i1 = GetLL(Id + fragments); tmp.i2 = GetLL(Id + fragments); tmp.l = GetLL(Id + fragments); _w *= pow(rest); _w += tmp; } // printf("w: "); // _w.print(); } else { if(Id * seq_size + s > n) { PutInt(0, 0); Send(0); return 0; } for(int i = 1; i <= s; ++i) { _w *= p; _w += SeqAt(i + Id * seq_size); } // _w.print(); } search(); long long result = 0; for(int i = 0; i < N; ++i) { Receive(i); result += GetInt(i); } printf("%lld\n", result); 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 | #include "poszukiwania.h" #include "message.h" #include <cstdio> #include <algorithm> using std::min; #define ll long long int s, n, N, Id; const int m1 = 1e9 + 9; const int m2 = 1100000017; const ll p = 10; struct Hash { ll i1, i2, l; Hash(ll x = 0) : i1(x % m1), i2(x % m2), l(x) {} Hash(ll i1, ll i2, ll l) : i1(i1 % m1), i2(i2 % m2), l(l) {} void operator+=(Hash const& o) { i1 = (i1 + o.i1) % m1; i2 = (i2 + o.i2) % m2; l = (l + o.l); } void operator-=(Hash const& o) { i1 = (i1 - o.i1 + m1) % m1; i2 = (i2 - o.i2 + m2) % m2; l = (l - o.l); } void operator*=(int x) { i1 = (i1 * x) % m1; i2 = (i2 * x) % m2; l *= x; } void operator*=(Hash o) { i1 = (i1 * o.i1) % m1; i2 = (i2 * o.i2) % m2; l *= o.l; } bool operator==(Hash const& o) const { return i1 == o.i1 && i2 == o.i2 && l == o.l; } void print() { printf("%lld %lld %lld\n", i1, i2, l); } }; Hash operator*(Hash o, ll a) { return { (a * o.i1) % m1, (a * o.i2) % m2, a * o.l }; } Hash pow(ll b) { Hash res(1), a(p); while(b) { if(b & 1) res *= a; a *= a; b >>= 1; } return res; } int seq_size; int beg; Hash _s, _w; void search() { int sum = 0; Hash _pow = pow(s); if(_s == _w) sum++; for(int i = 1; i <= seq_size; ++i) { if(_s == _w) sum++; // printf("searching..."); // _w.print(); _w *= p; _w -= _pow * SeqAt(i + Id * seq_size); if(i + Id * seq_size + s <= n) _w += SeqAt(i + Id * seq_size + s); else break; } PutInt(0, sum); Send(0); if(Id) exit(0); } int main() { N = NumberOfNodes(); Id = MyNodeId(); s = SignalLength(); n = SeqLength(); /* if(n < 10000) { N = 1; if(Id) return 0; }*/ Hash tmp; if(s > 10000) { int s_seq_size = (s + N - 1) / N; // printf("%d\n", s_seq_size); for(int i = Id * s_seq_size; i < (Id + 1) * s_seq_size; ++i) { if(i >= s) break; _s *= p; _s += (SignalAt(i + 1)); // _s.print(); } for(int i = 0; i < N; ++i) { PutLL(i, _s.i1); PutLL(i, _s.i2); PutLL(i, _s.l); Send(i); } _s = 0; for(int i = 0; i < N; ++i) { Receive(i); tmp.i1 = GetLL(i); tmp.i2 = GetLL(i); tmp.l = GetLL(i); // tmp.print(); _s *= pow(min(s_seq_size, s - i * s_seq_size)); _s += tmp; } // _s.print(); } else { for(int i = 1; i <= s; ++i) { _s *= p; _s += SignalAt(i); } } seq_size = (n + N - 1) / N; beg = seq_size * Id + 1; int rest = s % seq_size, fragments = s / seq_size; // printf("s contains %d x %d and %d\n", fragments, seq_size, rest); Hash _sum; if(s > seq_size) { for(int i = 1; i <= seq_size; ++i) { if(i + Id * seq_size >= n) break; _sum *= p; _sum += SeqAt(i + Id * seq_size); if(i == rest && rest && Id - fragments >= 0 && fragments) { // printf("send %d to %d\n", rest, Id - fragments); PutLL(Id - fragments, _sum.i1); PutLL(Id - fragments, _sum.i2); PutLL(Id - fragments, _sum.l); Send(Id - fragments); } // printf("%d %d\n", fragments, rest); if(i == rest && fragments == 0) { break; } } // _sum.print(); _w = _sum; if((Id + 1) * seq_size > n) { PutInt(0, 0); Send(0); return 0; } if(fragments) { for(int i = Id - fragments + 1; i < Id; ++i) { if(i >= 0) { // printf("*send %d to %d\n", seq_size, i); PutLL(i, _sum.i1); PutLL(i, _sum.i2); PutLL(i, _sum.l); Send(i); } } } if(Id * seq_size + s > n) { PutInt(0, 0); Send(0); return 0; } // count w for(int i = 1; i < fragments; i++) { // printf("try to get %d from %d by %d\n", seq_size, Id + i, Id); Receive(Id + i); tmp.i1 = GetLL(Id + i); tmp.i2 = GetLL(Id + i); tmp.l = GetLL(Id + i); _w *= pow(seq_size); _w += tmp; } if(rest) { // printf("try to get %d from %d\n", rest, Id + fragments); Receive(Id + fragments); tmp.i1 = GetLL(Id + fragments); tmp.i2 = GetLL(Id + fragments); tmp.l = GetLL(Id + fragments); _w *= pow(rest); _w += tmp; } // printf("w: "); // _w.print(); } else { if(Id * seq_size + s > n) { PutInt(0, 0); Send(0); return 0; } for(int i = 1; i <= s; ++i) { _w *= p; _w += SeqAt(i + Id * seq_size); } // _w.print(); } search(); long long result = 0; for(int i = 0; i < N; ++i) { Receive(i); result += GetInt(i); } printf("%lld\n", result); return 0; } |