QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#51083#1289. A + B Problemckiseki#RE 2ms3556kbC++1.4kb2022-09-30 17:29:362022-09-30 17:29:39

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-09-30 17:29:39]
  • 评测
  • 测评结果:RE
  • 用时:2ms
  • 内存:3556kb
  • [2022-09-30 17:29:36]
  • 提交

answer

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

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int T;
    cin >> T;
    while (T--) {
        int n, m;
        cin >> n >> m;
        string S, A, B;
        cin >> S;
        assert (S.size() == n + m);
        const int orgn = n, orgm = m;
        for (char c: S) {
            if (c == '1') {
                if (n > m) {
                    A += c;
                    --n;
                } else {
                    B += c;
                    --m;
                }
            } else {
                if (n <= m && n > 0) {
                    A += c;
                    --n;
                } else {
                    B += c;
                    --m;
                }
            }
        }
        n = orgn, m = orgm;
        assert (A.size() == n);
        assert (B.size() == m);
        reverse(A.begin(), A.end());
        reverse(B.begin(), B.end());
        string C(max(n, m), '\0');
        int carry = 0;
        for (int i = 0; i < max(n, m); i++) {
            int x = carry + (i < n ? A[i] - '0' : 0) + (i < m ? B[i] - '0' : 0);
            C[i] = char(x % 2 + '0');
            carry = x / 2;
        }
        if (carry) {
            assert (carry == 1);
            C += char(carry + '0');
        }
        reverse(C.begin(), C.end());
        while (C.size() > 1 && C.back() == 0)
            C.pop_back();
        cout << C << '\n';
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3556kb

input:

3
4 3
1000101
2 2
1111
1 1
00

output:

1101
110
0

result:

ok 3 lines

Test #2:

score: -100
Dangerous Syscalls

input:

11110
10 8
111011010011100100
3 5
01011000
7 6
1110101010000
9 1
0110100101
1 9
0100001110
8 10
000101101011111000
9 6
011111111000111
1 9
1011101101
10 7
00100011000100000
4 9
1000101101010
8 4
100100110000
8 9
00101111011000101
8 9
11000000101011110
7 6
1111010100110
2 9
01001110101
4 5
100010100
...

output:


result: