#include "poszukiwania.h" #include "message.h" #include <algorithm> #include <iostream> using namespace std; //#include <cstdio> #include <vector> #define FOR(i, b, n) for ( int (i) = b; (i) < (n); (i)++) #define FORR(i, b, n) for ( (i) = b; (i) < (n); (i)++) #define REP(i, n) for ( int (i) = 0; (i) < (n); (i)++) #define SC(n) scanf("%d", &(n)) #define SC2(n, m) scanf("%d%d", &(n), &(m)) #define SCLL(n) scanf("%lld", &(n)) #define PRT(n) printf("%d ", (n)) #define PRTLL(n) printf("%lld ", (n)) #define NXL printf("\n") typedef long long LL; typedef unsigned long long ULL; const int MANY = 3*10e7; //LL shorthash[MANY]; const int MUL = 31; const LL MOD = 1000000007; int N, S; LL modpow(LL a, int pow) { if(pow == 0) return 1; LL res = modpow(a, pow/2); res *= res; res %= MOD; if(pow % 2) { res *= a; res %= MOD; } return res; } LL count_hash_S(int beg, int end) { LL res = 0; FOR(i, beg, end) { res *= MUL; res += SignalAt(i+1); res %= MOD; } LL mn = modpow(MUL, S - end); res *= mn; res %= MOD; return res; } int count_short_hashes(int beg, int end, LL val) { const LL POT = modpow(MUL, S-1); //////// int howmany = 0; LL res = 0; REP(i, S) { if(beg+i>=N) break; res *= MUL; // cout << beg+i+1 << endl; res += SeqAt(beg+i+1); res %= MOD; } if(res == val) { // cout << beg << endl; howmany++; } //else //cout << "zle " << res << endl; for (int i=0; i<end-beg-S-1; i++) { res -= POT*SeqAt(beg+i+1); //cout << "a" << beg+i+1 << endl; res = (res%MOD + MOD)%MOD; res *= MUL; res += SeqAt(beg+S+i+1); // cout << "b" << beg+S+i+1 << endl; res %= MOD; if(res == val) { howmany++; //cout << "a" << beg+i << endl; } //else //cout << "zle " << res << endl; } return howmany; } int main() { N = SeqLength(); S = SignalLength(); int K = NumberOfNodes(); int me = MyNodeId(); int tp = (S+K-1)/K; int beg = me*tp; int end = min(S, (me+1)*tp); LL hash = count_hash_S(beg, end); if(me != 0) { PutLL(0, hash); Send(0); int tp = (N+K-2)/(K-1); int beg = (me-1)*tp; int end = min(N-S, (me)*tp); /////////; Receive(0); LL goodhash = GetLL(0); int howmany = count_short_hashes(beg, end, goodhash); PutInt(0, howmany); Send(0); } else { for (int i=1; i<K; i++) { int who = Receive(-1); hash += GetLL(who); hash %= MOD; } //cout << "hash " << hash << endl; for (int i=1; i<K; i++) { PutLL(i, hash); Send(i); } int res = 0; for (int i=1; i<K; i++) { int who = Receive(-1); res += GetInt(who); } cout << res << endl; } 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 | #include "poszukiwania.h" #include "message.h" #include <algorithm> #include <iostream> using namespace std; //#include <cstdio> #include <vector> #define FOR(i, b, n) for ( int (i) = b; (i) < (n); (i)++) #define FORR(i, b, n) for ( (i) = b; (i) < (n); (i)++) #define REP(i, n) for ( int (i) = 0; (i) < (n); (i)++) #define SC(n) scanf("%d", &(n)) #define SC2(n, m) scanf("%d%d", &(n), &(m)) #define SCLL(n) scanf("%lld", &(n)) #define PRT(n) printf("%d ", (n)) #define PRTLL(n) printf("%lld ", (n)) #define NXL printf("\n") typedef long long LL; typedef unsigned long long ULL; const int MANY = 3*10e7; //LL shorthash[MANY]; const int MUL = 31; const LL MOD = 1000000007; int N, S; LL modpow(LL a, int pow) { if(pow == 0) return 1; LL res = modpow(a, pow/2); res *= res; res %= MOD; if(pow % 2) { res *= a; res %= MOD; } return res; } LL count_hash_S(int beg, int end) { LL res = 0; FOR(i, beg, end) { res *= MUL; res += SignalAt(i+1); res %= MOD; } LL mn = modpow(MUL, S - end); res *= mn; res %= MOD; return res; } int count_short_hashes(int beg, int end, LL val) { const LL POT = modpow(MUL, S-1); //////// int howmany = 0; LL res = 0; REP(i, S) { if(beg+i>=N) break; res *= MUL; // cout << beg+i+1 << endl; res += SeqAt(beg+i+1); res %= MOD; } if(res == val) { // cout << beg << endl; howmany++; } //else //cout << "zle " << res << endl; for (int i=0; i<end-beg-S-1; i++) { res -= POT*SeqAt(beg+i+1); //cout << "a" << beg+i+1 << endl; res = (res%MOD + MOD)%MOD; res *= MUL; res += SeqAt(beg+S+i+1); // cout << "b" << beg+S+i+1 << endl; res %= MOD; if(res == val) { howmany++; //cout << "a" << beg+i << endl; } //else //cout << "zle " << res << endl; } return howmany; } int main() { N = SeqLength(); S = SignalLength(); int K = NumberOfNodes(); int me = MyNodeId(); int tp = (S+K-1)/K; int beg = me*tp; int end = min(S, (me+1)*tp); LL hash = count_hash_S(beg, end); if(me != 0) { PutLL(0, hash); Send(0); int tp = (N+K-2)/(K-1); int beg = (me-1)*tp; int end = min(N-S, (me)*tp); /////////; Receive(0); LL goodhash = GetLL(0); int howmany = count_short_hashes(beg, end, goodhash); PutInt(0, howmany); Send(0); } else { for (int i=1; i<K; i++) { int who = Receive(-1); hash += GetLL(who); hash %= MOD; } //cout << "hash " << hash << endl; for (int i=1; i<K; i++) { PutLL(i, hash); Send(i); } int res = 0; for (int i=1; i<K; i++) { int who = Receive(-1); res += GetInt(who); } cout << res << endl; } return 0; } |