#include<cstdio> #include<algorithm> #include "message.h" #include "poszukiwania.h" #include<utility> #include<iostream> typedef unsigned long long int ll; using namespace std; int s,m, myNum, non; const ll p=1063; // a small prime number const ll q=(1ULL<<61)-1; // a big prime number const ll mask = (1ULL << 32) - 1; ll modq(ll x) { // x % q // x = 2^61 * (x >> 61) + x mod 2^61 return (x >> 61) + (x & q); } ll mod32q(ll x) { // 2^32 * x % q // x = 2^29 * (x >> 29) + x mod 2^29 // 2^32 * x = 2^61 * (x >> 29) + (x mod 2^29) << 32 return (x >> 29) + ( (x & ((1 << 29) - 1)) << 32 ); } ll mul(ll a, ll b) { // 2^61 = 1 // a = a1 a0, b = b1 b0, a * b = 2^64 * a1 * b1 + 2^32 * (a1 * b0 + a0 * b1) + a0 * b0 // = 2^3 * mod(a1 * b1) ll a0 = a & mask; ll a1 = a >> 32; ll b0 = b & mask; ll b1 = b >> 32; return (modq(modq(a1 * b1) << 3) + mod32q(a1 * b0) + mod32q(a0 * b1) + modq(a0 * b0)) % q; } // tu moznaby dac szybkie potegowanie ale // i tak kazdy procek poteguje ze trzy razy tylko ll power(ll p, ll i) { if (i == 0) return 1; ll p2 = power(p, i / 2); ll result = mul(p2, p2); if (i & 1) result = mul(result, p); return result; } ll slowpower(ll p, ll i) { ll result=1; for(int j=0; j<i; ++j) result=mul(result,p); return result; } ll HashS() { int intLen=s/non+1; int myIntBegin=myNum*intLen; ll h=0; for(int i = min(myIntBegin+intLen,s)-1; i >= myIntBegin;--i) { h=mul(h,p); h=(h+SignalAt(i+1))%q; } ll hPref=h; if(myNum!=0) { int instance = Receive(myNum-1); ll hash_prev=GetLL(instance); hPref=(hash_prev+mul(power(p,myIntBegin),h))%q; } if(myNum!=non-1) { PutLL(myNum+1,hPref); Send(myNum+1); int instance = Receive(non-1); hPref=GetLL(instance); } // w tym momencie non-1 ma hash sygnalu else for(int i=0; i<non-1; ++i) { PutLL(i,hPref); Send(i); } return hPref; } pair<ll,ll> HashMSuff() { int intLen=m/non+1; int r=(s-1)%intLen; int myIntBegin=myNum*intLen; ll h=0; ll hr=0; for(int i = min(myIntBegin+intLen,m)-1; i >= myIntBegin;--i) { h=mul(h,p); h=(h+SeqAt(i+1))%q; //cout << i << " , " << h << endl; if(i==myIntBegin+r) hr=h; } //cout << "hr = " << hr << " my piece = " << h << endl; ll hSuff=h; ll hash_next=0; if(myNum!=non-1) { int instance = Receive(myNum+1); hash_next=GetLL(instance); hSuff=(mul(hash_next,power(p,intLen))+h)%q; hr=(mul(hash_next,power(p,intLen-r))+hr)%q; } if(myNum!=0) { PutLL(myNum-1,hSuff); Send(myNum-1); } return pair<ll,ll>(hr, hash_next); } int main() { s=SignalLength(); m=SeqLength(); non=NumberOfNodes(); myNum=MyNodeId(); // kazdy sie dowiaduje jaki jest hash sygnalu ll signal_h=HashS(); pair<ll,ll> suffPair=HashMSuff(); ll hr=suffPair.first; ll hash_next=suffPair.second; int intLen=m/non+1; int r=(s-1)%intLen; int myIntBegin=myNum*intLen; if(myIntBegin+r >= s) { int address=(myIntBegin+r-s)/intLen; PutLL(address,hr); Send(address); } ll hr_next=0; if(myIntBegin+intLen-1+s < non*intLen) { // jesli spodziewamy sie wiadomosci int instance = Receive((myIntBegin+intLen-1+s)/intLen); hr_next=GetLL(instance); } int counter=0; ll pp=power(p,s); for(int i=min(m,myIntBegin+intLen)-1; i>=myIntBegin; --i) { hash_next=(mul(hash_next,p)+SeqAt(i+1))%q; if(i+s <= m) { // znaczy miescimy sie if(hash_next == (signal_h + mul(hr_next,pp))%q ) counter++; hr_next=(mul(hr_next,p) + SeqAt(i+s))%q; } } // zbieranie wyników if(myNum!=0) { PutInt(0,counter); Send(0); } else { int result=counter; for(int i=1; i<non; ++i) { int instance=Receive(i); result += GetInt(instance); } printf("%d",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 | #include<cstdio> #include<algorithm> #include "message.h" #include "poszukiwania.h" #include<utility> #include<iostream> typedef unsigned long long int ll; using namespace std; int s,m, myNum, non; const ll p=1063; // a small prime number const ll q=(1ULL<<61)-1; // a big prime number const ll mask = (1ULL << 32) - 1; ll modq(ll x) { // x % q // x = 2^61 * (x >> 61) + x mod 2^61 return (x >> 61) + (x & q); } ll mod32q(ll x) { // 2^32 * x % q // x = 2^29 * (x >> 29) + x mod 2^29 // 2^32 * x = 2^61 * (x >> 29) + (x mod 2^29) << 32 return (x >> 29) + ( (x & ((1 << 29) - 1)) << 32 ); } ll mul(ll a, ll b) { // 2^61 = 1 // a = a1 a0, b = b1 b0, a * b = 2^64 * a1 * b1 + 2^32 * (a1 * b0 + a0 * b1) + a0 * b0 // = 2^3 * mod(a1 * b1) ll a0 = a & mask; ll a1 = a >> 32; ll b0 = b & mask; ll b1 = b >> 32; return (modq(modq(a1 * b1) << 3) + mod32q(a1 * b0) + mod32q(a0 * b1) + modq(a0 * b0)) % q; } // tu moznaby dac szybkie potegowanie ale // i tak kazdy procek poteguje ze trzy razy tylko ll power(ll p, ll i) { if (i == 0) return 1; ll p2 = power(p, i / 2); ll result = mul(p2, p2); if (i & 1) result = mul(result, p); return result; } ll slowpower(ll p, ll i) { ll result=1; for(int j=0; j<i; ++j) result=mul(result,p); return result; } ll HashS() { int intLen=s/non+1; int myIntBegin=myNum*intLen; ll h=0; for(int i = min(myIntBegin+intLen,s)-1; i >= myIntBegin;--i) { h=mul(h,p); h=(h+SignalAt(i+1))%q; } ll hPref=h; if(myNum!=0) { int instance = Receive(myNum-1); ll hash_prev=GetLL(instance); hPref=(hash_prev+mul(power(p,myIntBegin),h))%q; } if(myNum!=non-1) { PutLL(myNum+1,hPref); Send(myNum+1); int instance = Receive(non-1); hPref=GetLL(instance); } // w tym momencie non-1 ma hash sygnalu else for(int i=0; i<non-1; ++i) { PutLL(i,hPref); Send(i); } return hPref; } pair<ll,ll> HashMSuff() { int intLen=m/non+1; int r=(s-1)%intLen; int myIntBegin=myNum*intLen; ll h=0; ll hr=0; for(int i = min(myIntBegin+intLen,m)-1; i >= myIntBegin;--i) { h=mul(h,p); h=(h+SeqAt(i+1))%q; //cout << i << " , " << h << endl; if(i==myIntBegin+r) hr=h; } //cout << "hr = " << hr << " my piece = " << h << endl; ll hSuff=h; ll hash_next=0; if(myNum!=non-1) { int instance = Receive(myNum+1); hash_next=GetLL(instance); hSuff=(mul(hash_next,power(p,intLen))+h)%q; hr=(mul(hash_next,power(p,intLen-r))+hr)%q; } if(myNum!=0) { PutLL(myNum-1,hSuff); Send(myNum-1); } return pair<ll,ll>(hr, hash_next); } int main() { s=SignalLength(); m=SeqLength(); non=NumberOfNodes(); myNum=MyNodeId(); // kazdy sie dowiaduje jaki jest hash sygnalu ll signal_h=HashS(); pair<ll,ll> suffPair=HashMSuff(); ll hr=suffPair.first; ll hash_next=suffPair.second; int intLen=m/non+1; int r=(s-1)%intLen; int myIntBegin=myNum*intLen; if(myIntBegin+r >= s) { int address=(myIntBegin+r-s)/intLen; PutLL(address,hr); Send(address); } ll hr_next=0; if(myIntBegin+intLen-1+s < non*intLen) { // jesli spodziewamy sie wiadomosci int instance = Receive((myIntBegin+intLen-1+s)/intLen); hr_next=GetLL(instance); } int counter=0; ll pp=power(p,s); for(int i=min(m,myIntBegin+intLen)-1; i>=myIntBegin; --i) { hash_next=(mul(hash_next,p)+SeqAt(i+1))%q; if(i+s <= m) { // znaczy miescimy sie if(hash_next == (signal_h + mul(hr_next,pp))%q ) counter++; hr_next=(mul(hr_next,p) + SeqAt(i+s))%q; } } // zbieranie wyników if(myNum!=0) { PutInt(0,counter); Send(0); } else { int result=counter; for(int i=1; i<non; ++i) { int instance=Receive(i); result += GetInt(instance); } printf("%d",result); } return 0; } |