题目地址:http://codevs.cn/problem/1045/
代码:
var
i,n,js,c,k:longint;
a,b:array[1..10000]of longint;
s:string;
begin
readln(n);
readln(s);
fillchar(a,sizeof(a),0);
for i:=1 to length(s) do
begin
if s[i]>'9'
then
begin
case s[i] of
'a','A':a[i]:=10;
'b','B':a[i]:=11;
'c','C':a[i]:=12;
'd','D':a[i]:=13;
'e','E':a[i]:=14;
'f','F':a[i]:=15;
end;
end
else a[i]:=ord(s[i])-48;
end;
c:=length(s);
for i:=1 to c do
if a[i]<>a[c-i+1]
then break;
if i=c
then begin writeln('STEP=0');exit;end;
js:=0;
while js<30 do
begin
inc(js);
fillchar(b,sizeof(b),0);
for i:=1 to c do
begin
b[i]:=a[i]+a[c-i+1];
end;
k:=0;
while k<=c do
begin
k:=k+1;
if b[k]>n-1
then
begin
b[k+1]:=b[k+1]+b[k] div n;
b[k]:=b[k] mod n;
if k=c
then c:=c+1;
end;
end;
a:=b;
for i:=1 to c do
if a[i]<>a[c-i+1]
then break;
if i=c
then begin writeln('STEP=',js);exit;end;
end;
writeln('Impossible!');
end.