#include "poszukiwania.h" #include "message.h" #include<bits/stdc++.h> using namespace std; typedef pair<int,int> PI; typedef __int128_t LL; typedef double D; #define FI first #define SE second #define MP make_pair #define PB push_back #define R(I,N) for(int I=0;I<N;I++) #define F(I,A,B) for(int I=A;I<B;I++) #define FD(I,N) for(int I=N-1;I>=0;I--) #define make(A) scanf("%d",&A) #define make2(A,B) scanf("%d%d",&A,&B) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define db if(1)printf template<typename C> void MA(C& a,C b){if(a<b)a=b;} template<typename C> void MI(C& a,C b){if(a>b)a=b;} int proc,id; const long long x = 76291622609ll; const long long P = (long long)(1e9+7) * (long long)(1e9+9); int n,m; LL pot(LL a,LL w){ LL r = 1; while(w){ if(w&1){ r*=a; r%=P; } w/=2; a*=a; a%=P; } return r; } LL h; void sumujh(){ if(id == 0){ F(i,1,proc){ Receive(i); h += GetLL(i); } h%=P; F(i,1,proc){ PutLL(i,h); Send(i); } }else{ PutLL(0,h); Send(0); Receive(0); h = GetLL(0); } } void hash_sygnalu(int po,int ko){ F(i,po,ko){ int pom = SignalAt(i+1); h *= x; h += pom; h %= P; } h = h*pot(x,n-ko)%P; sumujh(); } int wyn; LL g; unordered_set<int> d; int il; void hash_seq(int po,int ko){ int nn = n % il + po; bool cz = 0; F(i,po,ko){ if(i == nn){ cz = 1; PutLL(0,g); } int pom = SeqAt(i+1); g *= x; g += pom; g %= P; } if(cz == 0)PutLL(0,0); PutLL(0,g); Send(0); if(id == 0){ LL mn = pot(x,il); LL mn2 = pot(x,n%il); int pom = n/il; LL ak = 0; int ost = -1; R(i,proc){ PutLL(i,ak); Receive(i); if(i >= pom){ ost = i - pom; PutLL(i-pom,(ak*mn2 + GetLL(i))%P); }else GetLL(i); ak *= mn; ak += GetLL(i); ak %= P; } F(i,ost+1,proc)PutLL(i,0ll); R(i,proc)Send(i); } Receive(0); LL poh = GetLL(0); LL koh = GetLL(0); LL pp = pot(x,n); if(po + n > m)return; F(i,po,ko){ // printf("%d: spr %d %lld %lld\n",id,i,(long long)poh, (long long)koh); if((poh*pp + h)%P == koh)wyn ++; if(i + n >= m)return; poh *= x; poh += SeqAt(i+1); poh %= P; koh *= x; koh += SeqAt(i+n+1); koh %= P; } } main(){ proc = NumberOfNodes(); id = MyNodeId(); n = SignalLength(); m = SeqLength(); il = (n+proc-1)/proc; hash_sygnalu(min(il*id,n), min(il*(id+1),n)); il = (m+proc-1)/proc; hash_seq(min(il*id,m), min(il*(id+1),m)); if(id == 0){ F(i,1,proc){ Receive(i); wyn += GetInt(i); } printf("%d\n",wyn); }else{ PutInt(0,wyn); Send(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 | #include "poszukiwania.h" #include "message.h" #include<bits/stdc++.h> using namespace std; typedef pair<int,int> PI; typedef __int128_t LL; typedef double D; #define FI first #define SE second #define MP make_pair #define PB push_back #define R(I,N) for(int I=0;I<N;I++) #define F(I,A,B) for(int I=A;I<B;I++) #define FD(I,N) for(int I=N-1;I>=0;I--) #define make(A) scanf("%d",&A) #define make2(A,B) scanf("%d%d",&A,&B) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define db if(1)printf template<typename C> void MA(C& a,C b){if(a<b)a=b;} template<typename C> void MI(C& a,C b){if(a>b)a=b;} int proc,id; const long long x = 76291622609ll; const long long P = (long long)(1e9+7) * (long long)(1e9+9); int n,m; LL pot(LL a,LL w){ LL r = 1; while(w){ if(w&1){ r*=a; r%=P; } w/=2; a*=a; a%=P; } return r; } LL h; void sumujh(){ if(id == 0){ F(i,1,proc){ Receive(i); h += GetLL(i); } h%=P; F(i,1,proc){ PutLL(i,h); Send(i); } }else{ PutLL(0,h); Send(0); Receive(0); h = GetLL(0); } } void hash_sygnalu(int po,int ko){ F(i,po,ko){ int pom = SignalAt(i+1); h *= x; h += pom; h %= P; } h = h*pot(x,n-ko)%P; sumujh(); } int wyn; LL g; unordered_set<int> d; int il; void hash_seq(int po,int ko){ int nn = n % il + po; bool cz = 0; F(i,po,ko){ if(i == nn){ cz = 1; PutLL(0,g); } int pom = SeqAt(i+1); g *= x; g += pom; g %= P; } if(cz == 0)PutLL(0,0); PutLL(0,g); Send(0); if(id == 0){ LL mn = pot(x,il); LL mn2 = pot(x,n%il); int pom = n/il; LL ak = 0; int ost = -1; R(i,proc){ PutLL(i,ak); Receive(i); if(i >= pom){ ost = i - pom; PutLL(i-pom,(ak*mn2 + GetLL(i))%P); }else GetLL(i); ak *= mn; ak += GetLL(i); ak %= P; } F(i,ost+1,proc)PutLL(i,0ll); R(i,proc)Send(i); } Receive(0); LL poh = GetLL(0); LL koh = GetLL(0); LL pp = pot(x,n); if(po + n > m)return; F(i,po,ko){ // printf("%d: spr %d %lld %lld\n",id,i,(long long)poh, (long long)koh); if((poh*pp + h)%P == koh)wyn ++; if(i + n >= m)return; poh *= x; poh += SeqAt(i+1); poh %= P; koh *= x; koh += SeqAt(i+n+1); koh %= P; } } main(){ proc = NumberOfNodes(); id = MyNodeId(); n = SignalLength(); m = SeqLength(); il = (n+proc-1)/proc; hash_sygnalu(min(il*id,n), min(il*(id+1),n)); il = (m+proc-1)/proc; hash_seq(min(il*id,m), min(il*(id+1),m)); if(id == 0){ F(i,1,proc){ Receive(i); wyn += GetInt(i); } printf("%d\n",wyn); }else{ PutInt(0,wyn); Send(0); } } |