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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include<bits/stdc++.h>
//#define int LL
//#define PutInt PutLL
//#define GetInt GetLL
#define PII pair<int,int>
#define f first
#define s second
#define VI vector<int>
#define LL long long
#define MP make_pair
#define LD long double
#define PB push_back
#define ALL(V) V.begin(),V.end()
#define abs(x) max((x),-(x))
#define PDD pair<LD,LD> 
#define VPII vector< PII > 
#define siz(V) ((int)V.size())
#define FOR(x, b, e)  for(int x=b;x<=(e);x++)
#define FORD(x, b, e) for(int x=b;x>=(e);x--)
#define REP(x, n)     for(int x=0;x<(n);x++)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define VL vector<LL>
using namespace std;

#include "message.h"
#include "palindromy.h"
const int MXNODES = 104;
int NODES, N, ID;

/*

• GetLength() – zwraca n (1 ≤ n ≤ 5 · 108) – długość słowa s.
• GetLetter(i) – zwraca si (si ∈ {a, . . . , z}) – i-tą literę słowa s. Litery są ponumerowane od 0 do n − 1.
*/

void brute()
  {
  char *x = new char[N+2];
  int *R = new int[N+2];
  FOR(i, 0, N-1)
    x[i + 1] = GetLetter(i);
  
  x[0] = '!';
  x[N+1] = '#';
  
  LL res = 0;
  int i = 1, j = 0;
  while (i <= N) // parzyste
    {
      while (x[i - j] == x[i + j + 1])
        j++;
      
      R[i] = j;
//      cerr<<i<<" "<<j<<endl;
      int k = 1;
      while (R[i - k] != R[i] - k && k <= j)
      {      
        R[i + k] = min(R[i - k], R[i] - k);
        k++;
      }
      j=max(j - k, 0); 
      i += k;
    }
  FOR(i, 1, N)res += R[i];
  
  i = 1, j = 0;
  while (i <= N)
    {
      while (x[i - j - 1] == x[i + j + 1])
        j++;
      R[i] = j;
      int k = 1;
      while (R[i - k] != R[i] - k && k <= j)
      {      
        R[i + k] = min(R[i - k], R[i] - k);
        k++;
      }
      j=max(j - k, 0); 
      i += k;
    }    
  FOR(i, 1, N)res += R[i];
  res += N;
  printf("%lld\n", res);
  }

VPII podzial(int n, int ile) // zwraca podział 1..n na przedziały o podobnej długości
  {
  VPII res;
  int last = 0;
  REP(i, ile)
    {
    int a = last + 1;
    int b = a + n/ile + (n%ile > i) - 1; // to check, może na =
    
    mini(b, n);
    res.PB(MP(a, b));
    last = b;
    }
  assert(last == n);
  return res;
  }


bool notok;
int MAGIC = 60;
LL ssum;/*
int promien(int a, int b)
  {
  notok = 0;
  int cnt = 0;
//  cerr<<a<<" "<<b<<"   "<<N<<endl;
  while(a > 0 && b < N-1)
    {
    if(GetLetter(a-1) == GetLetter(b+1))
      {
      a--;
      b++;
      }
    else
      break;
    cnt++;
    if(cnt > MAGIC){notok = 1;break;}
    }
  ssum += cnt;
//  cerr<<a<<" "<<b<<" "<<(b-a+1)/2<<endl;
  return (b - a + 1)/2;
  }*/
int R(int a, int b)
  {
  return rand()%(b-a+1)+a;
  }
bool czek(int a, int b, int szer)
  {
  REP(i, 5)
    {
    int c = R(1, szer);
    if(GetLetter(a - c) != GetLetter(b + c))return 0;
    }
  return 1;
  }
bool czek2(int a, int b, int szer)
  {
  REP(i, 18)
    {
    int c = R(1, szer);
    if(GetLetter(a - c) != GetLetter(b + c))return 0;
    }
  return 1;
  }
int rzarzpromien(int a, int b)
  {
  if(a <= 0 || b >= N-1 || GetLetter(a-1) != GetLetter(b+1))
    {
    return (b - a + 1)/2;
    }
  
  notok = 0;
  int cnt = 0;
//  cerr<<a<<" "<<b<<"   "<<N<<endl;
   
  int ttt = min(a, (N-1) - b);
  if(czek2(a, b, ttt))
    {
    a-=ttt;
    b+=ttt;
    return (b - a + 1)/2;
    }

  FORD(i, 28, 0)
    {
    int prze = 1<<i;
    if(a - prze < 0 || b + prze >= N)continue;
    if(czek(a, b, prze))
      {
      a -= prze;
      b += prze;
      }
    }

  return (b - a + 1)/2;
  }


int obok = 4e7; // 2 * tyle + n/100
LL heure(int a, int b)
  {
  char *x = new char[b - a + obok * 2 + 10];
  int *R = new int[b - a + obok * 2 + 10];
  int n = 1;
  int poca = -1;
  int konb = -1;
  FOR(i, max(0, a-obok), min(b + obok, N-1))
    {
    
    if(i == a)poca = n;
    if(i == b)konb = n;
    x[n] = GetLetter(i);
    n++;
    }
  x[0] = '!';
  x[n+1] = '#';
  n++;
  
  LL res = 0;
  int i = 1, j = 0;
  while (i <= n) // parzyste
    {
      while (x[i - j] == x[i + j + 1])
        j++;
      
      R[i] = j;
      int k = 1;
      while (R[i - k] != R[i] - k && k <= j)
      {      
        R[i + k] = min(R[i - k], R[i] - k);
        k++;
      }
      j=max(j - k, 0); 
      i += k;
    }
  int cpa = a;
  FOR(i, poca, konb)
    {
    int poc = cpa - R[i] + 1;
    int kon = cpa + R[i];
    
    res += rzarzpromien(poc, kon);
    cpa++;
    }
  i = 1, j = 0;
  while (i <= n)
    {
      while (x[i - j - 1] == x[i + j + 1])
        j++;
      R[i] = j;
      int k = 1;
      while (R[i - k] != R[i] - k && k <= j)
      {      
        R[i + k] = min(R[i - k], R[i] - k);
        k++;
      }
      j=max(j - k, 0); 
      i += k;
    }    
  cpa = a;
  FOR(i, poca, konb)
    {
    int poc = cpa - R[i];
    int kon = cpa + R[i];
    
    res += rzarzpromien(poc, kon);
    cpa++;
    }
  res += (b - a + 1);
  return res;
  }


int main()          /////////////////////////
{
NODES = NumberOfNodes();
ID = MyNodeId();
N = GetLength();
if(N <= 1e8 + 1)
  {
  if(ID == 0)brute();
  return 0;
  }
VPII V = podzial(N, NODES);
REP(i, V.size())
  {
  V[i].f--;
  V[i].s--;
  }
LL res = heure(V[ID].f, V[ID].s);
PutLL(0, res);
Send(0);

if(ID == 0)
  {
  LL result = 0;
  REP(i, NODES)
    {
    Receive(i);
    LL x = GetLL(i);
    result += x;
    }
  printf("%lld\n", result);
  }
}