Submission

Status:

PPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: Prap

Problemset: บวกเลขฐาน

Language: cpp

Time: 0.003 second

Submitted On: 2026-04-03 17:04:34

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

// Helper to convert char to its integer value ('A' -> 10)
int charToInt(char c) {
    if (c >= '0' && c <= '9') return c - '0';
    return c - 'A' + 10;
}

// Helper to convert integer back to char (10 -> 'A')
char intToChar(int n) {
    if (n >= 0 && n <= 9) return n + '0';
    return n - 10 + 'A';
}

int main() {
    int N;
    string a, b;
    if (!(cin >> N >> a >> b)) return 0;

    string result = "";
    int i = a.length() - 1;
    int j = b.length() - 1;
    int carry = 0;

    // Loop until both strings are exhausted and no carry remains
    while (i >= 0 || j >= 0 || carry > 0) {
        int sum = carry;
        
        if (i >= 0) sum += charToInt(a[i--]);
        if (j >= 0) sum += charToInt(b[j--]);

        result += intToChar(sum % N); // Get the current digit
        carry = sum / N;             // Calculate the carry for the next column
    }

    // Since we appended digits, the result is backwards
    reverse(result.begin(), result.end());

    cout << result << endl;

    return 0;
}