HDU 1576 A/b (extended Euclidean algorithm)

Source: Internet
Author: User
Tags gcd greatest common divisor

Connection: http://acm.hdu.edu.cn/showproblem.php?pid=1576

Sample Input
21000 5387 123456789

Sample Output
79226060 Test Instructions: requirements (A/b)%9973, but because a is large, we only give N (n=a%9973) (we given a must be divisible by B, and gcd (b,9973) = 1). 

The solution of number theory: the mathematical formula pushes a push, cycle comes out, did not think of AH.

Set A = k * 9973 + N, A/b = c, c = P * 9973 + x,x is the answer we ask for. Easy to know, A = k* 9973 + N =b * P * 9973 + B * x, simplifying after k * 9973 = b * P * 9973 + b * x-n, so (b * x-n)%9973 = 0,n value know, B's value is known, and because X The value range is 0 to 9972, so the value of the enumeration x is the answer that satisfies the condition.

#include <iostream> #include <cstdio> #include <string.h>using namespace Std;int main () {    int N;    scanf ("%d", &n);    while (n--)    {        long long  n,b;        Long long X;        scanf ("%lld%lld", &n,&b);        for (int i=0;i<9973;++i)        {            if ((b*i-n)%9973 = = 0)            {                x=i;                break;            }        }        printf ("%lld\n", x);           }    return 0;}

extended Euclidean algorithm (template):
__int64 extended_euclid (__int64 a,__int64 b,__int64& x,__int64& Y) {    if (b==0)    {        x=1;        y=0;        return A;    }    __int64 r=extended_euclid (b,a%b,x,y)    __int64 temp=x;x=y;y=t-a/b*y;    return R; R is a, B, greatest common divisor}

Extended Euclidean algorithm:
The extended Euclidean algorithm is used in known non-negative integers ( Otherwise, the equation needs to be deformed, as the solution of 5x-13y=1 is deformed to 5x+ ( -13y) = 1, and then the result is processed . A, b solves a set of x, y so that Ax+by = GCD (A, B) =d (the solution must exist, according to the correlation theorem in number theory). extended Euclidean is commonly used in solving linear equations and equations.

Here is a useC + +Implementation of:
int exgcd (int a, int b, int &x, int &y)
{
if (b = = 0)
{
x = 1;
y = 0;
return A; It is difficult for---to find a value that is so realized, since the expansion of Euclid has a greater use; the individual thinks it is better to define a global array without return R.
}
int r = EXGCD (b, a% B, x, y);
int t = x;
x = y;
y = t-a/b * y;
return R;
}
ways to solve indefinite equations using the extended Euclidean algorithm:
   for the indefinite integer equation pa+qb=c, if C mod Gcd (A, B) = 0, the equation has an integer solution, otherwise there is no integer solution.
   the method for finding an integer solution is listed above, and after finding a set of solutions for P * A+q * b = GCD (A, B) p0,q0, , * P * A+q * b = GCD (A, b) other integer solution satisfies:
p = P0 +  b/gcd (A, b) * t
Q = Q0 -  a/gcd (A, b) * t (where T is an arbitrary integer, and T in P,q)
As for the integer solution of Pa+qb=c, just p * a +q * b = GCD (A, b) for each solution on the C/GCD (A, B) can be
after finding p * a+q * b = GCD (A, b) of a set of solutions p0,q0, should be
get p * a+q * b = c a set of solutions P1 = p0* (c /GCD (A, b)), q1 = q0* (b), p * A+q * b = Other integer solutions of C are satisfied:
P = P1 +  b /GCD (A, b) * t
Q = Q1 -  a/gcd (A, b) * t (where T is an arbitrary integer and T in P,q is the same )
P, q is all integer solutions for p * A+q * b = c.

Note:your Choice is C + + ide#include <iostream>using namespace std; #define K 9973int Uex (int a,int b,int &x,in T &y) {    int r;    int t;    if (b==0)    {        x=1;        y=0;        return A;    }    R=uex (b,a%b,x,y);    t=x;    x=y;    Y=t-a/b*y;    return r;} int main () {    int t,t,n,b,x,y;    scanf ("%d", &t);    while (t--)    {        scanf ("%d%d", &n,&b);        Uex (b,k,x,y);        X*=n;        if (x<0)        {            t=-x;            T=t%k;            x=k-t;     Or  do not use the T variable direct x=k-(-X)%k, or use while (x<0) {x+=k/1;}, but not recommended because it may time out! It is best to use x= (x%k+k)%k, and if statement is not required;          }        printf ("%d\n", x%k);    }    return 0;}
I haven't read it yet.

HDU 1576 A/b (extended Euclidean algorithm)

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.