Submission
Status:
PPPPPPPxP
Subtask/Task Score:
96/100
Score: 96
User: Newton4566
Problemset: บวกเลขฐาน
Language: cpp
Time: 0.004 second
Submitted On: 2026-10-10 15:23:04
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
string toBase(LL n, int b) {
if (n == 0) return 0;
string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string s = "";
while (n > 0) {
s += digits[n % b];
n /= b;
}
reverse(s.begin(), s.end());
return s;
}
LL fromBase(const string& s, int b) {
LL result = 0;
int i = 0;
for (; i < s.size(); ++i) {
char c = s[i];
int d;
if (c >= '0' && c <= '9') d = c - '0';
else d = c - 'A' + 10;
result = result * b + d;
}
return result;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int base;
string n1, n2;
cin >> base >> n1 >> n2;
LL sum = fromBase(n1, base) + fromBase(n2, base);
cout << toBase(sum, base);
return 0;
}