Question 254. Luogu P1082 Extended Euclidean Algorithm - [NOIP2012 Improve Group] Congruence Equations


Question 254. Luogu P1082 Extended Euclidean Algorithm - [NOIP2012 Improve Group] Congruence Equations


1. Overview of Solving Linear Congruence Equations

1. Interpretation of Linear Congruential Equations

ax ≡ c (mod b) represents the solution set for x, where x needs to satisfy (a*x)%b=c. Solving this problem is equivalent to solving the equation ax+by=c (or ax%b==c%b), where as long as the set of x can be found, then the set of y can also be found by x.

2. The method of solving the linear congruence equation: Extended Euclidean algorithm
(1) About the extended Euclidean algorithm:

Definition: As its name suggests, it is an extension of the Euclidean algorithm. After obtaining the greatest common divisor of integers a and b (we solve the greatest common divisor of a and b by the Euclidean algorithm, that is, the method of dividing by turns), the Hope to get integers x, y such that ax+by=gcd(a,b)

  • For integer a>b, when b=0, gcd(a,b)=a, at this time x=1, y=0; (We know that the Euclidean algorithm finally has the formal parameter b=0, return a, that is, a is equal to gcd(a,b), and at this time there is ax+by=gcd(a,b), then obviously x has to be equal to 1, y=0)
  • Suppose ax+by=gcd(a,b), the solutions of x and y are x1, y1, then ax1+by1=gcd(a,b), this is formula ①;
  • It can be seen from the Euclidean algorithm that the operation performed when b!=0 is gcd(b,a%b), that is, the formal parameters of the entered gcd a=b, b=a%b, then bx2+( a%b)y2=gcd(b,a%b), this is formula ②;
  • It can be known from the Euclidean algorithm that the operation performed when b!=0 is gcd(b,a%b), then gcd(a,b)=gcd(b,a%b), then combined with formulas ①② ax1+by1=bx2+(a%b)y2;
  • Since a%b=a-(a/b)*b (a division of b is logically equal to the quotient plus the remainder, then the remainder is equal to the dividend a minus the quotient multiplied by the divisor b), then ax1+by1=bx2+(a-(a /b)*b)y2=ay2+bx2-(a/b)*by2, that is, ax1+by1==ay2+b(x2-(a/b)*y2), this is formula ③;
  • It can be known from the identity theorem that if the formula ③ is established, then x1=y2, y1=x2-(a/b)*y2 ④;
  • In this way, we get x1, y1, that is, a set of solutions of ax+by=c, x1, y1 are based on x2, y2;
  • The above operations are also implemented recursively, which is based on the Euclidean recursive writing method. When the last b=0, the recursion ends, gcd(a, b) is obtained, and the return starts. Assuming that it is in the process of calling the Euclidean function, then x2 and y2 are in a deeper layer of recursion in the recursive process, and x1 and y1 are obtained by the assignment statement after the end of the deeper layer of recursive call. At the end of the call, the formal parameter a is equal to a in the original linear congruence equation, and b is also the same, then the corresponding x1, y1 is a set of solutions;

(2) Solve the linear congruence equation

1) The necessary and sufficient condition for the equation to have an integer solution is gcd(a,b)|c, that is, c is a multiple of gcd(a,b)
. We only need that x.
3) If x0, y0 are a set of solutions of the equation ax+by=c, then any solution of the equation can be expressed as x=x0+b't,y=y0-a't,t ∈ any integer, a'=a/gcd(a,b), b'=b/gcd(a,b)
4) In particular, if gcd(a,b)=1, and x0, y0 is the equation ax+ A set of solutions of by=c, then any solution of the equation can be expressed as x=x0+bt, y=y0-at, t∈Any integer
5) For the general linear congruence equation problem, it is often required to find a minimum Integer solution, the value is x=x0(x0 % t + t)%t, where t=b/gcd(a,b), t=b for 4)

Second, the topic

insert image description here

3. Problem solving

#include <bits/stdc++.h>

using namespace std;

int ex_gcd(int a,int b,int& x,int& y)
{
    
    
    if(b==0)
    {
    
    
        x=1;
        y=0;
        return a;
    }
    int ans=ex_gcd(b,a%b,x,y);
    int tmp=x;//x2
    x=y;//对应之前解释中的④式,x1=y2
    y=tmp-a/b*y;//y2=x2-(a/b)*y2,切记a,b不是一直等于原线性同余方程的a,b,只有最开始的扩欧函数的才是
    return ans;
}

int main()
{
    
    
    int a,b,x,y;
    cin>>a>>b;
    x=a,y=b;
    ex_gcd(a,b,x,y);
    x=(x%b+b)%b;//x=x0*(x0%t+t)%t,t=b/gcd(a,b),由于题目说ax ≡ 1 (mod b),则方程有整数解需1是gcd(a,b)的倍数,那显然gcd(a,b)=1,t=b,由于b>0,此时x必为最小正整数解
    cout<<x;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324347905&siteId=291194637