Submission

Status:

PPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: Kei

Problemset: บวกเลขฐาน

Language: cpp

Time: 0.004 second

Submitted On: 2026-10-07 12:22:02

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

int val(char ch){ return ch <= '9' ? ch - '0' : ch - 'A' + 10; }
char dig(int v){ return "0123456789ABCDEF"[v]; }

string base_add(int base, string a, string b){
    int thea,theb,theadd,carry=0;
    if(a.size()>b.size()) b=string(a.size()-b.size(),'0')+b;
    else if(b.size()>a.size()) a=string(b.size()-a.size(),'0')+a;
    string c(a.size()+1,'0');
    for(int i=a.size()-1;i>=0;i--){
        thea = val(a[i]);
        theb = val(b[i]);
        theadd = thea+theb+carry;
        c[i+1]=dig(theadd%base);
        carry=theadd/base;
    }
    if(carry!=0)c[0] = dig(carry);   
    int p=0;
    while(p+1<(int)c.size() && c[p]=='0')p++;
    return c.substr(p);
}

void solve(){
    int base;
    string n1,n2;
    cin >> base >> n1 >> n2;
    cout << base_add(base,n1,n2);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}