题目 › usaco-1573

Cow Circle

USACO 2026 Second · 铂金组 · 2026-01 · L4 顶尖 · C++17 · 时限 6 秒 · 测试点等分

查看原题跳到结果

题面

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

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

Farmer John has $N$ ($1 \leq N \leq 5000$) cows standing around a circular track divided into $M$ ($1 \leq M \leq 10^6$) equally spaced positions, numbered $0$ to $M-1$ clockwise. Cow $i$ is initially at position $x_i$, where $0 = x_1 < x_2 < \dots < x_N < M$.

For each $1 \leq i \leq N$, cow $i$ will independently and randomly choose either to face clockwise or counterclockwise with some probability specific to that cow. Once a cow has chosen her initial direction, she begins moving continuously in that direction at a constant speed of one position per minute. Whenever two cows meet (i.e., they occupy the same space), they bounce off of each other: immediately reversing their directions and continuing to move at the same speed in that direction.

Farmer John is wondering where cow $1$ will end up. For each $0 \leq i < M$, find the probability that cow $1$ is at position $i$ after $K$ ($1 \leq K \leq 10^{18}$) minutes.

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

The first line contains $T$ ($1 \leq T \leq 100$), the number of independent test cases. Each test case is specified as follows:

The first line of each test case contains $N$ ($1 \leq N \leq 5000$), $M$ ($1 \leq M \leq 10^6$), and $K$ ($1 \leq K \leq 10^{18}$).

The second line contains $N$ integers $p_1, \dots, p_N$ ($0 \leq p_i < 10^9 + 7$) where if $\frac{a_i}{b_i}$ is the probability cow $i$ goes clockwise, then $p_i \cdot b_i \equiv a_i \pmod{10^9+7}$.

The third and final line contains $N$ integers $x_1, x_2, \dots, x_N$.

It is guaranteed that the sum of $N^2$ over all test cases is $\leq 5000^2$ and the sum of $M$ over all testcases is $\leq 10^6$.

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

Output a new line for each test case. The line for each test case should be formatted as follows:

For every $0 \leq i < M$, let $\frac{p_i}{q_i}$ be the probability cow $1$ is in position $i$ at the end of $K$ minutes. Output $M$ space-separated integers $p_iq_i^{-1} \pmod{10^9 + 7}$ (where $p_iq_i^{-1} \cdot q_i \equiv p_i \pmod{10^9+7}$).

SAMPLE INPUT:

3
2 2 1
500000004 500000004 
0 1
3 3 1
500000004 500000004 500000004
0 1 2
5 10 13
500000004 1 500000004 0 500000004
0 3 4 7 9

SAMPLE OUTPUT:

500000004 500000004
500000004 250000002 250000002
0 0 0 125000001 375000003 0 125000001 375000003 0 0

For the first test case, both cows have a $\frac{1}{2}$ chance of going in either direction. If both pick the same direction, they will end up swapping positions (so cow $1$ ends up at $1$). Otherwise, they will bounce off in the middle and return to their original positions. Therefore, there is a $\frac{1}{2}$ chance for cow $1$ to end up at $0$ and a $\frac{1}{2}$ chance for cow $1$ to end up at $1$.

For the second test case, all cows again have a $\frac{1}{2}$ chance of going in either direction. For each combination of directions, here is where cow $1$ ends up at.

  • CW, CW, CW: $1$
  • CW, CW, CCW: $1$
  • CCW, CCW, CCW: $2$
  • CCW, CW, CCW: $2$
  • CW, CCW, CW: $0$
  • CW, CCW, CCW: $0$
  • CCW, CW, CW: $0$
  • CCW, CCW, CW: $0$

SCORING:

  • Input 2: $K \leq 100, N \leq 10$.
  • Input 3: $N \leq 10$.
  • Inputs 4-7: $\sum N^3 \leq 500^3$.
  • Inputs 8-11: $K < \frac{M}{2}$.
  • Inputs 12-15: No additional constraints.

Problem credits: Sujay Konda

结果

Kimi K3.1 ProKimi

max

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 105.1k平均输出 token(含思考) ↓
  • 23.0输出速度 tokens/s ↑
  • 4446 秒延迟 ↓
  • 1.17×CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

Kimi K3.1 Pro(high)Kimi high

high

0/3

