QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#750090#7695. Double UpalexhamidiAC ✓2ms3876kbC++142.9kb2024-11-15 12:35:152024-11-15 12:35:15

Judging History

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

  • [2024-11-15 12:35:15]
  • 评测
  • 测评结果:AC
  • 用时:2ms
  • 内存:3876kb
  • [2024-11-15 12:35:15]
  • 提交

answer

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

class BigNumber {
    std::string value;

public:
    BigNumber(const std::string& s = "0") : value(s) {
        // Remove leading zeros
        size_t firstNonZero = value.find_first_not_of('0');
        if (firstNonZero != std::string::npos)
            value = value.substr(firstNonZero);
        else
            value = "0";
    }

    bool operator==(const BigNumber& other) const {
        return value == other.value;
    }

    // Fixed multiplication by 2 specifically for this problem
    BigNumber multiplyBy2() const {
        if (value == "0") return BigNumber("0");

        std::vector<int> result(value.length() + 1, 0);
        int carry = 0;

        // Multiply each digit by 2
        for (int i = value.length() - 1; i >= 0; i--) {
            int digit = (value[i] - '0') * 2 + carry;
            result[i + 1] = digit % 10;
            carry = digit / 10;
        }
        result[0] = carry;

        // Convert back to string
        std::string resultStr;
        bool started = false;
        for (int digit : result) {
            if (digit != 0 || started) {
                resultStr += std::to_string(digit);
                started = true;
            }
        }
        if (resultStr.empty()) resultStr = "0";
        return BigNumber(resultStr);
    }

    bool operator<(const BigNumber& other) const {
        if (value.length() != other.value.length())
            return value.length() < other.value.length();
        return value < other.value;
    }

    friend std::ostream& operator<<(std::ostream& os, const BigNumber& num) {
        os << (num.value.empty() ? "0" : num.value);
        return os;
    }
};

BigNumber solve(std::vector<BigNumber>& nums) {
    bool changed = true;
    while (changed && nums.size() > 1) {
        changed = false;
        // Try merging first
        for (int i = 0; i < nums.size() - 1; i++) {
            if (nums[i] == nums[i + 1]) {
                nums[i] = nums[i].multiplyBy2();
                nums.erase(nums.begin() + i + 1);
                changed = true;
                break;
            }
        }

        // If no merging is possible, remove the smallest number
        if (!changed && nums.size() > 1) {
            int minIdx = 0;
            for (int i = 1; i < nums.size(); i++) {
                if (nums[i] < nums[minIdx]) {
                    minIdx = i;
                }
            }
            nums.erase(nums.begin() + minIdx);
            changed = true;
        }
    }

    return nums[0];
}

