UVa 443 - Humble Numbers Solution

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<long long> hum;
long long pw(long long x,long long p){
    long long ans=1;
    while(p){
        if(p&(1ll))
            ans*=x;
        x*=x,p>>=1;
    }
    return ans;
}
int main(){
    ios::sync_with_stdio(0);
    for(int i=0;i<31;i++){
        long long a=pw(2,i);
        for(int j=0;j<20;j++){
            long long b=pw(3,j);
            if(a*b<0||a*b>2e10)
                break;
            for(int k=0;k<14;k++){
                long long c=pw(5,k);
                if(a*b*c<0||a*b*c>2e10)
                    break;
                for(int l=0;l<12;l++){
                    long long d=pw(7,l);
                    if(a*b*c*d<0||a*b*c*d>2e10)
                        break;
                    hum.push_back(a*b*c*d);
                }
            }
        }
    }
    sort(hum.begin(),hum.end());
    int n;
    while(cin>>n){
        if(n==0)
            return 0;
        cout<<"The "<<n;
        if(n%100==11||n%100==12||n%100==13)
            cout<<"th";
        else if(n%10==1)
            cout<<"st";
        else if(n%10==2)
            cout<<"nd";
        else if(n%10==3)
            cout<<"rd";
        else
            cout<<"th";
        cout<<" humble number is "<<hum[n-1]<<'.'<<endl;
    }
}

Comments