#include "palindromy.h" #include <cstdlib> #include <iostream> #include "message.h" int MASTER_NODE=0; long long brute1(int nr, int end) { int b = nr - 1; int e = nr + 1; long long res = 1; while(b>=0 && e <end) { if(GetLetter(b) == GetLetter(e)) { b--; e++; res++; } else break; } return res; } long long brute2(int nr, int end) { int b = nr; int e = nr+1; long long res = 0; while(b>=0 && e < end) { if(GetLetter(b) == GetLetter(e)) { b--; e++; res++; } else break; } return res; } int main() { int my_id = MyNodeId(); int N = GetLength(); int nodes = NumberOfNodes(); //nodes = 1; int range = (N + nodes - 1) / nodes; int begin = std::min(N, my_id * range); int end = std::min(N, (my_id+1) * range); long long res1 = 0; long long res2 = 0; for(int i = begin; i < end; i++) { res1 += brute1(i, N); res2 += brute2(i, N); } if(my_id != MASTER_NODE && my_id < nodes) { PutLL(MASTER_NODE, res1); PutLL(MASTER_NODE, res2); Send(MASTER_NODE); } else { for(int node = 1; node < nodes; node++) { Receive(node); res1 += GetLL(node); res2 += GetLL(node); } } if(my_id == 0) { //printf("%lld %lld %lld\n", res1+res2, res1, res2); printf("%lld\n", res1+res2); } 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 | #include "palindromy.h" #include <cstdlib> #include <iostream> #include "message.h" int MASTER_NODE=0; long long brute1(int nr, int end) { int b = nr - 1; int e = nr + 1; long long res = 1; while(b>=0 && e <end) { if(GetLetter(b) == GetLetter(e)) { b--; e++; res++; } else break; } return res; } long long brute2(int nr, int end) { int b = nr; int e = nr+1; long long res = 0; while(b>=0 && e < end) { if(GetLetter(b) == GetLetter(e)) { b--; e++; res++; } else break; } return res; } int main() { int my_id = MyNodeId(); int N = GetLength(); int nodes = NumberOfNodes(); //nodes = 1; int range = (N + nodes - 1) / nodes; int begin = std::min(N, my_id * range); int end = std::min(N, (my_id+1) * range); long long res1 = 0; long long res2 = 0; for(int i = begin; i < end; i++) { res1 += brute1(i, N); res2 += brute2(i, N); } if(my_id != MASTER_NODE && my_id < nodes) { PutLL(MASTER_NODE, res1); PutLL(MASTER_NODE, res2); Send(MASTER_NODE); } else { for(int node = 1; node < nodes; node++) { Receive(node); res1 += GetLL(node); res2 += GetLL(node); } } if(my_id == 0) { //printf("%lld %lld %lld\n", res1+res2, res1, res2); printf("%lld\n", res1+res2); } return 0; } |