First we move the item and force it to a>b.
And then we can draw an image like this.
We found that the absolute value of x, Y is incremented at the intersection of the segment L and the x-axis, so we don't consider the minimum point at the bottom.
Then we find at the top of the point, because the slope is less than the -1,x of the reduction far from the Y plus the fast, so we know the pole at the intersection of the x-axis and L.
But the point is not necessarily the hour.
So we just have to find the top and the last two on the whole point.
So we ask ax+by=c the smallest positive integer solution y, then call x, then y minus A, then X, compare two times min (|x|+|y|), you can come to an answer.
Of course, if the first y=0, the answer is it.
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cmath>5 6 #definell Long Long7 8 using namespacestd;9 Ten ll gcd (ll A,ll b) One { A returnb==0? A:GCD (b,a%b); - } - the ll x, y; - - voidEXGCD (ll n,ll m) - { + if(m==0) {x=1, y=0;return;} -EXGCD (m,n%m); ll t=x; +x=y;y=t-n/m*y; A } at - intMain () - { - ll A,b,d; -scanf"%lld%lld",&a,&b); -ll gd=gcd (A, b); inA/=gd,b/=GD; - EXGCD (A, b); to while(~SCANF ("%lld",&d)) + { - if(D%GD) {printf ("beiju!\n");Continue;} theD/=GD; *ll ans1= (y*d%a+a)%A,ans; $Ans=abs (ANS1) +abs ((d-ans1*b)/a);Panax Notoginseng if(!ANS1) {printf ("%lld\n", ans);return 0;} -ans1-=A; theAns=min (Ans,abs (ANS1) +abs ((d-ans1*b)/a)); +printf"%lld\n", ans); A } the return 0; +}
View Code
Code slightly ugly. Give a B, give a bunch of C, ask Min (|x|+|y|).
About Ax+by=c's solution x, y, min (|x|+|y|) Value problem