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
#include <bits/stdc++.h>
#include "palindromy.h"
#include "message.h"
using namespace std;
using ull=unsigned long long;
const ull M=3e9+19, P=1117;
int node_begin[105], node_end[105];
int right_node[20][20];
ull fast_pow(ull a, ull e)
{
	if(e==0) return 1;
	if(e%2==0) return fast_pow(a*a%M, e/2);
	return fast_pow(a*a%M, e/2)*a%M;
}
const int N=5e8/14+10;
char* in_manacher; // #s#l#o#w#o#
int* out_manacher; // a -> 0, aba -> 1, itp.
void manacher()
{
    int l=strlen(in_manacher+1);
    in_manacher[0]='$';
    in_manacher[l+1]='@';
    int t=1;
    for(int i=2; i<=l; ++i)
    {
        if(t+out_manacher[t]<i)
        {
            t=i;
        }
        out_manacher[i]=min(t+out_manacher[t]-i, out_manacher[t-(i-t)]);
        while(in_manacher[i+out_manacher[i]]==in_manacher[i-out_manacher[i]]) ++out_manacher[i];
        --out_manacher[i];
        if(i+out_manacher[i]>t+out_manacher[t]) t=i;
    }
}
//przygotowanie slowa
void prepare_manacher(int l_end, int r_end)
{
    int l=r_end-l_end;
    for(int i=0; i<l; ++i)
    {
        in_manacher[2*i+2]=GetLetter(l_end+i);
        in_manacher[2*i+1]='#';
    }
    in_manacher[2*l+1]='#';
}
void clear_all_manacher()
{
    int l=strlen(in_manacher);
    memset(in_manacher, 0, (l+2));
    memset(out_manacher, 0, (l+2)*sizeof(int));
}
ull count_occurences(int l_end, int r_end)
{
	prepare_manacher(l_end, r_end);
	manacher();
	ull res=0;
	int n=GetLength();
	if(r_end==n) in_manacher[2*(r_end-l_end)+2]='^';
	else in_manacher[2*(r_end-l_end)+2]=GetLetter(r_end);
	if(l_end==0) in_manacher[0]='%';
	else in_manacher[0]=GetLetter(l_end-1);
	for(int i=2; i<=2*(r_end-l_end); ++i)
	{
		int left_end=i-out_manacher[i], right_end=out_manacher[i]+i+1;
		if(in_manacher[left_end-1]!=in_manacher[right_end]) res+=(out_manacher[i]+1+((i+1)%2))/2;
	}
	clear_all_manacher();
	return res;
}

