#include "message.h" #include "poszukiwania.h" #include <cstdio> #include <vector> #include <algorithm> using namespace std; #define LL long long #define int128 LL//__int128 #define MOD1 2000000011LL #define BASE1 1000000009LL #define MOD2 2000000033LL #define BASE2 1000000007LL #define MOD3 2000000063LL #define BASE3 1000000013LL LL S,M; int nodeId,numberOfNodes; inline int128 pow(int128 a,int128 b,int128 m){ int128 ret=1; while(b!=0){ if(b&1)ret=(ret*a)%m; a=(a*a)%m; b>>=1; } return ret; } inline int128 mod(int128 a,int128 b){ int128 c=a%b; if(c<0)return c+b; return c; } int main(){ nodeId=MyNodeId(); numberOfNodes=NumberOfNodes(); S=SignalLength(); M=SeqLength(); LL l=nodeId*S/numberOfNodes; LL r=(nodeId+1)*S/numberOfNodes; int128 hash1=0; int128 base1=pow(BASE1,l,MOD1); int128 hash2=0; int128 base2=pow(BASE2,l,MOD2); int128 hash3=0; int128 base3=pow(BASE3,l,MOD3); for(LL i=l;i<r;++i){ LL v=SignalAt(i+1)+1; hash1=(hash1+base1*v)%MOD1; base1=(base1*BASE1)%MOD1; hash2=(hash2+base2*v)%MOD2; base2=(base2*BASE2)%MOD2; hash3=(hash3+base3*v)%MOD3; base3=(base3*BASE3)%MOD3; } if(nodeId!=0){ PutLL(0,hash1); PutLL(0,hash2); PutLL(0,hash3); Send(0); Receive(0); hash1=GetLL(0); hash2=GetLL(0); hash3=GetLL(0); }else{ for(int i=1;i<numberOfNodes;++i){ Receive(i); LL h1=GetLL(i); LL h2=GetLL(i); LL h3=GetLL(i); hash1=(hash1+h1)%MOD1; hash2=(hash2+h2)%MOD2; hash3=(hash3+h3)%MOD3; } for(int i=1;i<numberOfNodes;++i){ PutLL(i,hash1); PutLL(i,hash2); PutLL(i,hash3); Send(i); } } LL sigHash1=hash1; LL sigHash2=hash2; LL sigHash3=hash3; numberOfNodes=min((LL)numberOfNodes,M); if(nodeId>=numberOfNodes)return 0; vector<pair<int,pair<LL,pair<LL,LL> > > > toSend; l=nodeId*M/numberOfNodes; r=(nodeId+1)*M/numberOfNodes; LL pow1=pow(BASE1,l,MOD1); LL pow2=pow(BASE2,l,MOD2); LL pow3=pow(BASE3,l,MOD3); hash1=0; base1=pow1; hash2=0; base2=pow2; hash3=0; base3=pow3; LL p=0; int source=-1; for(LL i=l;i<r;++i){ while(p*M/numberOfNodes<i+S){ if(p*M/numberOfNodes<=l-S)source=p; ++p; } if(p<numberOfNodes && p*M/numberOfNodes==(i+S)){ toSend.push_back(make_pair(p,make_pair(hash1,make_pair(hash2,hash3)))); } LL v=SeqAt(i+1)+1; hash1=(hash1+base1*v)%MOD1; base1=(base1*BASE1)%MOD1; hash2=(hash2+base2*v)%MOD2; base2=(base2*BASE2)%MOD2; hash3=(hash3+base3*v)%MOD3; base3=(base3*BASE3)%MOD3; } int128 prefHash1=0; int128 prefHash2=0; int128 prefHash3=0; if(nodeId!=0){ PutLL(0,hash1); PutLL(0,hash2); PutLL(0,hash3); Send(0); Receive(0); prefHash1=GetLL(0); prefHash2=GetLL(0); prefHash3=GetLL(0); }else{ prefHash1=0; prefHash2=0; prefHash3=0; for(int i=1;i<numberOfNodes;++i){ PutLL(i,hash1); PutLL(i,hash2); PutLL(i,hash3); Send(i); Receive(i); LL h1=GetLL(i); LL h2=GetLL(i); LL h3=GetLL(i); hash1=(hash1+h1)%MOD1; hash2=(hash2+h2)%MOD2; hash3=(hash3+h3)%MOD3; } } for(pair<int,pair<LL,pair<LL,LL> > > p:toSend){ PutLL(p.first,(prefHash1+p.second.first)%MOD1); PutLL(p.first,(prefHash2+p.second.second.first)%MOD2); PutLL(p.first,(prefHash3+p.second.second.second)%MOD3); Send(p.first); } int128 lhash1=0; int128 lbase1=1; int128 lhash2=0; int128 lbase2=1; int128 lhash3=0; int128 lbase3=1; hash1=prefHash1; hash2=prefHash2; hash3=prefHash3; base1=pow1; base2=pow2; base3=pow3; LL i=l; LL ans=0; if(l-S>=0){ //assert(source!=-1); Receive(source); lhash1=GetLL(source); lbase1=pow(BASE1,i-S,MOD1); lhash2=GetLL(source); lbase2=pow(BASE2,i-S,MOD2); lhash3=GetLL(source); lbase3=pow(BASE3,i-S,MOD3); }else{ if(S<=r){ for(;i<S;++i){ LL v=SeqAt(i+1)+1; hash1=(hash1+base1*v)%MOD1; base1=(base1*BASE1)%MOD1; hash2=(hash2+base2*v)%MOD2; base2=(base2*BASE2)%MOD2; hash3=(hash3+base3*v)%MOD3; base3=(base3*BASE3)%MOD3; } }else i=r; } for(;i<r;++i){ if(mod(hash1-lhash1,MOD1)==(sigHash1*lbase1)%MOD1 && mod(hash2-lhash2,MOD2)==(sigHash2*lbase2)%MOD2 && mod(hash3-lhash3,MOD3)==(sigHash3*lbase3)%MOD3) ++ans; LL v=SeqAt(i+1)+1; hash1=(hash1+base1*v)%MOD1; base1=(base1*BASE1)%MOD1; hash2=(hash2+base2*v)%MOD2; base2=(base2*BASE2)%MOD2; hash3=(hash3+base3*v)%MOD3; base3=(base3*BASE3)%MOD3; v=SeqAt(i-S+1)+1; lhash1=(lhash1+lbase1*v)%MOD1; lbase1=(lbase1*BASE1)%MOD1; lhash2=(lhash2+lbase2*v)%MOD2; lbase2=(lbase2*BASE2)%MOD2; lhash3=(lhash3+lbase3*v)%MOD3; lbase3=(lbase3*BASE3)%MOD3; } if(nodeId==numberOfNodes-1){ if(mod(hash1-lhash1,MOD1)==(sigHash1*lbase1)%MOD1 && mod(hash2-lhash2,MOD2)==(sigHash2*lbase2)%MOD2 && mod(hash3-lhash3,MOD3)==(sigHash3*lbase3)%MOD3) ++ans; } if(nodeId!=0){ PutInt(0,ans); Send(0); }else{ for(int i=1;i<numberOfNodes;++i){ Receive(i); ans+=GetInt(i); } printf("%d",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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | #include "message.h" #include "poszukiwania.h" #include <cstdio> #include <vector> #include <algorithm> using namespace std; #define LL long long #define int128 LL//__int128 #define MOD1 2000000011LL #define BASE1 1000000009LL #define MOD2 2000000033LL #define BASE2 1000000007LL #define MOD3 2000000063LL #define BASE3 1000000013LL LL S,M; int nodeId,numberOfNodes; inline int128 pow(int128 a,int128 b,int128 m){ int128 ret=1; while(b!=0){ if(b&1)ret=(ret*a)%m; a=(a*a)%m; b>>=1; } return ret; } inline int128 mod(int128 a,int128 b){ int128 c=a%b; if(c<0)return c+b; return c; } int main(){ nodeId=MyNodeId(); numberOfNodes=NumberOfNodes(); S=SignalLength(); M=SeqLength(); LL l=nodeId*S/numberOfNodes; LL r=(nodeId+1)*S/numberOfNodes; int128 hash1=0; int128 base1=pow(BASE1,l,MOD1); int128 hash2=0; int128 base2=pow(BASE2,l,MOD2); int128 hash3=0; int128 base3=pow(BASE3,l,MOD3); for(LL i=l;i<r;++i){ LL v=SignalAt(i+1)+1; hash1=(hash1+base1*v)%MOD1; base1=(base1*BASE1)%MOD1; hash2=(hash2+base2*v)%MOD2; base2=(base2*BASE2)%MOD2; hash3=(hash3+base3*v)%MOD3; base3=(base3*BASE3)%MOD3; } if(nodeId!=0){ PutLL(0,hash1); PutLL(0,hash2); PutLL(0,hash3); Send(0); Receive(0); hash1=GetLL(0); hash2=GetLL(0); hash3=GetLL(0); }else{ for(int i=1;i<numberOfNodes;++i){ Receive(i); LL h1=GetLL(i); LL h2=GetLL(i); LL h3=GetLL(i); hash1=(hash1+h1)%MOD1; hash2=(hash2+h2)%MOD2; hash3=(hash3+h3)%MOD3; } for(int i=1;i<numberOfNodes;++i){ PutLL(i,hash1); PutLL(i,hash2); PutLL(i,hash3); Send(i); } } LL sigHash1=hash1; LL sigHash2=hash2; LL sigHash3=hash3; numberOfNodes=min((LL)numberOfNodes,M); if(nodeId>=numberOfNodes)return 0; vector<pair<int,pair<LL,pair<LL,LL> > > > toSend; l=nodeId*M/numberOfNodes; r=(nodeId+1)*M/numberOfNodes; LL pow1=pow(BASE1,l,MOD1); LL pow2=pow(BASE2,l,MOD2); LL pow3=pow(BASE3,l,MOD3); hash1=0; base1=pow1; hash2=0; base2=pow2; hash3=0; base3=pow3; LL p=0; int source=-1; for(LL i=l;i<r;++i){ while(p*M/numberOfNodes<i+S){ if(p*M/numberOfNodes<=l-S)source=p; ++p; } if(p<numberOfNodes && p*M/numberOfNodes==(i+S)){ toSend.push_back(make_pair(p,make_pair(hash1,make_pair(hash2,hash3)))); } LL v=SeqAt(i+1)+1; hash1=(hash1+base1*v)%MOD1; base1=(base1*BASE1)%MOD1; hash2=(hash2+base2*v)%MOD2; base2=(base2*BASE2)%MOD2; hash3=(hash3+base3*v)%MOD3; base3=(base3*BASE3)%MOD3; } int128 prefHash1=0; int128 prefHash2=0; int128 prefHash3=0; if(nodeId!=0){ PutLL(0,hash1); PutLL(0,hash2); PutLL(0,hash3); Send(0); Receive(0); prefHash1=GetLL(0); prefHash2=GetLL(0); prefHash3=GetLL(0); }else{ prefHash1=0; prefHash2=0; prefHash3=0; for(int i=1;i<numberOfNodes;++i){ PutLL(i,hash1); PutLL(i,hash2); PutLL(i,hash3); Send(i); Receive(i); LL h1=GetLL(i); LL h2=GetLL(i); LL h3=GetLL(i); hash1=(hash1+h1)%MOD1; hash2=(hash2+h2)%MOD2; hash3=(hash3+h3)%MOD3; } } for(pair<int,pair<LL,pair<LL,LL> > > p:toSend){ PutLL(p.first,(prefHash1+p.second.first)%MOD1); PutLL(p.first,(prefHash2+p.second.second.first)%MOD2); PutLL(p.first,(prefHash3+p.second.second.second)%MOD3); Send(p.first); } int128 lhash1=0; int128 lbase1=1; int128 lhash2=0; int128 lbase2=1; int128 lhash3=0; int128 lbase3=1; hash1=prefHash1; hash2=prefHash2; hash3=prefHash3; base1=pow1; base2=pow2; base3=pow3; LL i=l; LL ans=0; if(l-S>=0){ //assert(source!=-1); Receive(source); lhash1=GetLL(source); lbase1=pow(BASE1,i-S,MOD1); lhash2=GetLL(source); lbase2=pow(BASE2,i-S,MOD2); lhash3=GetLL(source); lbase3=pow(BASE3,i-S,MOD3); }else{ if(S<=r){ for(;i<S;++i){ LL v=SeqAt(i+1)+1; hash1=(hash1+base1*v)%MOD1; base1=(base1*BASE1)%MOD1; hash2=(hash2+base2*v)%MOD2; base2=(base2*BASE2)%MOD2; hash3=(hash3+base3*v)%MOD3; base3=(base3*BASE3)%MOD3; } }else i=r; } for(;i<r;++i){ if(mod(hash1-lhash1,MOD1)==(sigHash1*lbase1)%MOD1 && mod(hash2-lhash2,MOD2)==(sigHash2*lbase2)%MOD2 && mod(hash3-lhash3,MOD3)==(sigHash3*lbase3)%MOD3) ++ans; LL v=SeqAt(i+1)+1; hash1=(hash1+base1*v)%MOD1; base1=(base1*BASE1)%MOD1; hash2=(hash2+base2*v)%MOD2; base2=(base2*BASE2)%MOD2; hash3=(hash3+base3*v)%MOD3; base3=(base3*BASE3)%MOD3; v=SeqAt(i-S+1)+1; lhash1=(lhash1+lbase1*v)%MOD1; lbase1=(lbase1*BASE1)%MOD1; lhash2=(lhash2+lbase2*v)%MOD2; lbase2=(lbase2*BASE2)%MOD2; lhash3=(lhash3+lbase3*v)%MOD3; lbase3=(lbase3*BASE3)%MOD3; } if(nodeId==numberOfNodes-1){ if(mod(hash1-lhash1,MOD1)==(sigHash1*lbase1)%MOD1 && mod(hash2-lhash2,MOD2)==(sigHash2*lbase2)%MOD2 && mod(hash3-lhash3,MOD3)==(sigHash3*lbase3)%MOD3) ++ans; } if(nodeId!=0){ PutInt(0,ans); Send(0); }else{ for(int i=1;i<numberOfNodes;++i){ Receive(i); ans+=GetInt(i); } printf("%d",ans); } return 0; } |