B题是否无法用Python通过?
同样的思路用Python会报错,报错内容为:“程序异常退出,请检查是否存在语法错误或者数组越界非法访问等情况
Warning: cannot find your CPU L2 cache size in /proc/cpuinfo
Traceback (most recent call last):
File "a.pypy3", line 8, in <module>
nums.append(int(input()))
ValueError: invalid literal for int() with base 10: '0 1073741823'
”
但C++却可以正常通过。
代码如下:
Python:
from collections import defaultdict if __name__ == '__main__': n, m = map(int, input().split()) nums = [] for i in range(n): nums.append(int(input())) ans = 0 for i in range(m - 1, -1, -1): mask_to_nums = defaultdict(lambda: defaultdict(int)) ok = False for j in range(n): cur = nums[j] & ans mask_to_nums[cur][nums[j] >> i & 1] += 1 if mask_to_nums[cur][nums[j] >> i & 1] >= 2: ok = True break if ok: ans |= 1 << i print(ans)
C++:
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <vector> #include <array> #include <algorithm> #include <bitset> #include <string> #include <sstream> #include <chrono> #include <cstring> #include <climits> #include <cassert> #include <cmath> #include <stack> #include <queue> #include <numeric> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <iomanip> #include <tuple> #include <functional> using namespace std; typedef long long LL; typedef unsigned long long ULL; constexpr int INF = 0x3f3f3f3f; constexpr int MOD = 998244353; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, m; cin >> n >> m; vector<int> nums(n); for (int i = 0; i < n; ++i) { cin >> nums[i]; } int ans = 0; for (int i = m - 1; i >= 0; --i) { unordered_map<int, unordered_map<int, int>> mask_to_nums; bool ok = false; for (int j = 0; j < n; ++j) { int cur = nums[j] & ans; mask_to_nums[cur][(nums[j] >> i) & 1]++; if (mask_to_nums[cur][(nums[j] >> i) & 1] >= 2) { ok = true; break; } } if (ok) { ans |= 1 << i; } } cout << ans << endl; return 0; }