解出的采样 ↑

  • 0.0采样 1 · 答案错误
  • 0.0采样 2 · 答案错误
  • 0.0采样 3 · 答案错误
  • 60.5k平均输出 token(含思考) ↓
  • 23.1输出速度 tokens/s ↑
  • 2679 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • —时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

2/3

解出的采样 ↑

  • 0.0采样 1 · 答案错误
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 83.5k平均输出 token(含思考) ↓
  • 164.1输出速度 tokens/s ↑
  • 526 秒延迟 ↓
  • 2.04×CPU 时间 / 官方解 ↓
  • 同阶 2时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

1/3

解出的采样 ↑

  • 0.0采样 1 · 生成超时
  • 100采样 2 · 解出
  • 0.0采样 3 · 答案错误
  • 63.0k平均输出 token(含思考) ↓
  • 115.7输出速度 tokens/s ↑
  • 1099 秒延迟 ↓
  • 1.22×CPU 时间 / 官方解 ↓
  • 同阶 1时间复杂度(相对官方解)

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

代码

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

using ll = long long;
const int MOD = 1000000007;

int modpow(int a, int e = MOD - 2) {
    ll r = 1, b = a;
    while (e) {
        if (e & 1) r = r * b % MOD;
        b = b * b % MOD;
        e >>= 1;
    }
    return (int)r;
}

struct Appearance {
    int pos, cow, dir;
};

