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
#include<bits/stdc++.h>
#define FOR(i,s,e) for(int i=(s);i<=(e);i++)
#define FORD(i,s,e) for(int i=(s);i>=(e);i--)
#define FOREACH(i,c) for( __typeof((c).begin()) i=(c).begin();i!=(c).end();++i)
#define ALL(k) (k).begin(),(k).end()
#define e1 first 
#define e2 second
#define mp make_pair
#define pb push_back
#define eb emplace_back 
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
const bool print=false;
const int MAXI=111;
const int MAXN=127111000;

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

int r[MAXN];

int n0;
inline int gl(int i){
	if(i<=0) return '#';
	if(i>n0) return '$';
	return GetLetter(i-1);
}

inline int gl2(int i){return gl(n0-i+1);}

main(){
	int nnodes=NumberOfNodes();
	int mynode=MyNodeId();
	n0=GetLength();
	int n=n0/2;
	
	LL wyn=0;
	if(mynode==0){
		int i=1,tt=0;
		r[0]=0;
		while(i<=n){
			bool aut=false;
			r[i]=tt;
			while(gl(i-r[i])==gl(i+r[i]+1)) r[i]++;
			tt=r[i];
			int k=1;
			while(k<=r[i] && r[i-k]!=r[i]-k){
				if(i+k<=n) r[i+k]=min(r[i-k],r[i]-k);
				else {aut=true;break;}
				k++;
			}
			if(aut) break;
			tt-=k;if(tt<0)tt=0;
			i+=k;
		}
		/*puts("0");
		FOR(i,1,n0) printf("%c ",gl(i));puts("");
		FOR(i,1,n) printf("%d ",r[i]);puts("");
		*/
		Receive(1);
		wyn=GetLL(1);
		Receive(2);
		wyn+=GetLL(2);
		Receive(3);
		wyn+=GetLL(3);
		
		wyn+=n0;
		FOR(i,1,n) wyn+=(LL)r[i];
		printf("%lld\n",wyn);
	}
	else if(mynode==1){
		int i=1,tt=0;
		r[0]=0;
		while(i<=n){
			bool aut=false;
			r[i]=tt;
			while(gl(i-r[i]-1)==gl(i+r[i]+1)) r[i]++;
			tt=r[i];
			int k=1;
			while(k<=r[i] && r[i-k]!=r[i]-k){
				if(i+k<=n) r[i+k]=min(r[i-k],r[i]-k);
				else {aut=true;break;}
				k++;
			}
			if(aut) break;
			tt-=k;if(tt<0)tt=0;
			i+=k;
		}
		/*puts("1");
		FOR(i,1,n0) printf("%c ",gl(i));puts("");
		FOR(i,1,n) printf("%d ",r[i]);puts("");
		*/
		FOR(i,1,n) wyn+=r[i];
		PutLL(0,wyn);
		Send(0);
	}
	else if(mynode==2){
		n=n0-n-1;
		int i=1,tt=0;
		r[0]=0;
		while(i<=n){
			bool aut=false;
			r[i]=tt;
			while(gl2(i-r[i])==gl2(i+r[i]+1)) r[i]++;
			tt=r[i];
			int k=1;
			while(k<=r[i] && r[i-k]!=r[i]-k){
				if(i+k<=n) r[i+k]=min(r[i-k],r[i]-k);
				else {aut=true;break;}
				k++;
			}
			if(aut) break;
			tt-=k;if(tt<0)tt=0;
			i+=k;
		}
		/*puts("2");
		FOR(i,1,n0) printf("%c ",gl2(i));puts("");
		FOR(i,1,n) printf("%d ",r[i]);puts("");
		*/
		FOR(i,1,n) wyn+=r[i];
		PutLL(0,wyn);
		Send(0);
	}
	else if(mynode==3){
		n=n0-n;
		int i=1,tt=0;
		r[0]=0;
		while(i<=n){
			bool aut=false;
			r[i]=tt;
			while(gl2(i-r[i]-1)==gl2(i+r[i]+1)) r[i]++;
			tt=r[i];
			int k=1;
			while(k<=r[i] && r[i-k]!=r[i]-k){
				if(i+k<=n) r[i+k]=min(r[i-k],r[i]-k);
				else {aut=true;break;}
				k++;
			}
			if(aut) break;
			tt-=k;if(tt<0)tt=0;
			i+=k;
		}
		/*puts("3");
		FOR(i,1,n0) printf("%c ",gl2(i));puts("");
		FOR(i,1,n) printf("%d ",r[i]);puts("");
		*/
		FOR(i,1,n) wyn+=r[i];
		PutLL(0,wyn);
		Send(0);
	}
		
	
	
}