int main() {
    int n;
    std::cin >> n;

    std::vector<BigNumber> nums;
    for (int i = 0; i < n; i++) {
        std::string num;
        std::cin >> num;
        nums.push_back(BigNumber(num));
    }

    BigNumber result = solve(nums);
    std::cout << result << "\n";

    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3604kb

input:

5
4 2 2 1 8

output:

16

result:

ok single line: '16'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3664kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

512

result:

ok single line: '512'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3716kb

input:

1000
1267650600228229401496703205376 1267650600228229401496703205376 1267650600228229401496703205376 1267650600228229401496703205376 1267650600228229401496703205376 1267650600228229401496703205376 1267650600228229401496703205376 1267650600228229401496703205376 1267650600228229401496703205376 1267650...

output:

649037107316853453566312041152512

result:

ok single line: '649037107316853453566312041152512'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3836kb

input:

1
1

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 0ms
memory: 3624kb

input:

1
2

output:

2

result:

ok single line: '2'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3804kb

input:

1
4294967296

output:

4294967296

result:

ok single line: '4294967296'

Test #7:

score: 0
Accepted
time: 0ms
memory: 3768kb

input:

1
18446744073709551616

output:

18446744073709551616

result:

ok single line: '18446744073709551616'

Test #8:

score: 0
Accepted
time: 0ms
memory: 3840kb

input:

1
1267650600228229401496703205376

output:

1267650600228229401496703205376

result:

ok single line: '1267650600228229401496703205376'

Test #9:

score: 0
Accepted
time: 1ms
memory: 3584kb

input:

384
18014398509481984 72057594037927936 72057594037927936 72057594037927936 72057594037927936 72057594037927936 72057594037927936 72057594037927936 72057594037927936 72057594037927936 72057594037927936 72057594037927936 72057594037927936 72057594037927936 72057594037927936 72057594037927936 10737418...

output:

618970019642690137449562112

result:

ok single line: '618970019642690137449562112'

Test #10:

score: 0
Accepted
time: 1ms
memory: 3632kb

input:

430
36893488147419103232 128 2251799813685248 2251799813685248 2251799813685248 2251799813685248 2251799813685248 2251799813685248 2251799813685248 2251799813685248 2251799813685248 2251799813685248 2251799813685248 2251799813685248 2251799813685248 2251799813685248 2251799813685248 2251799813685248...

output:

158456325028528675187087900672

result:

ok single line: '158456325028528675187087900672'

Test #11:

score: 0
Accepted
time: 1ms
memory: 3648kb

input:

527
1099511627776 1099511627776 1099511627776 1099511627776 1099511627776 1099511627776 1099511627776 1099511627776 1099511627776 1099511627776 1099511627776 1099511627776 1099511627776 1099511627776 1099511627776 1099511627776 1099511627776 1099511627776 1099511627776 1099511627776 1099511627776 10...

output:

158456325028528675187087900672

result:

ok single line: '158456325028528675187087900672'

Test #12:

score: 0
Accepted
time: 2ms
memory: 3600kb

input:

809
1073741824 77371252455336267181195264 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 2097152 20...

output:

19807040628566084398385987584

result:

ok single line: '19807040628566084398385987584'

Test #13:

score: 0
Accepted
time: 1ms
memory: 3600kb

input:

435
618970019642690137449562112 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 65536 128 128 128 128 128 128 128 128 128 128 128 128 128 ...

output:

316912650057057350374175801344

result:

ok single line: '316912650057057350374175801344'

Test #14:

score: 0
Accepted
time: 2ms
memory: 3552kb

input:

857
4398046511104 4398046511104 4398046511104 4398046511104 4398046511104 4398046511104 4398046511104 4398046511104 4398046511104 4398046511104 4398046511104 4398046511104 4398046511104 4398046511104 4398046511104 4398046511104 4398046511104 4398046511104 4398046511104 4398046511104 4398046511104 43...

output:

1267650600228229401496703205376

result:

ok single line: '1267650600228229401496703205376'

Test #15:

score: 0
Accepted
time: 2ms
memory: 3704kb

input:

765
17592186044416 2417851639229258349412352 2417851639229258349412352 2417851639229258349412352 2417851639229258349412352 2417851639229258349412352 2417851639229258349412352 2417851639229258349412352 2417851639229258349412352 2417851639229258349412352 2417851639229258349412352 241785163922925834941...

output:

1237940039285380274899124224

result:

ok single line: '1237940039285380274899124224'

Test #16:

score: 0
Accepted
time: 1ms
memory: 3808kb

input:

122
2097152 18014398509481984 18014398509481984 18014398509481984 18014398509481984 18014398509481984 18014398509481984 18014398509481984 18014398509481984 18014398509481984 18014398509481984 18014398509481984 18014398509481984 18014398509481984 18014398509481984 18014398509481984 18014398509481984 ...

output:

10141204801825835211973625643008

result:

ok single line: '10141204801825835211973625643008'

Test #17:

score: 0
Accepted
time: 1ms
memory: 3876kb

input:

491
8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 8192 562949953421312 9444732965739290427392 9444732965739290427392 9444732965739290427392 9444732965739290427392 9444732965739290427392 94447329657392904273...

output:

1267650600228229401496703205376

result:

ok single line: '1267650600228229401496703205376'

Test #18:

score: 0
Accepted
time: 1ms
memory: 3616kb

input:

164
549755813888 549755813888 549755813888 549755813888 549755813888 549755813888 549755813888 549755813888 549755813888 549755813888 549755813888 549755813888 549755813888 549755813888 549755813888 549755813888 549755813888 549755813888 549755813888 549755813888 549755813888 549755813888 5497558138...

output:

20282409603651670423947251286016

result:

ok single line: '20282409603651670423947251286016'