struct Candidate {
    int pos = 0;
    int g = 0;
    int cow[2] = {0, 0};
    int mask[2] = {0, 0};
};

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

    int T;
    cin >> T;
    while (T--) {
        int N, M;
        ll K;
        cin >> N >> M >> K;

        vector<int> p(N), x(N), ccw(N), invCCW(N, 0);
        for (int &v : p) cin >> v;
        for (int &v : x) cin >> v;

        vector<int> answer(M, 0);
        auto printAnswer = [&]() {
            for (int i = 0; i < M; i++) {
                if (i) cout << ' ';
                cout << answer[i];
            }
            cout << '\n';
        };

        ll q = K / M;
        int r = (int)(K % M);

        for (int i = 0; i < N; i++) {
            ccw[i] = 1 - p[i];
            if (ccw[i] < 0) ccw[i] += MOD;
            if (ccw[i]) invCCW[i] = modpow(ccw[i]);
        }

        if (N == 1) {
            int a = (x[0] + r) % M;
            int b = x[0] - r;
            if (b < 0) b += M;
            answer[a] = (answer[a] + p[0]) % MOD;
            answer[b] = (answer[b] + ccw[0]) % MOD;
            printAnswer();
            continue;
        }

        int twoQ = (int)(2 * (q % N) % N);

        // Multiply a polynomial by ccw[i] + p[i]*y.  The vector's size
        // always equals (number of factors + 1), including trailing zeroes.
        auto multiplyPoly = [&](vector<int> &P, int i) {
            int d = (int)P.size() - 1;
            int a = ccw[i], b = p[i];
            P.resize(d + 2, 0);

            if (b == 0) {
                P[d + 1] = 0;
                return;
            }
            if (a == 0) {
                for (int k = d + 1; k >= 1; k--) P[k] = P[k - 1];
                P[0] = 0;
                return;
            }

            P[d + 1] = (int)((ll)b * P[d] % MOD);
            for (int k = d; k >= 1; k--) {
                P[k] = ((ll)a * P[k] + (ll)b * P[k - 1]) % MOD;
            }
            P[0] = (int)((ll)a * P[0] % MOD);
        };

        // Exact division by ccw[i] + p[i]*y.
        auto dividePoly = [&](vector<int> &P, int i) {
            int newSize = (int)P.size() - 1;
            int a = ccw[i], b = p[i];

            if (a == 0) {
                for (int k = 0; k < newSize; k++) P[k] = P[k + 1];
                P.pop_back();
                return;
            }
            if (b == 0) {
                P.pop_back();
                return;
            }

            int inv = invCCW[i];
            for (int k = 0; k < newSize; k++) {
                int cur = P[k];
                if (k) {
                    int sub = (int)((ll)b * P[k - 1] % MOD);
                    cur -= sub;
                    if (cur < 0) cur += MOD;
                }
                P[k] = (int)((ll)cur * inv % MOD);
            }
            P.pop_back();
        };

        // If K is a whole number of laps, only the total number of
        // clockwise choices is needed.
        if (r == 0) {
            vector<int> dp(1, 1);
            for (int i = 0; i < N; i++) multiplyPoly(dp, i);

            for (int s = 0; s <= N; s++) {
                int rank = (int)((ll)twoQ * s % N);
                answer[x[rank]] = (answer[x[rank]] + dp[s]) % MOD;
            }
            printAnswer();
            continue;
        }

        int H = M - r;

        vector<Appearance> apps;
        apps.reserve(2 * N);
        for (int i = 0; i < N; i++) {
            int a = x[i] + r;
            if (a >= M) a -= M;
            int b = x[i] - r;
            if (b < 0) b += M;
            apps.push_back({a, i, 1}); // clockwise
            apps.push_back({b, i, 2}); // counterclockwise
        }
        sort(apps.begin(), apps.end(),
             [](const Appearance &a, const Appearance &b) {
                 return a.pos < b.pos;
             });

        vector<Candidate> candidates;
        for (int i = 0; i < (int)apps.size();) {
            int j = i;
            Candidate c;
            c.pos = apps[i].pos;

            while (j < (int)apps.size() && apps[j].pos == c.pos) {
                int cow = apps[j].cow;
                int k = 0;
                while (k < c.g && c.cow[k] != cow) k++;
                if (k == c.g) {
                    c.cow[k] = cow;
                    c.mask[k] = 0;
                    c.g++;
                }
                c.mask[k] |= apps[j].dir;
                j++;
            }
            candidates.push_back(c);
            i = j;
        }

        vector<int> regionIds[4];
        for (int i = 0; i < (int)candidates.size(); i++) {
            int v = candidates[i].pos;
            int reg;
            if (v < r && v < H) reg = 0;
            else if (v >= r && v < H) reg = 1;
            else if (v < r) reg = 2;
            else reg = 3;
            regionIds[reg].push_back(i);
        }

        vector<int> mark(N, 0), outResidue(N, 0);
        int stamp = 0;

        vector<int> Pin, Pout, tmpIn, tmpOut;
        vector<pair<int, int>> inTerms;

        for (int reg = 0; reg < 4; reg++) {
            if (regionIds[reg].empty()) continue;

            int e0, delta;
            if (reg == 0) {
                e0 = -1;
                delta = (H > r ? 1 : -1);
            } else if (reg == 1) {
                e0 = 0;
                delta = -1;
            } else if (reg == 2) {
                e0 = -2;
                delta = 1;
            } else {
                e0 = -1;
                delta = (H > r ? 1 : -1);
            }

            auto normN = [&](int v) {
                v %= N;
                if (v < 0) v += N;
                return v;
            };

            int aOut = normN(e0 - twoQ);
            int aIn = normN(aOut + delta);

            vector<int> resIn(N + 1), resOut(N + 1);
            for (int i = 0; i <= N; i++) {
                resIn[i] = (int)((ll)aIn * i % N);
                resOut[i] = (int)((ll)aOut * i % N);
            }

            bool first = true;
            int L = 0, R = 0;

            for (int id : regionIds[reg]) {
                const Candidate &cand = candidates[id];
                int v = cand.pos;

                int leftPos, rightPos, betaCount;

                if (reg == 0) {
                    int A = H + v;
                    int B = r + v;
                    int bi = (int)(lower_bound(x.begin(), x.end(), B) - x.begin());
                    betaCount = bi;
                    if (A > B) {
                        leftPos = B;
                        rightPos = A;
                    } else {
                        leftPos = A;
                        rightPos = B;
                    }
                } else if (reg == 1) {
                    int A = v - r;
                    int B = r + v;
                    leftPos = A;
                    rightPos = B;
                    betaCount = (int)(lower_bound(x.begin(), x.end(), B) - x.begin());
                } else if (reg == 2) {
                    int e = v - H;
                    int A = H + v;
                    leftPos = e;
                    rightPos = A;
                    betaCount = N + (int)(lower_bound(x.begin(), x.end(), e) - x.begin());
                } else {
                    int A = v - r;
                    int e = v - H;
                    if (A > e) {
                        leftPos = e;
                        rightPos = A;
                    } else {
                        leftPos = A;
                        rightPos = e;
                    }
                    betaCount = N + (int)(lower_bound(x.begin(), x.end(), e) - x.begin());
                }

                int newL = (int)(lower_bound(x.begin(), x.end(), leftPos) - x.begin());
                int newR = (int)(lower_bound(x.begin(), x.end(), rightPos) - x.begin());

                if (first) {
                    Pin.assign(1, 1);
                    Pout.assign(1, 1);
                    for (int i = 0; i < N; i++) {
                        if (newL <= i && i < newR) multiplyPoly(Pin, i);
                        else multiplyPoly(Pout, i);
                    }
                    L = newL;
                    R = newR;
                    first = false;
                } else {
                    int leaveEnd = min(R, newL);
                    for (int i = L; i < leaveEnd; i++) {
                        dividePoly(Pin, i);
                        multiplyPoly(Pout, i);
                    }

                    int enterStart = max(R, newL);
                    for (int i = enterStart; i < newR; i++) {
                        dividePoly(Pout, i);
                        multiplyPoly(Pin, i);
                    }

                    L = newL;
                    R = newR;
                }

                bool inside[2];
                bool needIn = false, needOut = false;
                for (int k = 0; k < cand.g; k++) {
                    inside[k] = (L <= cand.cow[k] && cand.cow[k] < R);
                    if (inside[k]) needIn = true;
                    else needOut = true;
                }

                const vector<int> *inPtr = &Pin;
                const vector<int> *outPtr = &Pout;

                if (needIn) {
                    tmpIn = Pin;
                    inPtr = &tmpIn;
                }
                if (needOut) {
                    tmpOut = Pout;
                    outPtr = &tmpOut;
                }

                for (int k = 0; k < cand.g; k++) {
                    if (inside[k]) dividePoly(tmpIn, cand.cow[k]);
                    else dividePoly(tmpOut, cand.cow[k]);
                }

                ++stamp;
                for (int t = 0; t < (int)outPtr->size(); t++) {
                    int coef = (*outPtr)[t];
                    if (!coef) continue;
                    int rr = resOut[t];
                    if (mark[rr] != stamp) {
                        mark[rr] = stamp;
                        outResidue[rr] = coef;
                    } else {
                        int nv = outResidue[rr] + coef;
                        if (nv >= MOD) nv -= MOD;
                        outResidue[rr] = nv;
                    }
                }

                inTerms.clear();
                for (int s = 0; s < (int)inPtr->size(); s++) {
                    int coef = (*inPtr)[s];
                    if (coef) inTerms.push_back({s, coef});
                }

                auto calculate = [&](int need) -> int {
                    __int128 sum = 0;
                    for (auto [s, coef] : inTerms) {
                        int rr = need - resIn[s];
                        if (rr < 0) rr += N;
                        if (mark[rr] == stamp && outResidue[rr]) {
                            sum += (ll)coef * outResidue[rr];
                        }
                    }
                    return (int)(sum % MOD);
                };

                int betaMod = betaCount % N;

                for (int bits = 0; bits < (1 << cand.g); bits++) {
                    int pw = 1;
                    int atHere = 0;
                    int fixedIn = 0, fixedOut = 0;

                    for (int k = 0; k < cand.g; k++) {
                        int cow = cand.cow[k];
                        int dir = (bits >> k) & 1; // 1 = clockwise

                        pw = (int)((ll)pw * (dir ? p[cow] : ccw[cow]) % MOD);

                        bool present = dir ? (cand.mask[k] & 1) : (cand.mask[k] & 2);
                        if (present) atHere++;

                        if (dir) {
                            if (inside[k]) fixedIn++;
                            else fixedOut++;
                        }
                    }

                    if (atHere == 0 || pw == 0) continue;

                    int base = betaMod + resIn[fixedIn];
                    base %= N;
                    base += resOut[fixedOut];
                    base %= N;

                    int need0 = (base == 0 ? 0 : N - base);
                    int prob = calculate(need0);

                    if (atHere == 2) {
                        int need1 = (N - 1) - base;
                        if (need1 < 0) need1 += N;
                        int second = calculate(need1);
                        prob += second;
                        if (prob >= MOD) prob -= MOD;
                    }

                    answer[v] = (answer[v] + (ll)pw * prob) % MOD;
                }
            }
        }

        printAnswer();
    }

    return 0;
}