题目 › usaco-1452

All Pairs Similarity

USACO 2024 December · 铂金组 · 2024-12 · L4 顶尖 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

来源:USACO 官网。模型收到的是同一份题面的文字。

**Note: The memory limit for this problem is 512MB, twice the default.**

Farmer John's $N$ ($1\le N\le 5\cdot 10^5$) cows are each assigned a bitstring of length $K$ that is not all zero ($1\le K\le 20$). Different cows may be assigned the same bitstring.

The Jaccard similarity of two bitstrings is defined as the number of set bits in their bitwise intersection divided by the number of set bits in their bitwise union. For example, the Jaccard similarity of the bitstrings $\texttt{11001}$ and $\texttt{11010}$ would be $2/4$.

For each cow, output the sum of her bitstring's Jaccard similarity with each of the $N$ cows' bitstrings including her own, modulo $10^9+7$. Specifically, if the sum is equal to a rational number $a/b$ where $a$ and $b$ are integers sharing no common factors, output the unique integer $x$ in the range $[0,10^9+7)$ such that $bx-a$ is divisible by $10^9+7$.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains $N$ and $K$.

The next $N$ lines each contain an integer $i\in (0,2^K)$, representing a cow associated with the length-$K$ binary representation of $i$.

INPUT FORMAT (input arrives from the terminal / stdin):

Output the sum modulo $10^9+7$ for each cow on a separate line.

SAMPLE INPUT:

4 2
1
1
2
3

SAMPLE OUTPUT:

500000006
500000006
500000005
500000006

The cows are associated with the following bitstrings: $[\texttt{01}, \texttt{01}, \texttt{10}, \texttt{11}]$.

For the first cow, the sum is $\text{sim}(1,1)+\text{sim}(1,1)+\text{sim}(1,2)+\text{sim}(1,3)=1+1+0+1/2\equiv 500000006\pmod{10^9+7}$.

The second cow's bitstring is the same as the first cow's, so her sum is the same as above.

For the third cow, the sum is

$\text{sim}(2,1)+\text{sim}(2,1)+\text{sim}(2,2)+\text{sim}(2,3)=0+0+1+1/2\equiv 500000005\pmod{10^9+7}$.

SCORING:

  • Inputs 2-15: There will be two test cases for each of $K\in \{10,15,16,17,18,19,20\}$.

Problem credits: Benjamin Qi

结果

Kimi K3.1 ProKimi

max

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 15.7k平均输出 token(含思考) ↓
  • 24.3输出速度 tokens/s ↑
  • 676 秒延迟 ↓
  • 1.67×CPU 时间 / 官方解 ↓
  • 更差 1、同阶 2时间复杂度(相对官方解)

Kimi K3.1 Pro(high)Kimi high

high

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 15.7k平均输出 token(含思考) ↓
  • 24.3输出速度 tokens/s ↑
  • 676 秒延迟 ↓
  • 1.67×CPU 时间 / 官方解 ↓
  • 更差 1、同阶 2时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 37.2k平均输出 token(含思考) ↓
  • 220.3输出速度 tokens/s ↑
  • 193 秒延迟 ↓
  • 1.60×CPU 时间 / 官方解 ↓
  • 不可测 3时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

2/3

解出的采样 ↑

  • 0.0采样 1 · 答案错误
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 27.5k平均输出 token(含思考) ↓
  • 144.3输出速度 tokens/s ↑
  • 137 秒延迟 ↓
  • 1.24×CPU 时间 / 官方解 ↓
  • 不确定 1、同阶 1时间复杂度(相对官方解)

↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 87 行
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

static constexpr uint32_t MOD = 1000000007;
static constexpr int MAXK = 20;

struct Poly {
    uint32_t a[MAXK + 1];
    uint32_t b[MAXK + 1];
};

static inline uint32_t addmod(uint32_t a, uint32_t b) {
    uint32_t s = a + b;
    return s - (MOD & (uint32_t)-(int)(s >= MOD));
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, K;
    cin >> N >> K;
    const int M = 1 << K;

    vector<Poly> dp(M);
    vector<int> cows(N);

    for (int i = 0; i < N; ++i) {
        cin >> cows[i];
        ++dp[cows[i]].a[0];
    }

    vector<uint8_t> pc(M);
    for (int i = 1; i < M; ++i)
        pc[i] = pc[i >> 1] + (i & 1);

    // dp[x].a + delta * dp[x].b represents
    // sum_y f[y] * (1+delta)^|x&y| * r^|y\x|, modulo delta^2.
    for (int lev = 1, bit = 1; lev <= K; ++lev, bit <<= 1) {
        for (int base = 0; base < M; base += bit << 1) {
            for (int off = 0; off < bit; ++off) {
                Poly &p = dp[base + off];
                Poly &q = dp[base + off + bit];

                int maxd = lev - pc[off];
                uint32_t prevA = 0, prevB = 0;

                for (int d = 0; d <= maxd; ++d) {
                    uint32_t a0 = p.a[d], b0 = p.b[d];
                    uint32_t a1 = q.a[d], b1 = q.b[d];

                    q.a[d] = addmod(a0, a1);
                    q.b[d] = addmod(addmod(b0, b1), a1);

                    p.a[d] = addmod(a0, prevA);
                    p.b[d] = addmod(b0, prevB);

                    prevA = a1;
                    prevB = b1;
                }
            }
        }
    }

    vector<uint32_t> inv(K + 1);
    inv[1] = 1;
    for (int i = 2; i <= K; ++i)
        inv[i] = MOD - (uint64_t)(MOD / i) * inv[MOD % i] % MOD;

    vector<int> answer(M, -1);

    for (int x : cows) {
        if (answer[x] == -1) {
            int s = pc[x];
            uint64_t res = 0;
            for (int v = 0; v <= K - s; ++v) {
                res += (uint64_t)dp[x].b[v] * inv[s + v];
                res %= MOD;
            }
            answer[x] = (int)res;
        }
        cout << answer[x] << '\n';
    }

    return 0;
}