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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "palindromy.h"
#include "message.h"

#include <iostream>
#include <algorithm>

typedef long long i64;

const int N = 50000000 + 10, S = 100 + 10, B1 = 31, B2 = 37, MOD = 1000000007;

int power(int base, int exp) {
  int res = 1;
  for (; exp; exp >>= 1) {
    if (exp & 1) res = (i64)res * base % MOD;
    base = (i64)base * base % MOD;
  }
  return res;
}

struct Info {
  int x, y;
  Info() { x = y = 0; }
  Info(int _x, int _y): x(_x), y(_y) {}
  inline i64 encode() const { return (i64)x << 32 | y; }
  inline bool operator== (const Info &rhs) const {
    return x == rhs.x && y == rhs.y;
  }
  explicit Info(i64 z) {
    x = z >> 32;
    y = z & 0xffffffff;
  }
  inline Info& operator+= (char rhs) {
    rhs = rhs - 'a' + 1;
    x = ((i64)x * B1 + rhs) % MOD;
    y = ((i64)y * B2 + rhs) % MOD;
    return *this;
  }
  inline Info& operator+= (const Info &rhs) {
    x = (x + rhs.x) % MOD;
    y = (y + rhs.y) % MOD;
    return *this;
  }
  inline Info& operator*= (const Info &rhs) {
    x = (i64)x * rhs.x % MOD;
    y = (i64)y * rhs.y % MOD;
    return *this;
  }
  inline Info& operator<<= (int rhs) {
    x = (i64)x * power(B1, rhs) % MOD;
    y = (i64)y * power(B2, rhs) % MOD;
    return *this;
  }
};

const Info B(B1, B2);

int n, id, tot, size;

int x[S], y[S], l[S], r[S], cnt;

int p, q;

char _s[2 * N];
int _pool[2 * N];

i64 ans;

void solve1() {
  int len = r[p] - l[p];
  Info pre, suf;
  char *s = _s;
  for (int i = 0; i < len; ++i) s[i] = GetLetter(l[p] + i);
  for (int i = 0; i < len; ++i) pre += s[i];
  for (int i = len - 1; i >= 0; --i) suf += s[i];
  for (int i = 0; i < tot; ++i) {
    if (x[i] != y[i]) {
      PutLL(i, pre.encode());
      PutLL(i, suf.encode());
      Send(i);
    }
  }
  for (int i = len; i > 0; --i) s[2 * i - 1] = s[i - 1];
  for (int i = 0; i <= 2 * len; i += 2) s[i] = '#';
  s[2 * len + 1] = '$';
  int *mx = _pool;
  mx[0] = 0;
  int k = 0;
  for (int i = 1; i <= 2 * len; ++i) {
    mx[i] = 0;
    if (k + mx[k] > i) mx[i] = std::min(k + mx[k] - i, mx[2 * k - i]);
    while (s[i - mx[i]] == s[i + mx[i]]) ++mx[i];
    if (i + mx[i] > k + mx[k]) k = i;
  }
  for (int i = 1; i <= 2 * len; ++i) ans += mx[i] / 2;
}

Info pre[S], suf[S];

void exkmp(const char *s, const char *t, int ls, int lt, int *ex, int *next) {
  for (int i = 1, j = -1, a, p; i < lt; i++, j--) {
    if (j < 0 || i + next[i - a] >= p) {
      if (j < 0) j = 0, p = i;
      while (p < lt && t[j] == t[p]) j++, p++;
      next[i] = j, a = i;
    } else {
      next[i] = next[i - a];
    }
  }
  for (int i = 0, j = -1, a, p; i < ls; i++, j--) {
    if (j < 0 || i + next[i - a] >= p) {
      if (j < 0) j = 0, p = i;
      while (p < ls && j < lt && s[p] == t[j]) j++, p++;
      ex[i] = j, a = i;
    } else {
      ex[i] = next[i - a];
    }
  }
}

void solve2() {
  int u = r[p] - l[p], v = r[q] - l[q];
  char *s = _s, *t = _s + N;
  for (int i = 0; i < u; ++i) s[i] = GetLetter(l[p] + i);
  for (int i = 0; i < v; ++i) t[i] = GetLetter(l[q] + i);
  for (int i = 0; i < tot; ++i) {
    if (x[i] == y[i]) {
      Receive(i);
      pre[x[i]] = Info(GetLL(i));
      suf[x[i]] = Info(GetLL(i));
    }
  }
  int *ex = _pool, *next = _pool + N;
  std::reverse(s, s + u);
  exkmp(s, t, u, v, ex, next);
  Info mpre, msuf;
  for (int i = p + 1; i < q; ++i) (mpre <<= r[i] - l[i]) += pre[i];
  for (int i = q - 1; i > p; --i) (msuf <<= r[i] - l[i]) += suf[i];
  Info info(power(B1, l[q] - r[p]), power(B2, l[q] - r[p]));
  //  if (p == 0 && q == 2) fprintf(stderr, "%d %d %d %d\n", mpre.x, mpre.y, msuf.x, msuf.y);
  for (int i = 0; i < u; ++i) {
    msuf += s[i];
    mpre.x = (mpre.x + info.x * (s[i] - 'a' + 1LL)) % MOD;
    mpre.y = (mpre.y + info.y * (s[i] - 'a' + 1LL)) % MOD;
    info *= B;
    //    if (p == 0 && q == 2) fprintf(stderr, "%d %d %d %d\n", mpre.x, mpre.y, msuf.x, msuf.y);
    if (mpre == msuf) ans += (i + 1 < u ? ex[i + 1] : 0);
  }
  exkmp(t, s, v, u, ex, next);
  mpre = msuf = Info();
  for (int i = p + 1; i < q; ++i) (mpre <<= r[i] - l[i]) += pre[i];
  for (int i = q - 1; i > p; --i) (msuf <<= r[i] - l[i]) += suf[i];
  if (mpre == msuf) ans += ex[0];
  info = Info(power(B1, l[q] - r[p]), power(B2, l[q] - r[p]));
  for (int i = 0; i < v; ++i) {
    mpre += t[i];
    msuf.x = (msuf.x + info.x * (t[i] - 'a' + 1LL)) % MOD;
    msuf.y = (msuf.y + info.y * (t[i] - 'a' + 1LL)) % MOD;
    info *= B;
    if (mpre == msuf) ans += (i + 1 < v ? ex[i + 1] : 0);
  }
}

int main() {
  n = GetLength();
  id = MyNodeId();
  cnt = 1;
  while (cnt < n && (cnt + 1) * (cnt + 2) / 2 <= NumberOfNodes()) ++cnt;
  size = (n + cnt - 1) / cnt;
  for (int i = 0; i < cnt; ++i) l[i] = i * size, r[i] = std::min((i + 1) * size, n);
  tot = 0;
  for (int i = 0; i < cnt; ++i)
    for (int j = 0; j <= i; ++j)
      x[tot] = j, y[tot] = i, ++tot;
  if (id >= tot) return 0;
  p = x[id], q = y[id];
  if (p == q) solve1(); else solve2();
  if (id) {
    PutLL(0, ans);
    Send(0);
  } else {
    for (int i = 1; i < tot; ++i) {
      Receive(i);
      i64 temp = GetLL(i);
      //      fprintf(stderr, "%d %d %lld\n", x[i], y[i], temp);
      ans += temp;
    }
    std::cout << ans << '\n';
  }
  return 0;
}