#include "poszukiwania.h" #include <message.h> #include <cstring> #include <cstdio> #include <algorithm> #include <vector> using namespace std; void recv_data( int src, void *buf, size_t size ) { int *s = (int*)buf; for ( int i=0; i<size/4; i++ ) { s[i] = GetInt( src ); } } #define recv_struct( src, s ) recv_data( src, (void*)&s, sizeof(s) ) void send_data( int dst, void *buf, size_t size ) { int *s = (int*)buf; for ( int i=0; i<size/4; i++ ) { PutInt( dst, s[i] ); } Send(dst); } #define send_struct( dst, s ) send_data( dst, (void*)&s, sizeof(s) ) #define HMOD (1000000000000000000ULL+19) #define HMUL (1000000000ULL+9) typedef unsigned long long ull; class hash_t { public: hash_t() {} __uint128_t h = 0; __uint128_t mul = 1; inline void operator+=( const hash_t &h2 ) { mul = (mul*h2.mul)%HMOD; h = (h*h2.mul+h2.h)%HMOD; } inline void operator+=( const ull &a ) { mul = (mul*HMUL)%HMOD; h = (h*HMUL+a)%HMOD; } inline void operator*=( const __uint128_t &a ) { mul = (mul*a)%HMOD; h = (h*a)%HMOD; } inline hash_t operator-( const hash_t &h2 ) { hash_t r; r.mul = mul; r.h = (h+HMOD-h2.h)%HMOD; return r; } inline void operator-=( const __uint128_t &a ) { h = (h-a+((__uint128_t)HMOD)*HMOD)%HMOD; } inline bool operator==( const hash_t &h2 ) { return ( h==h2.h /*&& mul==h2.mul*/ ); } }; __uint128_t hpow_mod( int p ) { __uint128_t r = 1; __uint128_t mul = HMUL; while ( p ) { if ( p&1 ) r = (r*mul)%HMOD; mul = (mul*mul)%HMOD; p>>=1; } return r; } typedef class { public: hash_t p; hash_t t; } beg_descr; beg_descr ins_data[ 10005 ]; hash_t blocks[ 10005 ]; int main() { int n = SeqLength(); int m = SignalLength(); int inst_count = NumberOfNodes(); int inst = MyNodeId(); /*{ hash_t test_h[ 1000 ]; hash_t h; for ( int i=1; i<=n; i++ ) { h += SeqAt( i ); test_h[i]=h; } printf("%llu\n",); printf("%llu\n",); return 0; }*/ int b,e; { beg_descr d; b = ((ull)m)*(inst)/inst_count; e = ((ull)m)*(inst+1)/inst_count; for ( int i=b+1; i<=e; i++ ) d.p += (ull)SignalAt( i ); b = ((ull)n)*(inst)/inst_count; e = ((ull)n)*(inst+1)/inst_count; for ( int i=b+1; i<=e; i++ ) d.t += (ull)SeqAt( i ); send_struct( 0, d ); } if ( !inst ) { for ( int i=0; i<inst_count; i++ ) { Receive( i ); recv_struct( i, ins_data[i] ); } hash_t p_hash; for ( int i=0; i<inst_count; i++ ) { p_hash += ins_data[i].p; blocks[i] = ins_data[i].t; } blocks[ inst_count ] = p_hash; for ( int i=0; i<inst_count; i++ ) send_data( i, blocks, (inst_count+1)*sizeof(*blocks) ); } Receive( 0 ); recv_data( 0, blocks, (inst_count+1)*sizeof(*blocks) ); { int w = 0; hash_t h_p = blocks[ inst_count ]; hash_t h; int wsk_inst = inst; for ( ; (((long long)n)*(wsk_inst+1)/inst_count)<b+m; wsk_inst++ ) h += blocks[ wsk_inst ]; //printf(" %d %d\n",inst,wsk_inst); //printf(" %llu\n",(ull)h.h); int wsk = ((long long)n)*(wsk_inst)/inst_count +1; while ( wsk<b+m&&wsk<=n ) h += (ull)SeqAt( wsk++ ); //hash_t h_b; __uint128_t minus_mul = hpow_mod( m-1 ); for ( int i=b+1; i<=e&&wsk<=n; i++ ) { h += SeqAt( wsk++ ); if ( h == h_p ) { w++; /*fprintf(stderr,"%d\n",i);*/ } //printf(" %llu %llu\n",(ull)h.h,(ull)h_p.h); h -= minus_mul * SeqAt( i ); } send_struct( 0, w ); } //fprintf( stderr, "aaaaaaaa\n" ); if ( !inst ) { int w = 0; for ( int i=0; i<inst_count; i++ ) { int a = Receive( -1 ); int b; recv_struct( a, b ); w += b; } printf("%d\n",w); } 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 | #include "poszukiwania.h" #include <message.h> #include <cstring> #include <cstdio> #include <algorithm> #include <vector> using namespace std; void recv_data( int src, void *buf, size_t size ) { int *s = (int*)buf; for ( int i=0; i<size/4; i++ ) { s[i] = GetInt( src ); } } #define recv_struct( src, s ) recv_data( src, (void*)&s, sizeof(s) ) void send_data( int dst, void *buf, size_t size ) { int *s = (int*)buf; for ( int i=0; i<size/4; i++ ) { PutInt( dst, s[i] ); } Send(dst); } #define send_struct( dst, s ) send_data( dst, (void*)&s, sizeof(s) ) #define HMOD (1000000000000000000ULL+19) #define HMUL (1000000000ULL+9) typedef unsigned long long ull; class hash_t { public: hash_t() {} __uint128_t h = 0; __uint128_t mul = 1; inline void operator+=( const hash_t &h2 ) { mul = (mul*h2.mul)%HMOD; h = (h*h2.mul+h2.h)%HMOD; } inline void operator+=( const ull &a ) { mul = (mul*HMUL)%HMOD; h = (h*HMUL+a)%HMOD; } inline void operator*=( const __uint128_t &a ) { mul = (mul*a)%HMOD; h = (h*a)%HMOD; } inline hash_t operator-( const hash_t &h2 ) { hash_t r; r.mul = mul; r.h = (h+HMOD-h2.h)%HMOD; return r; } inline void operator-=( const __uint128_t &a ) { h = (h-a+((__uint128_t)HMOD)*HMOD)%HMOD; } inline bool operator==( const hash_t &h2 ) { return ( h==h2.h /*&& mul==h2.mul*/ ); } }; __uint128_t hpow_mod( int p ) { __uint128_t r = 1; __uint128_t mul = HMUL; while ( p ) { if ( p&1 ) r = (r*mul)%HMOD; mul = (mul*mul)%HMOD; p>>=1; } return r; } typedef class { public: hash_t p; hash_t t; } beg_descr; beg_descr ins_data[ 10005 ]; hash_t blocks[ 10005 ]; int main() { int n = SeqLength(); int m = SignalLength(); int inst_count = NumberOfNodes(); int inst = MyNodeId(); /*{ hash_t test_h[ 1000 ]; hash_t h; for ( int i=1; i<=n; i++ ) { h += SeqAt( i ); test_h[i]=h; } printf("%llu\n",); printf("%llu\n",); return 0; }*/ int b,e; { beg_descr d; b = ((ull)m)*(inst)/inst_count; e = ((ull)m)*(inst+1)/inst_count; for ( int i=b+1; i<=e; i++ ) d.p += (ull)SignalAt( i ); b = ((ull)n)*(inst)/inst_count; e = ((ull)n)*(inst+1)/inst_count; for ( int i=b+1; i<=e; i++ ) d.t += (ull)SeqAt( i ); send_struct( 0, d ); } if ( !inst ) { for ( int i=0; i<inst_count; i++ ) { Receive( i ); recv_struct( i, ins_data[i] ); } hash_t p_hash; for ( int i=0; i<inst_count; i++ ) { p_hash += ins_data[i].p; blocks[i] = ins_data[i].t; } blocks[ inst_count ] = p_hash; for ( int i=0; i<inst_count; i++ ) send_data( i, blocks, (inst_count+1)*sizeof(*blocks) ); } Receive( 0 ); recv_data( 0, blocks, (inst_count+1)*sizeof(*blocks) ); { int w = 0; hash_t h_p = blocks[ inst_count ]; hash_t h; int wsk_inst = inst; for ( ; (((long long)n)*(wsk_inst+1)/inst_count)<b+m; wsk_inst++ ) h += blocks[ wsk_inst ]; //printf(" %d %d\n",inst,wsk_inst); //printf(" %llu\n",(ull)h.h); int wsk = ((long long)n)*(wsk_inst)/inst_count +1; while ( wsk<b+m&&wsk<=n ) h += (ull)SeqAt( wsk++ ); //hash_t h_b; __uint128_t minus_mul = hpow_mod( m-1 ); for ( int i=b+1; i<=e&&wsk<=n; i++ ) { h += SeqAt( wsk++ ); if ( h == h_p ) { w++; /*fprintf(stderr,"%d\n",i);*/ } //printf(" %llu %llu\n",(ull)h.h,(ull)h_p.h); h -= minus_mul * SeqAt( i ); } send_struct( 0, w ); } //fprintf( stderr, "aaaaaaaa\n" ); if ( !inst ) { int w = 0; for ( int i=0; i<inst_count; i++ ) { int a = Receive( -1 ); int b; recv_struct( a, b ); w += b; } printf("%d\n",w); } return 0; } |