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
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <sstream>
#include <cmath>
using namespace std;

typedef vector<int> vi;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<int,int> pii;
#define mp make_pair

#define REP(i,n) for (int i=0,___=(n); i<___; ++i)
#define FOR(i,a,b) for (int i=(a),___=(b); i<=___; ++i)
#define FORD(i,a,b) for (int i=(a),___=(b); i>=___; --i)
#define FOR2(i,a,b) for (int ___A=(a),___B=(b),___BR=1; ___BR; ___BR=0) for (i=___A; i<=___B; ++i)

int read() { int n; scanf("%d", &n); return n; }
ll readl() { ll n; scanf("%lld", &n); return n; }
char readc() { static char s[32]; scanf("%s", s); return *s; }


#include "palindromy.h"
#include "message.h"

// kod skopiowany (i lekko poprawiony) stąd: http://www.mimuw.edu.pl/~jrad/teksty/manacher.cpp

//Palindromy parzyste
long long Manacher1(const string &x)
{
  long long res = 0;
  vector<int> R;
  R.push_back(0);
  int i = 1, j = 0;
  while (i <= x.size() - 1)
  {
    while (x[i - j] == x[i + j + 1])
      j++;
    R.push_back(j);
    res += R.back();
    int k = 1;
    while (R[i - k] != R[i] - k && k <= j)
    {
      R.push_back(min(R[i - k], R[i] - k));
      res += R.back();
      k++;
    }
    j=max(j - k, 0); i += k;
  }
  return res;
}

//Palindromy nieparzyste
long long Manacher2(const string &x)
{
  long long res = 0;
  vector<int> R;
  R.push_back(0);
  int i = 1, j = 0;
  while (i <= x.size() - 1)
  {
    while (x[i - j - 1] == x[i + j + 1])
      j++;
    R.push_back(j);
    res += R.back();
    int k = 1;
    while (R[i - k] != R[i] - k && k <= j)
    {
      R.push_back(min(R[i - k], R[i] - k)); k++;
      res += R.back();
    }
    j=max(j - k, 0); i += k;
  }
  return res;
}

int main() {
	if (MyNodeId() > 0) return 0;

	int n = GetLength();
	string s;
	s.resize(n + 2);
	s[0] = '$';
	s[n+1] = '#';
	for (int i=0; i<n; ++i) s[i+1] = GetLetter(i);

	printf("%lld\n", Manacher1(s) + Manacher2(s) + n);

	return 0;
}