题目 › usaco-1564

Moo Hunt

USACO 2026 Second · 铜组 · 2026-01 · L1 基础 · Python 3.11 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

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

Bessie is playing the popular game "Moo Hunt". In this game, there are $N$ ($3 \le N \le 20$) cells in a line, numbered from $1$ to $N$. All cells either have the character $M$ or $O$ with the $i$-th cell having character $s_i$.

Bessie plans to perform $K$ ($1 \le K \le 2 \cdot 10^5$) mooves. On her $i$-th moove, Bessie will tap $3$ different cells ($x_{i},y_{i},z_{i}$) ($1 \le x_{i},y_{i},z_{i} \le N$). Bessie will earn a point if $s_{x_i}=M$ and $s_{y_i}=s_{z_i}=O$. In other words, Bessie will earn a point if she forms the string $MOO$ by tapping cells $x_{i},y_{i},z_{i}$ in that order.

Farmer John wants to help Bessie get a new high score. He wants you to find the maximum possible score Bessie could get across all possible boards if she performs the $K$ mooves as well as the number of different boards that will allow Bessie to achieve this maximum possible score. Two boards are different if there exists a cell such that the corresponding characters at the cell are different.

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

The first line contains $N$ and $K$, the number of cells and the number of mooves Bessie will perform.

Each of the next $K$ lines contains $x_i, y_i, z_i$ describing Bessie's $i$-th move ($x_i, y_i, z_i$ are pairwise distinct).

OUTPUT FORMAT (print output to the terminal / stdout):

Output the maximum possible score Bessie could achieve, followed by the count of different boards that will allow Bessie to achieve this maximum score.

SAMPLE INPUT:

5 6
1 2 3
1 2 3
1 3 5
2 3 4
5 3 2
5 2 3

SAMPLE OUTPUT:

4 2
The boards $MOOOM$ and $MOOMM$ allow Bessie to achieve a maximum score of $4$. In both boards, Bessie will earn points on mooves $1,2,5,6$. It can be shown that this is the maximum score Bessie can achieve, and those two boards are the only possible boards allowing Bessie to achieve a score of $4$.

SAMPLE INPUT:

6 12
2 4 3
2 3 4
3 5 2
3 5 1
3 1 5
3 1 2
6 1 5
1 6 4
2 3 6
3 6 2
4 1 6
3 4 2

SAMPLE OUTPUT:

6 3
The boards that allow Bessie to achieve a maximum possible score of $6$ are $OOMOOO$, $OOMMOO$, and $OOMOOM$.

SCORING:

  • Inputs 3-5: $N \le 8, K \le 10^4$
  • Inputs 6-12: There will be one test for each $N \in \{14,15,16,17,18,19,20\}$ with no additional constraints on $K$.

Problem credits: Alex Liang

结果

Kimi K3.1 ProKimi

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 12.9k平均输出 token(含思考) ↓
  • 27.4输出速度 tokens/s ↑
  • 471 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 4Pylint 问题 ↓
  • 9.39Pylint 得分 ↑
  • 10圈复杂度 ↓
  • 58.9可维护性指数 ↑

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 3.4k平均输出 token(含思考) ↓
  • 217.4输出速度 tokens/s ↑
  • 16 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 3Pylint 问题 ↓
  • 9.29Pylint 得分 ↑
  • 9圈复杂度 ↓
  • 61.6可维护性指数 ↑

MiniMax M3.1 FlashMiniMax

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 12.9k平均输出 token(含思考) ↓
  • 179.3输出速度 tokens/s ↑
  • 73 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 3Pylint 问题 ↓
  • 8.85Pylint 得分 ↑
  • 6圈复杂度 ↓
  • 70.6可维护性指数 ↑

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

代码

Kimi K3.1 Pro · 采样 1 · 99 行
import sys


def main():
    it = iter(map(int, sys.stdin.buffer.read().split()))
    n = next(it)
    k = next(it)

    # Aggregate identical mooves.
    weights = {}
    nn = n * n
    for _ in range(k):
        x = next(it) - 1
        y = next(it) - 1
        z = next(it) - 1
        key = (x * n + y) * n + z
        weights[key] = weights.get(key, 0) + 1
    del it

    # For a set S of cells containing M, a moove contributes
    # w * [x in S] * [y not in S] * [z not in S].
    # Expanding gives linear, pairwise, and triple terms.
    linear = [0] * n
    pair = [[0] * n for _ in range(n)]
    triple = [[[0] * n for _ in range(n)] for _ in range(n)]

    for key, w in weights.items():
        x = key // nn
        rem = key - x * nn
        y, z = divmod(rem, n)

        linear[x] += w

        pair[x][y] -= w
        pair[y][x] -= w
        pair[x][z] -= w
        pair[z][x] -= w

        triple[x][y][z] += w
        triple[x][z][y] += w
        triple[y][x][z] += w
        triple[y][z][x] += w
        triple[z][x][y] += w
        triple[z][y][x] += w

    size = 1 << n
    scores = [0] * size

    # Fast conversion from a power of two to its bit index.
    bit_index = [0] * size
    for i in range(n):
        bit_index[1 << i] = i

    # Process masks according to their least significant set bit.
    # If v is that bit, all other set bits are above v. Scores of
    # masks containing only those higher bits have already been computed.
    for v in range(n - 1, -1, -1):
        base = v + 1
        m = n - base
        subsets = 1 << m

        # delta[mask] contains all pair and triple terms involving v
        # and cells in mask, excluding the linear term.
        delta = [0] * subsets
        pair_v = pair[v]
        triple_v = triple[v]
        linear_v = linear[v]
        v_bit = 1 << v

        scores[v_bit] = linear_v

        for mask in range(1, subsets):
            bit = mask & -mask
            u = base + bit_index[bit]
            rest = mask ^ bit

            value = delta[rest] + pair_v[u]
            row = triple_v[u]

            rr = rest
            while rr:
                b = rr & -rr
                value += row[base + bit_index[b]]
                rr ^= b

            delta[mask] = value

            higher_mask = mask << base
            scores[higher_mask | v_bit] = (
                scores[higher_mask] + linear_v + value
            )

    best = max(scores)
    boards = scores.count(best)
    sys.stdout.write(f"{best} {boards}")


if __name__ == "__main__":
    main()