题目 › usaco-1501

Transforming Pairs

USACO 2025 February · 铂金组 · 2025-02 · L4 顶尖 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

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

Answer $Q$ ($1\le Q\le 10^5$) independent queries each of the following form:

You are given four integers $a,b,c,d$ ($-10^{18}\le a,b,c,d\le 10^{18}$). In one operation you can either do $a\mathrel{+}=b$, or $b\mathrel{+}=a$. Determine the minimum number of operations to transform $(a,b)$ into $(c,d)$, or if it is impossible to do so, output $-1$.

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

The first line contains $Q$.

The next $Q$ lines each contain four integers $a,b,c,d$.

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

The answer for each query on a separate line.

SAMPLE INPUT:

4
5 -3 -1 -3
5 3 5 2
5 3 8 19
5 3 5 3

SAMPLE OUTPUT:

2
-1
3
0

First query: $(5,-3)\to (2,-3)\to (-1,-3)$

Second query: Impossible.

Third query: $(5,3) \to (8, 3) \to (8, 11) \to (8, 19)$

Fourth query: No operations necessary.

SCORING:

  • Input 2: $|a|, |b|, |c|,|d|\le 10$
  • Input 3: $a,b\ge 0$
  • Input 4: $a \geq 0 \geq b$
  • Input 5: $a \leq 0 \leq b$
  • Input 6: $a,b\le 0$
  • Input 7: $c,d\ge 0$
  • Input 8: $c \geq 0 \geq d$
  • Input 9: $c \leq 0 \leq d$
  • Input 10: $c,d\le 0$
  • Inputs 11-14: $Q \leq 10^3$
  • Inputs 15-19: No additional constraints.

Problem credits: Benjamin Qi

结果

Kimi K3.1 ProKimi

max

3/3

解出的采样 ↑

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

Kimi K3.1 Pro(high)Kimi high

high

2/3

解出的采样 ↑

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

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

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

MiniMax M3.1 FlashMiniMax

auto(max)

3/3

解出的采样 ↑

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

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

代码

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

using ll = long long;
using i128 = __int128_t;

static inline ll absll(ll x) { return x >= 0 ? x : -x; }

// Coefficients for abs(A), abs(B), then adjusted for their signs.
static pair<i128, i128> bezout(ll A, ll B) {
    ll r0 = absll(A), r1 = absll(B);
    i128 x0 = 1, x1 = 0, y0 = 0, y1 = 1;

    while (r1 != 0) {
        ll q = r0 / r1;
        ll nr = r0 % r1;
        r0 = r1;
        r1 = nr;

        i128 nx = x0 - (i128)q * x1;
        i128 ny = y0 - (i128)q * y1;
        x0 = x1; x1 = nx;
        y0 = y1; y1 = ny;
    }

    if (A < 0) x0 = -x0;
    if (B < 0) y0 = -y0;
    return {x0, y0};
}

// b is positive.
static i128 floorDiv(i128 a, i128 b) {
    i128 q = a / b;
    if (a % b < 0) --q;
    return q;
}

// b is positive.
static i128 ceilDiv(i128 a, i128 b) {
    i128 q = a / b;
    if (a % b > 0) ++q;
    return q;
}

static string toString(i128 x) {
    if (x == 0) return "0";
    bool neg = x < 0;
    if (neg) x = -x;
    string s;
    while (x > 0) {
        s.push_back(char('0' + int(x % 10)));
        x /= 10;
    }
    if (neg) s.push_back('-');
    reverse(s.begin(), s.end());
    return s;
}

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

    int Q;
    cin >> Q;

    while (Q--) {
        ll a, b, c, d;
        cin >> a >> b >> c >> d;

        if (a == c && b == d) {
            cout << "0\n";
            continue;
        }
        if (a == 0 && b == 0) {
            cout << "-1\n";
            continue;
        }

        ll g = std::gcd(absll(a), absll(b));
        if (std::gcd(absll(c), absll(d)) != g) {
            cout << "-1\n";
            continue;
        }

        // Reduce to primitive vectors.
        ll A = a / g, B = b / g;
        ll C = c / g, D = d / g;

        auto [u, v] = bezout(A, B); // u*A + v*B = 1
        auto [z, t] = bezout(C, D); // z*C + t*D = 1

        /*
           Every integer determinant-one matrix taking (A,B) to (C,D)
           has the form

               M_k = Q * L^k * P^(-1),

           where L = [[1,1],[0,1]].  Its entries are affine in k.
           We need exactly those k for which all entries are nonnegative;
           such a matrix, and only such a matrix, is a product of the two
           operation matrices.
        */

        i128 p0 = (i128)C * u + (i128)t * B;
        i128 q0 = (i128)C * v - (i128)t * A;
        i128 r0 = (i128)D * u - (i128)z * B;
        i128 s0 = (i128)D * v + (i128)z * A;

        i128 cp = -(i128)C * B;
        i128 cq =  (i128)C * A;
        i128 cr = -(i128)D * B;
        i128 cs =  (i128)D * A;

        bool ok = true, hasLo = false, hasHi = false;
        i128 lo = 0, hi = 0;

        auto addConstraint = [&](i128 base, i128 coef) {
            if (!ok) return;
            if (coef == 0) {
                if (base < 0) ok = false;
            } else if (coef > 0) {
                i128 bound = ceilDiv(-base, coef);
                if (!hasLo || bound > lo) {
                    lo = bound;
                    hasLo = true;
                }
            } else {
                i128 bound = floorDiv(base, -coef);
                if (!hasHi || bound < hi) {
                    hi = bound;
                    hasHi = true;
                }
            }
        };

        addConstraint(p0, cp);
        addConstraint(q0, cq);
        addConstraint(r0, cr);
        addConstraint(s0, cs);

        if (!ok || (hasLo && hasHi && lo > hi)) {
            cout << "-1\n";
            continue;
        }

        // Factor a nonnegative SL(2,Z) matrix into the two operation
        // matrices, batching consecutive identical factors.
        auto wordLength = [&](i128 k) -> i128 {
            i128 p = p0 + cp * k;
            i128 q = q0 + cq * k;
            i128 r = r0 + cr * k;
            i128 s = s0 + cs * k;

            if (p < 0 || q < 0 || r < 0 || s < 0) return -1;

            i128 ans = 0;
            while (true) {
                if (q == 0) { // R^r
                    ans += r;
                    break;
                }
                if (r == 0) { // L^q
                    ans += q;
                    break;
                }

                i128 n;
                if (p >= r && q >= s) {
                    // The last factors are L: subtract the second row
                    // from the first row.
                    n = std::min(p / r, q / s);
                    if (n <= 0) return -1;
                    p -= n * r;
                    q -= n * s;
                } else {
                    // The last factors are R: subtract the first row
                    // from the second row.
                    n = std::min(r / p, s / q);
                    if (n <= 0) return -1;
                    r -= n * p;
                    s -= n * q;
                }
                ans += n;
            }
            return ans;
        };

        i128 best = -1;
        auto tryK = [&](i128 k) {
            i128 len = wordLength(k);
            if (len >= 0 && (best < 0 || len < best)) best = len;
        };

        // The shortest representation occurs at an endpoint of the
        // feasible interval.  Normally there is only one finite endpoint.
        if (hasLo) tryK(lo);
        if (hasHi && (!hasLo || hi != lo)) tryK(hi);
        if (!hasLo && !hasHi) tryK(0);

        if (best < 0) cout << "-1\n";
        else cout << toString(best) << '\n';
    }

    return 0;
}