回文数的判断有个神奇的公式:
g[i]==g[leng+-i]
其中leng为字符串长度,看每个g[i]是否都满足它,若满足,就是回文数
ps:洛谷的impossible有毒,必须得复制题干中的impossible
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char c[];
int t,k,lena,leng=,a[],b[],g[],n;
void pd();
void jf()
{
int x=;leng=;g[]=;
while(leng<=lena+)
{g[leng]=a[leng]+a[lena+-leng]+x;
x=g[leng]/n;
g[leng]%=n;
leng++;
}g[leng+]=x;t++;//cout<<t;
while(g[leng]==&&leng>)
{leng--;
}
pd();
}
void pd()
{
int o=;
for(int i=;i<=leng/;i++)
{if(g[i]==g[leng+-i])
{o++;if(o==leng/)
{cout<<"STEP="<<t;k++;}
}
else
{for(int i=;i<=leng;i++)
a[i]=g[i];
lena=leng;
}
}
}
int main()
{scanf("%d",&n);
scanf("%s",c);
lena=strlen(c);
for(int i=;i<=lena-;i++)
{if(c[i]>)a[lena-i]=+c[i]--;
else a[lena-i]=c[i]-;
}
while(t<=&&k==)
{jf();
}
if(k==)cout<<"Impossible!";
}