Extended Euclid HDU 1576

Source: Internet
Author: User

Test instructions (A/b)%9973, but because a is very large, we only give N (n=a%9973) (we given a must be divisible by B, and gcd (b,9973) = 1).

because: a%9973=n;

So: 9973*y+n=a:

Set: A/b=x; (divisible)

So: 9973*y+n=b*x;

So: b*x-9973*y=n; ① type

Also because: gcd (b,9973) = 1;

So there must be a X1*b+9973*y1=1;② type

② type *n=① Type

So just ask for a x1, you can get X, and because x=a/b, as long as the MoD 9973 is the answer.

Now give the extended Euclid:

For GCD (A, a, b) = d, the Euclidean-to-German division of (A, c) will eventually get (d, 0). At this point, for a =d, B = 0 into a*x + b*y = d, it is obvious that x = 1,y can be any value. We can use a = d, b = 0 To reverse the introduction of any GCD (A, b) = d satisfies the solution of a*x + B*y = d. If X0,y0 is the solution of B*x + (a%b) *y = d, then what is the solution for a*x + b*y = d?

B*x + (a%b) *y = D→b*x + (A-[a/b]*b) *y = d→a*y + b* (x-[a/b]*y) = d so a*x + B*y = D's solution x1 = y0,y1= x0-[a/b]*y0;

We can launch A*X+B*Y=GCD (A, B), in the lower, X and Y. Now all the problems are solved.

#include <stdio.h>
#include <cstring>
#include <iostream>
#define LL Long Long
using namespace Std;     
void Ex_gcd (ll a,ll b,ll &x,ll &y)//Extended Euclidean template, simple recursion, and finally b=0, so one layer at a layer up to the value of x, y
{
if (b==0)
{
x=1,y=0;
return;   
}
EX_GCD (b,a%b,x,y);
ll Temp=x;
X=y;
Y=temp-a/b*y;
return;   
}
int main ()
{
ll t,a,b,n,x,y;
cin>>t;     
while (t--)
{
Cin>>n>>b;
int ans;
EX_GCD (b,9973,x,y);
x=x*n;//x=a/b
Ans=c; CXc, so the

#include <stdio.h>
#include <cstring>
#include <iostream>
#define LL Long Long
using namespace Std;
void Ex_gcd (ll a,ll b,ll &x,ll &y)
{
if (b==0)
{
x=1,y=0;
return;
}
EX_GCD (B,a%b,x,y);
ll Temp=x;
X=y;
Y=temp-a/b*y;
return;
}
int main ()
{
ll T,a,b,n,x,y;
cin>>t;
while (t--)
{
cin>>n>>b;
int ans;
EX_GCD (B,9973,x,y);
X=x*n;
Ans= (x%9973+9973)%9973;//(A + b)% P = (a% p + b% p)% p, so (x%9973+9973)%9973=x%9973, if X is negative, x%9973 will go wrong, so Into (x%9973+9973)%9973, first make x positive again mod 9973
cout<<ans<<endl;
}
return 0;

}


cout<<ans<<endl;
}
return 0;

}

Extended Euclid HDU 1576

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.