Lucy and the Flowers

Read problems statements in Mandarin Chinese  and Russian.
The staff of one of the most famous ChefLand's restaurants have just received the news! A delegation from the neighbouring country is going to visit the restaurant. The staff want to make this visit exciting and pleasant. Lucy is also a member of the staff and among all the things that the staff want to do, her assignment is to arrange the flowers on the tables.
Generally, there are 26 kinds of flowers, a small latin letter corresponds to a single kind. Equal letters correspond to equal kinds. Different letters correspond to different kinds. According to the old ChefLand tradition, one can make a bouquet from some flowers only if it is a substring of some magic string T. ChefLands make bouquets in such a manner that the order of flowers in them is important. The restaurant has a tradition to decorate a single table with N, not necessary different, bouquets. Their order matters.
Some decorations look really similar. Generally, two arrangements (let's call them A and B) are similar if B is a cyclic shift of A, or B is a reversed cyclic shift of A. For example the sequence ("aba", "c", "b") is a cyclic shift of a sequence ("b", "aba", "c"), and the sequence ("c", "b", "a", "e", "d") is a reversed cyclic shift of a sequence ("a", "b", "c", "d", "e").
Now Lucy thinks that the most beautiful bouquets are "symmetric" ones. The bouquet is "symmetric" if the string that corresponds to the arrangement of flowers in it is a palindrome (it reads from left to right the same as from right to left). 
So, she thinks that the decoration of the table will be especially beautiful if all the bouquets in it are "symmetric". But then, Lucy had realized that the number of such decorations of the tables can be less than the number of guests. She doesn't want to decorate two tables in a similar manner, so she would like to know the number of distinct  decorations such that no two tables looks similar.
Input
The first line of input consists of a single string T.
The second line of input consists of a single integer N.
Output
Output the number of distinct decorations.
Constraints
1 ≤ |T| ≤ 100000;
1 ≤ N ≤ 600;
String T consists only of lowercase English letters.
Example

Input:
aba
4

Output:
21

Example input:

Example output:


Solution:

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<queue>
    #include<algorithm>
    #include<string>
    #include<map>
    #include<set>
    #include<sstream>
    #include<iomanip>
    #include<cmath>
    using namespace std;
    #define ll long long
    #define ull unsigned long long
    //#define inf 1000000007
    #define mod 1000000007
    const ll inf = 100000000000000007LL;
    #define pii pair<int,int>
    #define vi vector<int>
    #define all(x) x.begin(),x.end()
    #define mp make_pair
    #define pb push_back
    #define x first
    #define y second
    #define N 100010
    
    struct bignum
    {
    	int x[4000],n;
    	bignum(){n=0;memset(x,0,sizeof(x));}
    	bignum(int a)
    	{
    		n=0;
    		memset(x,0,sizeof(x));
    		while(a>0)x[n++]=a%10,a/=10;
    	}
    	bignum operator +(const bignum&a)const
    	{
    		bignum c=*this;int i,r;
    		for(i=0,r=0;i<max(a.n,c.n)||r>0;i++)
    		{
    			c.x[i]+=(i<a.n?a.x[i]:0)+r;
    			r=c.x[i]>=10;
    			if(r)
    			{
    				r=c.x[i]/10;c.x[i]%=10;
    			}	
    		}
    		c.n=i;
    		return c;
    	}
    	bignum operator *(const bignum&a)const
    	{
    		bignum c;
    		memset(c.x,0,sizeof(c.x));
    		for(int i=0;i<a.n;i++)
    		for(int j=0;j<n;j++)
    		{
    			c.x[i+j]+=a.x[i]*x[j];
    		}
    		c.n=a.n+n-1;
    		c.trim();
    		return c;
    	}
    	void operator =(const bignum&a)
    	{
    		n=a.n;
    		for(int i=0;i<n;i++)x[i]=a.x[i];
    	}
    	void operator=(int a)
    	{
    		n=0;
    		memset(x,0,sizeof(x));
    		while(a)x[n++]=a%10,a/=10;
    	}
    	bignum operator /(int v)const
    	{
    		bignum c=*this;
    		for(int i=n-1,r=0;i>=0;i--)
    		{
    			int z=c.x[i]+r*10;
    			r=z%v;c.x[i]=z/v;
    		}
    		c.trim();
    		return c;
    	}
    	void trim()
    	{
    		int i,c;
    		for(i=0,c=0;i<n||c;i++)
    		{
    			x[i]+=c;
    			c=x[i]>=10;
    			if(c)c=x[i]/10,x[i]%=10;
    		}
    		n=i;
    		while(n&&x[n-1]==0)n--;
    	}
    	void print()
    	{
    		for(int i=n-1;i>=0;i--)printf("%d",x[i]);
    		puts("");
    	}
    };
    
    
    char s[N];
    const int base1=59,base2=13799,mod1=1299709,mod2=1000000009;
    int b1[N],h1[N],cnt,head[mod1],next[N];
    ull b2[N],h2[N],g[N];
    void init()
    {
    	int i,j,n;
    	memset(head,-1,sizeof(head));
    	cnt=0;
    	n=strlen(s);
    	h1[0]=h2[0]=0;
    	for(i=0;i<n;i++)
    	{
    		h1[i+1]=((h1[i]*base1+s[i]-'a'+1)%mod1);
    		h2[i+1]=((h2[i]*base2+s[i]-'a'+1)%mod2);
    	}
    	b1[0]=b2[0]=1;
    	for(int i=1;i<N;i++)
    	{
    		b1[i]=(b1[i-1]*base1%mod1);
    		b2[i]=(b2[i-1]*base2%mod2);
    	}
    }
    bool insert(int t1,ull t2)
    {
    	for(int i=head[t1];i+1;i=next[i])
    	{
    		if(g[i]==t2)return 0;
    	}
    	g[cnt]=t2,next[cnt]=head[t1],head[t1]=cnt++;
    	return 1;
    }
    int d[N*2],a[N*2];
    int manlache()
    {
    	int i,j,k=0,id,len=1,n=strlen(s);
    	a[0]=-1;
    	for(i=0;i<n;i++)
    	{
    		a[len++]=s[i]-'a'+1;
    		a[len++]=0;
    	}
    	a[len-1]=-2;
    	for(i=1;i<len;i++)
    	{
    		j=1;
    		if(i<k)j=min(k-i,d[id+id-i]);
    		while(a[i-j]==a[i+j])j++;
    		d[i]=j;
    		if(i+j>k)k=i+j,id=i;
    	}
    	return len;
    }
    int solve(int len)
    {
    	int ans=0;
    	int i,s,e;
    	for(i=1;i<len-1;i++)
    	{
    		if(i&1)s=(i-1)/2-(d[i]-1)/2,e=(i-1)/2+(d[i]-1)/2;
    		else s=(i-1)/2-d[i]/2+1,e=(i+1)/2+d[i]/2-1;
    		s++,e++;
    		//printf("%d %d\n",s,e);
    		while(s<=e)
    		{
    			int t1=(h1[e]-1LL*h1[s-1]*b1[e-s+1]%mod1+mod1)%mod1;
    			ull t2=(h2[e]-h2[s-1]*b2[e-s+1]%mod2+mod2)%mod2;
    			if(insert(t1,t2)==0)break;
    			else ans++,s++,e--;
    		}
    	}
    	return ans;
    }
    bignum power[610];
    int polya(int c,int len)
    {
    	power[0]=1;
    	for(int i=1;i<=len;i++)
    	power[i]=power[i-1]*bignum(c);
    	bignum ans1=0,ans2=0;
    	for(int i=1;i<=len;i++)ans1=ans1+power[__gcd(i,len)];
    	if(len&1)ans2=power[(len+1)/2]*bignum(len);
    	else ans2=(power[len/2]+power[len/2+1])*bignum(len/2);
    	//ans1.print(),ans2.print();
    	bignum ans=ans1+ans2;
    	//ans.print();
    	ans1=(ans1+ans2)/(2*len);
    	ans1.print();
    }
    int main()
    {
    	int T,i,j,k,ca=0,m;
    	while(~scanf("%s",s))
    	{
    		init();
    		int len=manlache();
    		int ret=solve(len);
    		//printf("%d %d\n",len,ret);
    		scanf("%d",&m);
    		polya(ret,m);
    	}
    }