#include <iostream> #include <set> #include <vector> #include "poszukiwania.h" #include "message.h" using namespace std; typedef long long ll; typedef pair<ll, ll> pll; const ll P = 37ULL; const ll PRIME = 1000000007ULL; // const ll SMALL_M = 0; const ll SMALL_M = 100000ULL; // const ll SMALL_S = 0; const ll SMALL_S = 100000ULL; ll S, M, L; int nid, nodes; ll calc_powL(ll k) { if (k == 0) return 1; ll res = calc_powL(k/2); res = (res * res) % PRIME; if ((k & 1) == 0) return res; else return (res * P) % PRIME; } inline ll h(ll hp, ll x) { return ((hp * P) % PRIME + x) % PRIME; } ll calch(ll b, ll e) { ll hv = 0; for (ll i = b; i <= e; i++) hv = h(hv, SignalAt(i)); return hv; } vector<pll> search(ll b, ll e, const set<ll>& calchs, ll l) { vector<pll> res; vector<ll> hs; hs.push_back(0); ll powL = calc_powL(l); ll hv = 0; for (ll i = b; i <= e; i++) { hv = h(hv, SeqAt(i)); // cerr << SeqAt(i) << ':' << hv; hs.push_back(hv); if ((i - b + 1) >= l) { ll hp = (hs[i-b+1-l] * powL) % PRIME; ll ht = (hv + PRIME - hp) % PRIME; // cerr << ':' << hp << ':' << ht; auto it = calchs.find(hv - hp); if (it != calchs.end()) res.push_back({*it, i-l}); } // cerr << '\n'; } return res; } void solve1() { if (nid > 0) return; // cerr << calch(1, S) << '\n'; auto res = search(1, M, {calch(1, S)}, S); // for (auto x : res) { // cerr << x.first << ' ' << x.second << ", "; // } // cerr << '\n'; cout << res.size() << '\n'; } inline pll segmentM(ll l) { ll size = M / nodes; return {nid * size + 1, (nid + 1 == nodes) ? M : min((nid + 1) * size - 1 + l, M)}; } inline pll segmentS() { ll size = S / nodes; return {nid * size + 1, (nid + 1 == nodes) ? S : ((nid + 1) * size + 1)}; // TODO } inline pll segmentS(int nid) { ll size = S / nodes; return {nid * size + 1, (nid + 1 == nodes) ? S : ((nid + 1) * size + 1)}; // TODO } void solve2() { if (nid == 0) { ll ch = calch(1, S); for (int i = 0; i < nodes; i++) { PutLL(i, ch); Send(i); } } set<ll> chs; Receive(0); chs.insert(GetLL(0)); auto sm = segmentM(S); // cerr << "sm [" << sm.first << ", " << sm.second << "] "; auto res = search(sm.first, sm.second, chs, S); // cerr << "res=" << res.size() << '\n'; PutLL(0, res.size()); Send(0); if (nid == 0) { ll res = 0; for (int i = 0; i < nodes; i++) { Receive(i); res += GetLL(i); } cout << res << '\n'; } } void solve3_join() { vector<vector<ll>> res(nodes); for (int i = 0; i < nodes; i++) { Receive(i); ll k = GetLL(i); cerr << "k=" << k << '\n'; // for (int j = 0; j < k; j++) { ll ch = GetLL(i), pos = GetLL(i); cerr << "(" << ch << ", " << pos << ")\n"; } } cout << "?" << '\n'; } void solve3() { auto ss = segmentS(); ll l = ss.second - ss.first + 1; auto ssL = (nid+1 != nodes) ? segmentS(nodes-1) : segmentS(0); ll l2 = ssL.second - ssL.first + 1; ll ch = calch(ss.first, ss.second); cerr << "ss [" << ss.first << ", " << ss.second << "]\n"; for (int i = 0; i < nodes; i++) { PutLL(i, ch); Send(i); } set<ll> chs; for (int i = 0; i < nodes; i++) { Receive(i); chs.insert(GetLL(i)); } for (auto x : chs) { cerr << x << ' '; } cerr << '\n'; auto sm = segmentM(l); cerr << "sm [" << sm.first << ", " << sm.second << "]\n"; auto res = search(sm.first, sm.second, chs, l); cerr << "res=" << res.size() << '\n'; auto resL = search(sm.first, sm.second, chs, l2); cerr << "resL=" << resL.size() << '\n'; PutLL(0, res.size()); for (auto x : res) { PutLL(0, x.first); PutLL(0, x.second); } Send(0); if (nid == 0) solve3_join(); } int main() { S = SignalLength(), M = SeqLength(); nid = MyNodeId(); nodes = NumberOfNodes(); if (M < SMALL_M) { solve1(); return 0; } // if (S < SMALL_S) { solve2(); return 0; } solve2(); 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 | #include <iostream> #include <set> #include <vector> #include "poszukiwania.h" #include "message.h" using namespace std; typedef long long ll; typedef pair<ll, ll> pll; const ll P = 37ULL; const ll PRIME = 1000000007ULL; // const ll SMALL_M = 0; const ll SMALL_M = 100000ULL; // const ll SMALL_S = 0; const ll SMALL_S = 100000ULL; ll S, M, L; int nid, nodes; ll calc_powL(ll k) { if (k == 0) return 1; ll res = calc_powL(k/2); res = (res * res) % PRIME; if ((k & 1) == 0) return res; else return (res * P) % PRIME; } inline ll h(ll hp, ll x) { return ((hp * P) % PRIME + x) % PRIME; } ll calch(ll b, ll e) { ll hv = 0; for (ll i = b; i <= e; i++) hv = h(hv, SignalAt(i)); return hv; } vector<pll> search(ll b, ll e, const set<ll>& calchs, ll l) { vector<pll> res; vector<ll> hs; hs.push_back(0); ll powL = calc_powL(l); ll hv = 0; for (ll i = b; i <= e; i++) { hv = h(hv, SeqAt(i)); // cerr << SeqAt(i) << ':' << hv; hs.push_back(hv); if ((i - b + 1) >= l) { ll hp = (hs[i-b+1-l] * powL) % PRIME; ll ht = (hv + PRIME - hp) % PRIME; // cerr << ':' << hp << ':' << ht; auto it = calchs.find(hv - hp); if (it != calchs.end()) res.push_back({*it, i-l}); } // cerr << '\n'; } return res; } void solve1() { if (nid > 0) return; // cerr << calch(1, S) << '\n'; auto res = search(1, M, {calch(1, S)}, S); // for (auto x : res) { // cerr << x.first << ' ' << x.second << ", "; // } // cerr << '\n'; cout << res.size() << '\n'; } inline pll segmentM(ll l) { ll size = M / nodes; return {nid * size + 1, (nid + 1 == nodes) ? M : min((nid + 1) * size - 1 + l, M)}; } inline pll segmentS() { ll size = S / nodes; return {nid * size + 1, (nid + 1 == nodes) ? S : ((nid + 1) * size + 1)}; // TODO } inline pll segmentS(int nid) { ll size = S / nodes; return {nid * size + 1, (nid + 1 == nodes) ? S : ((nid + 1) * size + 1)}; // TODO } void solve2() { if (nid == 0) { ll ch = calch(1, S); for (int i = 0; i < nodes; i++) { PutLL(i, ch); Send(i); } } set<ll> chs; Receive(0); chs.insert(GetLL(0)); auto sm = segmentM(S); // cerr << "sm [" << sm.first << ", " << sm.second << "] "; auto res = search(sm.first, sm.second, chs, S); // cerr << "res=" << res.size() << '\n'; PutLL(0, res.size()); Send(0); if (nid == 0) { ll res = 0; for (int i = 0; i < nodes; i++) { Receive(i); res += GetLL(i); } cout << res << '\n'; } } void solve3_join() { vector<vector<ll>> res(nodes); for (int i = 0; i < nodes; i++) { Receive(i); ll k = GetLL(i); cerr << "k=" << k << '\n'; // for (int j = 0; j < k; j++) { ll ch = GetLL(i), pos = GetLL(i); cerr << "(" << ch << ", " << pos << ")\n"; } } cout << "?" << '\n'; } void solve3() { auto ss = segmentS(); ll l = ss.second - ss.first + 1; auto ssL = (nid+1 != nodes) ? segmentS(nodes-1) : segmentS(0); ll l2 = ssL.second - ssL.first + 1; ll ch = calch(ss.first, ss.second); cerr << "ss [" << ss.first << ", " << ss.second << "]\n"; for (int i = 0; i < nodes; i++) { PutLL(i, ch); Send(i); } set<ll> chs; for (int i = 0; i < nodes; i++) { Receive(i); chs.insert(GetLL(i)); } for (auto x : chs) { cerr << x << ' '; } cerr << '\n'; auto sm = segmentM(l); cerr << "sm [" << sm.first << ", " << sm.second << "]\n"; auto res = search(sm.first, sm.second, chs, l); cerr << "res=" << res.size() << '\n'; auto resL = search(sm.first, sm.second, chs, l2); cerr << "resL=" << resL.size() << '\n'; PutLL(0, res.size()); for (auto x : res) { PutLL(0, x.first); PutLL(0, x.second); } Send(0); if (nid == 0) solve3_join(); } int main() { S = SignalLength(), M = SeqLength(); nid = MyNodeId(); nodes = NumberOfNodes(); if (M < SMALL_M) { solve1(); return 0; } // if (S < SMALL_S) { solve2(); return 0; } solve2(); return 0; } |