|
#include<iostream>
using namespace std;
const int mm=100;
int s[mm],a[mm],i,p,x,n;
string M;
int N;
string d(string x)///颠倒
{
string y="";
for(int i=0; i<=x.size()-1; i++)y=x+y;
return y;
}
bool isr(string x)///判断回文
{
return x==d(x);
}
string add(string a,string b)///高精加
{
string c="";
int j=0;
if(a.size()<b.size())swap(a,b);
for(int i=1; i<=a.size()-b.size(); i++)b=b+"0";
for(int i=a.size()-1; i>=0; i--)
{
int x1;
if(a<='9')x1=a-'0';
if('A'<=a)x1=a-'A'+10;
int x2;
if(b<='9')x2=b-'0';
if('A'<=b)x2=b-'A'+10;
int x3=x1+x2+j;
//cout<<i<<" x1:"<<x1<<" x2:"<<x2<<" x3:"<<x3<<" j:"<<j<<endl;
j=x3/N;///考虑N进制
x3%=N;
char ch;
if(x3>=10)ch='A'+x3-10;
else ch='0'+x3;
c=ch+c;
//cout<<"a["<<i<<"]:"<<a<<endl;cout<<"b["<<i<<"]:"<<b<<endl;
}
if(j>0)c=char(j+'0')+c;
//cout<<c<<endl;
return c;
}
int main()
{
cin>>N>>M;
int ans=0;
while(!isr(M) && ans<30)
{
ans++;
M=add(M,d(M));
}
if(ans<30)cout<<"STEP="<<ans;
else cout<<"Impossible!";
return 0;
}
|
|