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
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll base1=23333;
const ll base2=13331;
const ll mod1=1000000007;
const ll mod2=1000000009;
// head
#include "palindromy.h"
#include "message.h"

int C=13,M=91;
int T,myid,n,block;
// 0..12
// 13..90
struct hashv {
	int pw1,pw2,hv1,hv2,rhv1,rhv2;
	void init() { pw1=pw2=1; hv1=hv2=rhv1=rhv2=0; }
	bool ispal() { return hv1==rhv1&&hv2==rhv2; }
	void addh(char c) {
		hv1=(hv1+c*(ll)pw1)%mod1;
		hv2=(hv2+c*(ll)pw2)%mod2;
		rhv1=(rhv1*base1+c)%mod1;
		rhv2=(rhv2*base2+c)%mod2;
		pw1=pw1*base1%mod1;
		pw2=pw2*base2%mod2;		
	}
	void addt(char c) {
		hv1=(hv1*base1+c)%mod1;
		hv2=(hv2*base2+c)%mod2;
		rhv1=(rhv1+c*(ll)pw1)%mod1;
		rhv2=(rhv2+c*(ll)pw2)%mod2;
		pw1=pw1*base1%mod1;
		pw2=pw2*base2%mod2;
	}
	void print() {
		printf("%d %d %d %d %d %d\n",hv1,hv2,rhv1,rhv2,pw1,pw2);
	}
}rg[15],ts;
hashv merge(hashv a,hashv b) {
	hashv c;
	c.pw1=(ll)a.pw1*b.pw1%mod1;
	c.pw2=(ll)a.pw2*b.pw2%mod2;
	c.hv1=((ll)a.hv1*b.pw1+b.hv1)%mod1;
	c.hv2=((ll)a.hv2*b.pw2+b.hv2)%mod2;
	c.rhv1=(a.rhv1+(ll)a.pw1*b.rhv1)%mod1;
	c.rhv2=(a.rhv2+(ll)a.pw2*b.rhv2)%mod2;
	return c;
}
int l[15],r[15];
PII seg[110];
const int N=40100000;
int pool[N*2];
char st[N*2];

void Z(int *nt,int n,int s,int w) {
	nt[0]=n; nt[n]=0;
	for (int i=1,j=0,k=0;i<n;i+=k,j=max(0,j-k)) {
		for (;i+j<n&&st[s+w*j]==st[s+w*(i+j)];++j);
		for (nt[i]=j,k=1;k<=j&&nt[k]!=j-k;k++) nt[i+k]=min(nt[k],j-k);
	}
}
void Z_match(int *nt,int n,int s1,int w1,int *ex,int m,int s2,int w2) {
	ex[m]=0;
	for (int i=0,j=0,k=0;i<m;i+=k,j=max(0,j-k)) {
		for (;j<n && i+j<m&&st[s1+w1*j]==st[s2+w2*(i+j)];++j);
		for (ex[i]=j,k=1;k<=j&&k+nt[k]!=j;k++) ex[i+k]=min(nt[k],j-k);
	}
}

ll ret=0;
void manacher(int n,int *r) {
	for (int i=0,j=0,k;i<=n;i+=k,j=max(0,j-k)) {
		while(i>=j&&i+j<=n&&st[i-j]==st[i+j]) ++j;
		r[i]=j;
		for (k=1;k<r[i]&&r[i]-k!=r[i-k];k++) r[i+k]=min(r[i-k],r[i]-k);
	}
	for (int i=1;i<n;i++) ret+=r[i]/2;
}
int main() {
	T=NumberOfNodes();
	myid=MyNodeId();
	n=GetLength();
	block=(n+C-1)/C;
	int tot=0;
	rep(i,0,C) rep(j,i+1,C) seg[tot++]=mp(i,j);
	rep(i,0,C) {
		l[i]=min(i*block,n); r[i]=min((i+1)*block,n);
	}
	if (myid<C) {
		hashv hv; hv.init();
		rep(j,l[myid],r[myid]) {
			char p=GetLetter(j);
			st[2*(j-l[myid])+1]=p;
			hv.addt(p);
		}
		rep(k,C,M) {
			PutInt(k,hv.pw1);
			PutInt(k,hv.pw2);
			PutInt(k,hv.hv1);
			PutInt(k,hv.hv2);
			PutInt(k,hv.rhv1);
			PutInt(k,hv.rhv2);
			Send(k);
		}
		if (l[myid]==r[myid]) {
			PutLL(M,0); Send(M);
			return 0;
		}
		manacher(2*(r[myid]-l[myid]),pool);
		PutLL(M,ret); Send(M);
	} else if (myid<M) {
		int sl=seg[myid-C].fi,sr=seg[myid-C].se;
		if (l[sl]==r[sl]||l[sr]==r[sr]) {
			rep(i,0,C) {
				Receive(i);
				rg[i].pw1=GetInt(i);
				rg[i].pw2=GetInt(i);
				rg[i].hv1=GetInt(i);
				rg[i].hv2=GetInt(i);
				rg[i].rhv1=GetInt(i);
				rg[i].rhv2=GetInt(i);
			}
			PutLL(M,0); Send(M);
			return 0;
		}
		rep(i,l[sl],r[sl]) st[N-(r[sl]-i)]=GetLetter(i);
		rep(i,l[sr],r[sr]) st[N+(i-l[sr])]=GetLetter(i);
		int *nt=pool,*ex=pool+N;
		Z(nt,r[sr]-l[sr],N,1);
		Z_match(nt,r[sr]-l[sr],N,1,ex,r[sl]-l[sl],N-1,-1);
		rep(i,0,C) {
			Receive(i);
			rg[i].pw1=GetInt(i);
			rg[i].pw2=GetInt(i);
			rg[i].hv1=GetInt(i);
			rg[i].hv2=GetInt(i);
			rg[i].rhv1=GetInt(i);
			rg[i].rhv2=GetInt(i);
		}
		ts.init();
		rep(i,sl+1,sr) ts=merge(ts,rg[i]);
		if (ts.ispal()) ret+=ex[0];
		hashv ss=ts;
		char *pc=st+(N-1);
		per(i,l[sl],r[sl]) {
			ss.addh(*pc); --pc;
			if (ss.ispal()) ret+=ex[r[sl]-i];
		}
		Z(nt,r[sl]-l[sl],N-1,-1);
		Z_match(nt,r[sl]-l[sl],N-1,-1,ex,r[sr]-l[sr],N,1);
		ss=ts;
		pc=st+N;
		rep(i,l[sr],r[sr]) {
			ss.addt(*pc); ++pc;
			if (ss.ispal()) ret+=ex[i-l[sr]+1];
		}
		PutLL(M,ret); Send(M);
	} else if (myid==M) {
		rep(i,0,M) {
			Receive(i);
			ll c=GetLL(i);
			ret+=c;
		}
		printf("%lld\n",ret);
	}/* else if (myid==M+1) {
		rep(i,1,n+1) st[i+i-1]=GetLetter(i-1);
		manacher(2*n,pool);
		printf("%lld\n",ret);
	}*/
}