扩展欧几里得\exgcd

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll N = 1e6 + 9;
const ll inf = 0x3f3f3f3f;
ll exgcd(ll a, ll b, ll &x, ll &y) {
    if (!b) {
        x = 1;
        y = 0;
        return a;
    }
    ll g = exgcd(b, a%b, x, y);
    ll x1 = x;
    x = y;
    y = (x1 - a/b * y);
    return g;
}
void solve() {
    ll a, b, c;
    ll x, y;
    scanf("%lld%lld%lld", &a, &b, &c);
    ll g = exgcd(a, b,x, y);
    ll x1 = x*c/g;
    ll y1 = y*c/g;
    ll dx = b/g;
    ll dy = a/g;
    ll l = ceil((double)(-x1+1)/dx);
    ll r = floor((double)(y1-1)/dy);
    if (c % g != 0) {
        puts("-1");
    } else {
		/*
若该行对应的询问有整数解但无正整数解,包含 2 个由空格隔开的数字,
依次代表整数解中,x 的最小正整数值,y 的最小正整数值。
否则包含 5 个由空格隔开的数字,依次代表正整数解的数量,正整数解中,
x 的最小值,y 的最小值,x 的最大值,y 的最大值。
		*/
        if (l <= r) {
            ll minx = x1 + l * dx;
            ll maxx = x1 + r * dx;
            ll maxy = y1 - l * dy;
            ll miny = y1 - r * dy;
            ll cnt = r-l + 1;
            printf("%lld %lld %lld %lld %lld\n", cnt, minx, miny, maxx, maxy);
        } else 
            printf("%lld %lld\n", x1 + l * dx, y1 - r * dy);
    }
}
signed main() {
   ll t = 1;scanf("%lld", &t);
   while (t--) {
      solve();
   }
}

posted @ 2021-08-11 11:16  u_yan  阅读(29)  评论(0编辑  收藏  举报