- 回文数
- 2016-08-23 14:35:28 @
var
n,i,j,k,l,x,y,z,num:longint;
m:ansistring;
a,b,c:array[1..1000000]of longint;
d:boolean;
begin
readln(n);
readln(m);
y:=length(m);k:=0;
for i:=1 to y do
begin
j:=y-i+1;
case m[i] of
'0'..'9' : val(m[i],a[j]);
'A' : a[j]:=10;
'B' : a[j]:=11;
'C' : a[j]:=12;
'D' : a[j]:=13;
'E' : a[j]:=14;
'F' : a[j]:=15;
end;
end;
j:=1;
for i:=y downto 1 do
begin
b[j]:=a[i];
inc(j);
end;
j:=y div 2;
d:=true;
num:=0;
for i:=1 to j do
if a[i]<>a[y-i+1] then begin d:=false; end;
if d=true then writeln('STEP=',num)
else begin
repeat
d:=true;
i:=1; x:=0;
while i<=y do
begin
c[i]:=a[i]+b[i]+x;
x:=c[i] div n;
c[i]:=c[i] mod n;
i:=i+1;
end;
if x>0 then begin
z:=i; c[i]:=x; end
else z:=i-1;k:=k+z;
for l:=1 to z do
begin
a[l]:=0;
a[l]:=a[l]+c[l];
b[l]:=0;b[l]:=b[l]+c[k];dec(k);
end;
y:=z;
j:=y div 2;
num:=num+1;
for i:=1 to j do
if a[i]<>a[y-i+1] then begin d:=false;break;end
else begin d:=true; end;
until d or (num>=30);
end;
if num<=30 then writeln('STEP=',num)
else writeln('Impossible!');
end.