About Ax+by=c's solution x, y, min (|x|+|y|) Value problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.