题目 › usaco-1521

Moo Decomposition

USACO 2025 US Open · 金组 · 2025-03 · L3 困难 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

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

You have a long string $S$ of Ms and Os and an integer $K \geq 1$. Count the number of ways of ways to decompose $S$ into subsequences such that each subsequence is MOOOO....O with exactly $K$ Os, modulo $10^9+7$.

Since the string is very long, you are not given it explicitly. Instead, you are given an integer $L$ ($1 \leq L \leq 10^{18}$), and a string $T$ of length $N$ ($1 \leq N \leq 10^6$). The string $S$ is the concatenation of $L$ copies of the string $T$.

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

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

The second line contains the string $T$ of length $N$. Every character is either an M or an O.

It is guaranteed that the number of decompositions of $S$ is nonzero.

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

Output the number of decompositions of string $S$, modulo $10^9+7$.

SAMPLE INPUT:

2 6 1
MOOMOO

SAMPLE OUTPUT:

1

The only way to decompose $S$ into MOOs is to let the first three characters form a MOO and the last three characters form another MOO.

SAMPLE INPUT:

2 6 1
MMOOOO

SAMPLE OUTPUT:

6

There are six distinct ways to decompose the string into subsequences (uppercase letters form one MOO, lowercase letters form another):

  • MmOOoo
  • MmOoOo
  • MmOooO
  • MmoOOo
  • MmoOoO
  • MmooOO

SAMPLE INPUT:

1 4 2
MMOO

SAMPLE OUTPUT:

4

SAMPLE INPUT:

1 4 100
MMOO

SAMPLE OUTPUT:

976371285

Make sure to take the answer modulo $10^9+7$.

SCORING:

  • Inputs 5-7: $K=1$, $L = 1$
  • Inputs 8-10: $K=2$, $N\leq 1000$, $L = 1$
  • Inputs 11-13: $K=1$
  • Inputs 14-19: $L = 1$
  • Inputs 20-25: No additional constraints.

Problem credits: Dhruv Rohatgi

结果

Kimi K3.1 ProKimi

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 7.5k平均输出 token(含思考) ↓
  • 21.4输出速度 tokens/s ↑
  • 292 秒延迟 ↓
  • 0.08×CPU 时间 / 官方解 ↓
  • 不确定 3时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 13.6k平均输出 token(含思考) ↓
  • 192.2输出速度 tokens/s ↑
  • 69 秒延迟 ↓
  • 0.05×CPU 时间 / 官方解 ↓
  • 不确定 3时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 14.8k平均输出 token(含思考) ↓
  • 165.1输出速度 tokens/s ↑
  • 82 秒延迟 ↓
  • 0.06×CPU 时间 / 官方解 ↓
  • 不确定 3时间复杂度(相对官方解)

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

代码

Kimi K3.1 Pro · 采样 1 · 45 行
#include <bits/stdc++.h>
using namespace std;

static const long long MOD = 1000000007LL;

long long modpow(long long a, long long e) {
    long long r = 1;
    while (e > 0) {
        if (e & 1) r = r * a % MOD;
        a = a * a % MOD;
        e >>= 1;
    }
    return r;
}

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

    long long K, N, L;
    string T;
    cin >> K >> N >> L >> T;

    long long fact = 1;
    for (long long i = 2; i <= K; ++i) {
        fact = fact * i % MOD;
    }
    long long invFact = modpow(fact, MOD - 2);

    long long coefficient = 1;
    long long degree = 0;

    for (char c : T) {
        if (c == 'M') {
            coefficient = coefficient * invFact % MOD;
            degree += K;
        } else {
            coefficient = coefficient * (degree % MOD) % MOD;
            --degree;
        }
    }

    cout << modpow(coefficient, L) << '\n';
    return 0;
}