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
#include "palindromy.h"
#include "message.h"

#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;

void show_array(long long ar[], long long n){
  for (int i = 0; i< n; i++){
    cout << ar[i] << ",";
  }
}

int main() {

  //  calculate begin and end for the palindrom centre

  long long id = MyNodeId();
  long long N = GetLength();
  long long nn = NumberOfNodes();
  if (nn > N) {
    nn = N;
  }

  long long batch_size = ceil(1.0 * N / nn);
  long long begin = id * batch_size;
  long long end = min(begin + batch_size, N);
  long long actual_nodes = ceil(1.0 * N / batch_size);

  if (nn >= actual_nodes) nn = actual_nodes;

  long long no_pals = batch_size; //  every single letter is a palindrome as well

  //  find palindromes in the batch with length > 1

  //  for centre in [begin..end]
  for (long long centre = begin; centre < end; centre++){
    //  find the biggest odd palindrome with the centre
    //  no_pals += radius
    for(long long radius = 1; centre + radius < N && centre - radius >= 0; radius++) {
      if (GetLetter(centre-radius) == GetLetter(centre+radius)) {
        no_pals++;
      } else {
        break;
      }
    }

    //  find the biggest even palindrome with the centre
    //  no_pals += radius
    for(long long radius = 0; centre + radius + 1 < N && centre - radius >= 0; radius++) {
      if (GetLetter(centre-radius) == GetLetter(centre+radius+1)) {
        no_pals++;
      } else {
        break;
      }
    }
  }

  PutLL(0, no_pals);
  Send(0);

  //  0 node sums it all up and prints it out
  if (id == 0){
    long long sum = 0;
    for (int k = 0; k < nn; k++){
      Receive(k);
      long long k_sum = GetLL(k);
      sum += k_sum;
    }
    cout << sum << endl;
  }

  return 0;
}