Wednesday, 20 January 2016

Palindrom Numbers (UVALive 2389, ZOJ 1078, Regionals 2001 >> Latin America - South America)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100000

/*long long toDecimal(string s,int baseFrom)
{
    int cnt=0;
    long long res=0;
    for(int i=s.length()-1;i>=0;i--)
    {
        if ( s[i] > 47 && s[i] < 58 ) res+= (pow(baseFrom,cnt)*(s[i]-'0'));
        else  res+= (pow(baseFrom,cnt)*(s[i]-55));
        cnt++;
    }
    return res;
}*/

string deciamlTo(int num, int baseTo)
{
    string s="";
    while(num)
    {
        int tmp=num%baseTo;
        if(tmp<10)   s+=tmp+'0';
        else   s+= char (tmp+55);
        num/=baseTo;
    }
    if(s=="")    return "0";
    reverse(s.begin(), s.end());
    return s;
}

bool isPalindrome(string s)
{
    int sl=s.size()-1, st=0;
    while(st<sl)
    {
        if(s[st]!=s[sl]) return false;
        st++;
        sl--;
    }
    return true;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n;
    string tmp;
    while(cin>>n && n)
    {
        vector<int>v;
        for(int i=2;i<=16;i++)
        {
            tmp = deciamlTo(n, i);
            if(isPalindrome(tmp))
                v.push_back(i);
        }
        if(v.size()==0) cout<<"Number "<<n<<" is not palindrom\n";
        else
        {
            cout<<"Number "<<n<<" is palindrom in basis";
            for(int i=0;i<v.size();i++)
                cout<<" "<<v[i];
            cout<<"\n";
        }
    }

return 0;
}

No comments:

Post a Comment