#include <bits/stdc++.h> #include <stdint.h> #include "palindromy.h" #include "message.h" using namespace std; void slave() { int n = NumberOfNodes(); int ii = MyNodeId(); int s = n - 1; int l = GetLength(); s = min(s, l); if(ii >= s) return; // int i0 = l * ii / s; // int i1 = -1 + l * (ii + 1) / s; // cerr << "ii = " << ii << ", i0 = " << i0 << ", i1 = " << i1 << '\n'; int64_t ans = 0; for(int i = ii; i < l; i += s) { ans += 1; for(int j = 1; i-j >= 0 and i+j < l; j++) { if(GetLetter(i-j) == GetLetter(i+j)) { ans += 1; } else { break; } } for(int j = 1; i-j >= 0 and i+j-1 < l; j++) { if(GetLetter(i-j) == GetLetter(i+j-1)) { ans += 1; } else { break; } } } PutLL(n-1, ans); Send(n-1); } void master() { int n = NumberOfNodes(); int s = n - 1; int l = GetLength(); s = min(s, l); int64_t ans = 0; for(int i = 0; i < s; i++) { Receive(i); ans += GetLL(i); } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n = NumberOfNodes(); if(MyNodeId() != n-1) { slave(); } else { master(); } 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 | #include <bits/stdc++.h> #include <stdint.h> #include "palindromy.h" #include "message.h" using namespace std; void slave() { int n = NumberOfNodes(); int ii = MyNodeId(); int s = n - 1; int l = GetLength(); s = min(s, l); if(ii >= s) return; // int i0 = l * ii / s; // int i1 = -1 + l * (ii + 1) / s; // cerr << "ii = " << ii << ", i0 = " << i0 << ", i1 = " << i1 << '\n'; int64_t ans = 0; for(int i = ii; i < l; i += s) { ans += 1; for(int j = 1; i-j >= 0 and i+j < l; j++) { if(GetLetter(i-j) == GetLetter(i+j)) { ans += 1; } else { break; } } for(int j = 1; i-j >= 0 and i+j-1 < l; j++) { if(GetLetter(i-j) == GetLetter(i+j-1)) { ans += 1; } else { break; } } } PutLL(n-1, ans); Send(n-1); } void master() { int n = NumberOfNodes(); int s = n - 1; int l = GetLength(); s = min(s, l); int64_t ans = 0; for(int i = 0; i < s; i++) { Receive(i); ans += GetLL(i); } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n = NumberOfNodes(); if(MyNodeId() != n-1) { slave(); } else { master(); } return 0; } |