int main()
{
	int nodes=NumberOfNodes(), id=MyNodeId();
	int num=1;
	ull n=GetLength();
	nodes=min(n, (ull)nodes);
	while(num*num<=2*nodes) ++num;
	--num;
	nodes=min(nodes, (num*num+1)/2);
	if(id>=nodes) return 0; //jestes bezuzyteczna
	/*
	if(id==0) printf("%d\n", nodes);
	if(id==0) printf("%lld %lld\n", M, P);
	*/
	int k=0;
	for(int i=0; i<num; ++i) for(int j=i+1; j<num; ++j)
	{
		node_begin[k]=i;
		node_end[k]=j;
		right_node[i][j]=k;
		++k;
	}
	if(id<num)
	{
		ull forward_hash=0, backward_hash=0, p_pow=1; //forward=1+p+p2+... backward=...+p2+p+1
		for(int i=id*n/num; i<(id+1)*n/num; ++i)
		{
			forward_hash=(forward_hash+p_pow*GetLetter(i))%M;
			backward_hash=(backward_hash*P+GetLetter(i))%M;
			p_pow=(p_pow*P)%M;
		}
		PutLL(0, forward_hash);
		PutLL(0, backward_hash);
		Send(0);	
	}
	if(id==0)
	{
		ull forward_hash[20], backward_hash[20];
		for(int i=0; i<num; ++i)
		{
			Receive(i);
			forward_hash[i]=GetLL(i);
			backward_hash[i]=GetLL(i);
		}
		//printf("%lld\n", forward_hash[backward_hash[0]%num]);
		for(int i=0; i<num; ++i) for(int j=i+1; j<num; ++j)
		{
			ull f_h=0, b_h=0;
			for(int k=i+1; k<j; ++k)
			{
				f_h=(f_h+fast_pow(P, k*n/num-(i+1)*n/num)*forward_hash[k])%M;
				b_h=(fast_pow(P, (k+1)*n/num-k*n/num)*b_h+backward_hash[k])%M;
			}
			PutLL(right_node[i][j], f_h);
			PutLL(right_node[i][j], b_h);
			Send(right_node[i][j]);
		}
	}
	if(id<num*(num-1)/2)
	{
		int l=node_begin[id]*n/num, midl=(node_begin[id]+1)*n/num;
		int midr=(node_end[id]*n)/num, r=(node_end[id]+1)*n/num;
		Receive(0);
		ull f_h=GetLL(0);
		ull b_h=GetLL(0);
		int begin_mid=l+midr, end_mid=midl+r+1;
		vector<int> p_r(end_mid-begin_mid);
		ull frw_hash=f_h, bcw_hash=b_h, p_pow=fast_pow(P, midr-midl);
		char c_tmp=GetLetter(midr-1);
		for(int i=midl-1; i>=l; --i)
		{
			char c=GetLetter(i);
			frw_hash=(c+frw_hash*P)%M;
			bcw_hash=(bcw_hash+c*p_pow)%M;
			p_pow=(p_pow*P)%M;
			if(frw_hash==bcw_hash&&c==c_tmp) p_r[i+midr-begin_mid]=midr-i;
			else p_r[i+midr-begin_mid]=-1;
		}
		frw_hash=f_h; bcw_hash=b_h; p_pow=fast_pow(P, midr-midl);
		c_tmp=GetLetter(midl);
		for(int i=midr; i<r; ++i)
		{
			char c=GetLetter(i);
			frw_hash=(frw_hash+c*p_pow)%M;
			bcw_hash=(bcw_hash*P+c)%M;
			p_pow=(p_pow*P)%M;
			if(frw_hash==bcw_hash&&c==c_tmp) p_r[i+1+midl-begin_mid]=i+1-midl;
			else p_r[i+1+midl-begin_mid]=-1;
		}
		if(f_h==b_h&&(midl==midr||GetLetter(midl)==GetLetter(midr-1))) p_r[midl+midr-begin_mid]=midr-midl;
		else p_r[midl+midr-begin_mid]=-1;
		ull cnt=0;
		int mx=-1, mxpos=-1;
		/*
		printf("Instance %d report:\n", id);
		printf("(%d) [%d %d), [%d %d)\nbegin_mid=%d, end_mid=%d\nf#=%ull, b#=%ull\n", id, l, midl, midr, r, begin_mid, end_mid, f_h, b_h);
		for(int i: p_r) printf("%d ", i);
		printf("\n");
		fflush(stdout);
		*/
		for(int i=0; i<p_r.size(); ++i) if(p_r[i]!=-1)
		{
			int l_end=(i+begin_mid-p_r[i])/2, r_end=(i+begin_mid+p_r[i])/2;
			if(r_end>mx)
			{
				mx=r_end;
				mxpos=i;
			}
			if(2*mxpos-i>=0&&2*mx>i+begin_mid)
			{
				int tmp=min(p_r[2*mxpos-i], 2*mx-i-begin_mid);
				if(tmp!=-1) p_r[i]=tmp;
			}
			//if(i==2) printf("p_r=%d\n", p_r[i]);
			if(p_r[i]==-1) continue;
			l_end=(i+begin_mid-p_r[i])/2; r_end=(i+begin_mid+p_r[i])/2;
			while(l_end-1>=l&&r_end+1<=r&&GetLetter(l_end-1)==GetLetter(r_end))
			{
				--l_end;
				++r_end;
				p_r[i]+=2;
			}
			if(mx<=r_end)
			{
				mx=r_end;
				mxpos=i;
			}
			if((l_end==0||r_end==n||GetLetter(l_end-1)!=GetLetter(r_end))&&l_end<midl&&r_end>midr) cnt+=(p_r[i]+1)/2;
		}
		/*
		for(int i: p_r) printf("%d ", i);
		printf("\n");
		fflush(stdout);
		printf("(%d) cnt=%llu\n", id, cnt);
		*/
		PutLL(0, cnt);
		Send(0);
	}
	else if(id>=num*(num-1)/2)
	{
		in_manacher=new char[2*N];
		out_manacher=new int[2*N];
		int new_id=id-num*(num-1)/2;
		ull cnt=0;
		cnt+=count_occurences((2*new_id)*n/num, (2*new_id+1)*n/num);
		if(2*new_id+1<num) cnt+=count_occurences((2*new_id+1)*n/num, (2*new_id+2)*n/num);
		/*
		printf("Instance %d report:\n[%d, %d)\n", id, (2*new_id)*n/num, (2*new_id+1<num) ? (2*new_id+2)*n/num : (2*new_id+1)*n/num);
		printf("(%d) cnt=%llu\n", id, cnt);
		*/
		PutLL(0, cnt);
		Send(0);
		delete[] in_manacher;
		delete[] out_manacher;
	}
	if(id==0)
	{
		ull res=0;
		for(int i=0; i<nodes; ++i)
		{
			Receive(i);
			res+=GetLL(i);
		}
		printf("%llu\n", res);
	}
}