Solving the equations of linear equation by the algorithm

Source: Internet
Author: User
Tags gcd

Solving the equations of linear equation by the algorithm

1) solving a modal linear equation ax = b (mod n)

Equation ax = b (mod n), ax = b + NY->ax-ny = b

-AX + N (-y) =b where a,n,b is known. A set of special solutions to this equation can be obtained by expanding Euclidean.

Here are some theorems for solving the equation:

1. When and only when d|b, the equation ax = b (mod n) has a solution. D=GCD (A,n)

2.ax = b (mod n) either has a different solution of D, or no solution.

3. Make D=GCD (a,n) assume to Integer x ', y ', have d = Ax ' + NY ', if D | b, then the equation ax = b (mod n) has a solution with a value of x0, which satisfies:

X0=x ' (b/d) (mod n)

4. Assuming that the equation ax = b (mod n) has a solution, x0 is any solution to the equation, then the equation has a D different solution to modulo n, respectively:

Xi = x0 + i * (n/d), where i = 1,2,3......d-1

According to these 4 theorems, all solutions of the model linear equation can be easily obtained by using the extended Euclidean algorithm.

The pseudo code is as follows:

1 modular_linear_equation_solver (a,b,n)2(d,x', y')=extended_euclid (a,n)3 if(d|b)4X0=x'(b/d) mod n5      forI=0to D16Print (X0+i (n/d) mod n7 Else8Print"No Solutions"

2) solving the modular linear equation set

  x = A1 (mod m1)

  x = a2 (mod m2)

  x = a3 (mod m3)

  

  The first two equations of the equation group are solved. X=m1*k1+a1=m2*k2+a2

m1*k1+m2* (-K2) =a2-a1

This equation can be solved by Euclid to solve the k1 of the smallest positive integer x=m1*k1+a1 obviously x is the smallest positive integer solution of two equations.

The general solution of these two equations is X=X+K*LCM (m1,m2), x=x (mod LCM (m1,m2)), which transforms into a form of the same equation.

It is solved by this equation and the other equations behind it. The final result came out.

 Take POJ2891 as an example to paste the code:

Code:

1 /*************************************************************************2 > File Name:poj2891.cpp3 > Author:enumz4 > Mail: [email protected]5 > Created time:2014 October 28 Tuesday 02:50 07 seconds6  ************************************************************************/7 8#include <iostream>9#include <cstdio>Ten#include <cstdlib> One#include <string> A#include <cstring> -#include <list> -#include <queue> the#include <stack> -#include <map> -#include <Set> -#include <algorithm> +#include <cmath> -#include <bitset> +#include <climits> A #defineMAXN 100000 at #defineLL Long Long - using namespacestd; -ll EXTENDED_GCD (ll a,ll b,ll &x,ll &y)//The return value is GCD (A, b) - { - LL ret,tmp; -     if(b==0) in     { -x=1, y=0; to         returnA; +     } -RET=EXTENDED_GCD (b,a%b,x,y); thetmp=x; *x=y; $y=tmp-a/b*y;Panax Notoginseng     returnret; - } the intMain () + { A LL N; the      while(cin>>N) +     { -         Long LongA1,m1; $         Long Longa2,m2; $Cin>>a1>>M1; -         if(n==1) -printf"%lld\n", M1); the         Else -         {Wuyi             BOOLflag=0; the              for(intI=2; i<=n;i++) -             { WuCin>>a2>>m2; -                 if(flag==1)Continue; About                 Long Longx, y; $LL ret=EXTENDED_GCD (a1,a2,x,y); -                 if((M2-M1)%ret!=0) -flag=1; -                 Else A                 { +                     Long Longans1= (M2-M1)/ret*x; theans1=ans1% (a2/ret); -                     if(ans1<0) ans1+= (a2/ret); $m1=ans1*a1+M1; thea1=a1*a2/ret; the                 } the             } the             if(!flag) -cout<<m1<<Endl; in             Else thecout<<-1<<Endl; the         } About     } the     return 0; the}

Solving the equations of linear equation by the 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.