题目链接:https://ac.nowcoder.com/acm/contest/4853/D
思路:
#include <bits/stdc++.h> using namespace std; #define ll long long ll extend_gcd(ll a,ll b,ll&x,ll&y) { if(!b) { x = 1; y = 0; return a; } ll xt = 0, yt = 0; ll d = extend_gcd(b, a % b, xt, yt); x = yt; y = xt - yt * (a/b); return d;//gcd } ll minx(ll a,ll b,ll c)//x=(x%b+b)%b;(求出的就是最小正整数解) { ll x,y; ll gcd=extend_gcd(a,b,x,y); if(c%gcd!=0) return -1; x*=c/gcd;//转化为a*x+b*y=c的解 b/=gcd;//约去c后原来b就变为了b/gcd; if(b<0) b=-b;//如果b为负数就去绝对值 ll ans=x%b; if(ans<=0) ans+=b;//求最小正整数解 return ans;//返回的就是最小正整数解 } int main(){ ll a, b, c, k; ll x, y, z, w; cin>>a>>b>>c>>k; ll d=__gcd(b, c); x=minx(a, d, k); w=(k-x*a)/d; b/=d; c/=d; y=minx(b, c, w); z=(w-y*b)/c; cout<<x<<" "<<y<<" "<<z<<endl; return 0; }