• 个人简介

    有 N 个正整数 ,已知其中恰好有 K<5000 个数出现的次数均为奇数。请你找出这 K 个数。时间限制 1000ms,空间限制 5MB 要求使用C++17进行解答,1<=N<=3*10^6 所有数字均为int范围内的正整数

    #include <cstdio>
    #include <cstdint>
    #include <algorithm>
    
    namespace io {
        const int BUF_SIZE = 1 << 16;
        char buf[BUF_SIZE];
        int pos = 0, len = 0;
        inline char next_char() {
            if (pos == len) {
                pos = 0;
                len = fread(buf, 1, BUF_SIZE, stdin);
                if (len == 0) return EOF;
            }
            return buf[pos++];
        }
        inline uint32_t read_uint32() {
            uint32_t x = 0;
            char c = next_char();
            while (c < '0' || c > '9') { if (c == EOF) return 0; c = next_char(); }
            while (c >= '0' && c <= '9') { x = x * 10 + (c - '0'); c = next_char(); }
            return x;
        }
        char out_buf[BUF_SIZE];
        int out_pos = 0;
        inline void flush_output() { fwrite(out_buf, 1, out_pos, stdout); out_pos = 0; }
        inline void write_uint32(uint32_t x) {
            if (out_pos >= BUF_SIZE - 20) flush_output();
            if (x == 0) { out_buf[out_pos++] = '0'; out_buf[out_pos++] = '\n'; return; }
            char temp[10]; int tl = 0;
            while (x > 0) { temp[tl++] = '0' + (x % 10); x /= 10; }
            for (int i = tl - 1; i >= 0; i--) out_buf[out_pos++] = temp[i];
            out_buf[out_pos++] = '\n';
        }
    }
    
    const int M = 65536;       // 桶数 2^16
    const int D = 5;            // 哈希表数量
    const int MASK = M - 1;
    
    uint32_t buckets[D][M];     // 1.25 MB
    const uint32_t seeds[D] = { 0x12345678, 0x9abcdef0, 0xdeadbeef, 0xfeedface, 0x8badf00d };
    
    inline uint32_t hash(uint32_t x, uint32_t seed) {
        x ^= seed;
        x ^= x >> 16;
        x *= 0x85ebca6b;
        x ^= x >> 13;
        x *= 0xc2b2ae35;
        x ^= x >> 16;
        return x & MASK;
    }
    
    uint32_t candidates[50000]; // 0.19 MB (关键优化:从 393216 缩小到 50000)
    int candidate_count = 0;
    
    int main() {
        int n = (int)io::read_uint32();
        int k = (int)io::read_uint32();
    
        // 第一遍:XOR 哈希
        for (int i = 0; i < n; i++) {
            uint32_t x = io::read_uint32();
            for (int d = 0; d < D; d++)
                buckets[d][hash(x, seeds[d])] ^= x;
        }
    
        // 第二遍:收集无碰撞的候选值
        for (int d = 0; d < D; d++) {
            for (int b = 0; b < M; b++) {
                uint32_t v = buckets[d][b];
                if (v != 0) {
                    // 验证:v 单独占据一个桶(无碰撞)
                    if (buckets[d][hash(v, seeds[d])] == v) {
                        candidates[candidate_count++] = v;
                    }
                }
            }
        }
    
        // 排序去重
        std::sort(candidates, candidates + candidate_count);
        candidate_count = (int)(std::unique(candidates, candidates + candidate_count) - candidates);
    
        // 输出
        for (int i = 0; i < candidate_count; i++)
            io::write_uint32(candidates[i]);
        io::flush_output();
    
        return 0;
    }
  • 通过的题目

  • 最近活动

  • 最近编写的题解