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;
}

全部评论
搞出来了,有一个测试点有问题一直过不了,貌似因为他有一行里包含了两个数,所以在转换成int的时候报错了,这也意味着那个测试点里面的数没有n+1行输入,所以也不能用for循环,得改成while循环,然后以列表读取。 n,m=map(int, input().split()) a=[] while len(a)<n:     a+=list(map(int, input().split())) a.sort() res=0 for i in range(1,n):     x=a[i-1]     y=a[i]     ans=0     for j in range(m):         if x>>j&1==y>>j&1:             ans+=1<<j     res=max(res,ans) print(res)
1 回复 分享
发布于 2024-08-07 18:53 浙江

相关推荐

机智的豹子有点心碎:UU我还在找工作还没找到,一直在搜简历怎么改,总结了这些: 1.SEO:简历根据每一个岗位定制化:使用这个岗位中所描述的工作的词,它要求什么技能就把自己的技能描述成什么样子,把SEO用在自己身上(把我的简历和个人特质,当成一个热门产品来做 “搜索引擎优化”),让HR能用最低的门槛看到我 2."顺序:把岗位要求的技能跟经历放在简历的最开头、最显眼的位置" 3.包装:简历是一个最终交付说明书,只要最终学习成长做得到就可以,在合适的范围内自我吹捧(我这个人怎么能够在HR的角度被迅速的看懂和看到,减轻HR的工作压力) 4.每点加小标题​:用6~10字概括该段内容,便于面试官快速抓取信息。 5.避免空泛描述​:拒绝“培养了组织能力”等泛泛而谈,替换为具体行动和成果。 6."使用“三段式结构”​​:每段经历按“为什么做-做了什么-结果如何”展开: ​a) 为什么做​:痛点或目标(例如“品牌声量不足”) ​b) 做​了什么:方法论(例如“趋势洞察+竞品对标+人群细分”) ​c) 结果如何​:量化成果或影响(例如“推动客户投放20万预算”)" 7.量化成果​:用数字体现工作成效(如“整理500+份资料”“撰写2万字报告”)。 这些有的是我想去的岗的,如果对你有用的话按需修改就好~加油,早日上岸!
点赞 评论 收藏
分享
04-13 11:19
门头沟学院 HTML5
NullPointe...:27实习的都快结束了吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务