Uva 10551 Solution

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
ll other_deci(string s, int b)
{
    if(s == "0")
        return 0;

    ll r = s[0] - '0';
    for(int i=1; i<s.size(); i++)
        r = (r*b)+(s[i]-'0');

    return r;
}
string deci_other(ll n, int b)
{
    string s = "";

    while(n > 0)
    {
        int r = n%b;
        s = s + to_string(r);
        n = n/b;
    }
    reverse(s.begin(), s.end());
    return s;
}
int main()
{
    int n;
    string num, m;

    while(cin >> n && n > 0)
    {
        cin >> num >> m;
        ll r1 = other_deci(m, n);

        ll r = num[0] - '0';
        for(int i=1; i<num.size(); i++)
        {
            r = (r*n) + (num[i]-'0');
            r = r%r1;
        }

        string s = deci_other(r, n);
        if(s == "")
            cout << 0 << "\n";
        else
            cout << s << "\n";
    }

    return 0;
}

Comments