NOIP simulation tests 7

Test burst, burst zero, GG.

T1: solution of the equation

A Exgcd, the value of minimum solution, and then find all solutions.

However Exgcd forget oriented programming data range, Japanese sentence less, 40 minutes rolling thick.

This question of sentence to be special too much. . .

  1. Sentenced to pay attention to special 0
  2. Sentenced to pay attention to special symbols
  3. Special attention should judge whether the positive integer solution

In general this is three, on the issue of symbols, Exgcd certainly be positive, it can put a negative mark, taking opposite number, and then change back Exgcd.

About Exgcd, it is necessary to prove it again. . .

Known indeterminate equation \ (ax + by = c \ ) necessary conditions, first by a not remember the name of the reasoning, it solvability of \ ((A, b) | c \) .

We set \ (G = (A, b) \) , to solve the \ (AX + by = G \) .

因为\((a, b) = (b, a % b), a % b = a - b * \lfloor \frac{a}{b} \rfloor\),所以\(bx + (a - b * \lfloor \frac{a}{b} \rfloor)y = g\).即:\(ay + b(x - \lfloor \frac{a}{b} \rfloor y) = g\).

A set of solutions can be obtained original equation \ (x_1 = Y, X Y_1 = - \ lfloor \ FRAC {A} {B} \ rfloor Y \) .

And \ (C = kg \) , enabling the Indefinite sides of the equation we passenger \ (K \) to give \ (ax_0 by_0 = C + \) , where \ (= kx_1 x_0, y_0 ky_1 = \) .

This is a particular solution set of the original equation. To obtain the same solution, we let \ (the X-+ \ Delta the X-, the y-+ \ Delta the y-\) . \ (A (x_0 + \ Delta the X-) + b (y_0 + \ Delta the y-) = c \) . To make the equation balance, \ (A \ B of Delta X = \ of Delta Y \) .

Therefore, \ (\ of Delta X = \ FRAC {B} {G}, \ of Delta Y = \ FRAC} {A} {G \) .

With the original solution for the equation $ x = x_0 + t \ frac {b} {g}, y = y_0 - t \ frac {a} {g}, t \ in Z $.

Just greater than y 0 is obtained, and obtains just greater than the maximum y x 0 corresponding to the step difference by \ (\ frac {a} { g} \) then +1 is the answer.

This question is too difficult to adjust, \ (Have \ Fun \ the debugging! \) (Escaped

#include <bits/stdc++.h>
#define ll long long

ll Exgcd(ll a, ll b, ll &x, ll &y) {
    if (b == 0) {
        x = 1, y = 0; return a;
    }
    ll r = Exgcd(b, a % b, y, x);
    y -= (a / b) * x;
    return r;
}

ll Solve(ll a, ll b, ll c) {
    bool opa = 0, opb = 0;
    if (a == 0 && b == 0) return c == 0 ? -1 : 0;
    if (a == 0) return (c == 0 || (c % b == 0 && c / b > 0)) ? -1 : 0;
    if (b == 0) return (c == 0 || (c % a == 0 && c / a > 0)) ? -1 : 0;
    if (c < 0) a = -a, b = - b, c = -c;
    if (a < 0) a = -a, opa = 1;
    if (b < 0) b = -b, opb = 1;
    ll x, y;
    ll g = Exgcd(a, b, x, y);
    if (c % g != 0) return 0;
    ll t = c / g;       x *= t, y *= t, a /= g, b /= g, c = t;
    if (opa) a = -a, x = -x;
    if (opb) b = -b, y = -y;
    if (a < 0) a = -a, b = -b, c = -c;
    if (a * b < 0) return -1;
    x %= b;
    while (x <= 0) x += b;
    y = (c - a * x) / b;
    if (y < 0) return 0;
    ll y_min = y % a;
    while (y_min <= 0) y_min += a;
    return y_min <= y ? (y - y_min) / a + 1 : 0;
}

signed main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        ll a, b, c;
        scanf("%lld%lld%lld", &a, &b, &c);
        ll ans = Solve(a, b, c);
        if (ans == -1 || ans > 65535) puts("ZenMeZheMeDuo");
        else printf("%lld\n", ans);
    }
}

Guess you like

Origin www.cnblogs.com/gekoo/p/11227861.html