Official

B - Base K Editorial by en_translator


We will first aim at creating a function that converts \(S\) given in base \(K\) into its decimal representation.
This can be achieved by the following algorithm.

Let \(ans = 0\).
Inspect each digit of \(S\) one by one and perform the following operations in order.

  • Multiply \(ans\) by \(K\)
  • Add current digit to \(ans\)

Once the last digit has been inspected, \(ans\) is the desired value.

Finally, we can multiply \(A\) and \(B\) in base \(10\) to obtain the answer.

Please also refer to the sample code in C++, which we show below.

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


ll f(string s,ll k){ // A function that converts S in base K into its decimal representation
    ll ans=0;
    for(char x:s){
        ans *= k;
        ans += x - '0';
    }
    return ans;
}

signed main(){

    ll k;
    string a,b;
    cin>>k>>a>>b;
    ll A = f(a,k);
    ll B = f(b,k);
    cout<< A * B <<endl;

    return 0;
}

posted:
last update: