3

线性同余方程

 2 years ago
source link: https://xiaocairush.github.io/math/number-theory/linear-equation/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

线性同余方程

介绍

形如 ax≡c(modb) 的方程被称为 线性同余方程(Congruence Equation)。

「NOIP2012」同余方程

求解方法

根据以下两个定理,我们可以求出同余方程 ax≡c(modb) 的解。

定理 1:方程 ax+by=c 与方程 ax≡c(modb) 是等价的,有整数解的充要条件为 gcd(a,b)∣c。

根据定理 1,方程 ax+by=c,我们可以先用扩展欧几里得算法求出一组 x0,y0,也就是 ax0+by0=gcd(a,b),然后两边同时除以 gcd(a,b),再乘 c。然后就得到了方程 acgcd(a,b)x0+bcgcd(a,b)y0=c,然后我们就找到了方程的一个解。

定理 2:若 gcd(a,b)=1,且 x0、y0 为方程 ax+by=c 的一组解,则该方程的任意解可表示为:x=x0+bt,y=y0−at, 且对任意整数 t 都成立。

根据定理 2,可以求出方程的所有解。但在实际问题中,我们往往被要求求出一个最小整数解,也就是一个特解 x=(xmodt+t)modt,其中 t=bgcd(a,b)。

代码实现

// C++ Version
int ex_gcd(int a, int b, int& x, int& y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  int d = ex_gcd(b, a % b, x, y);
  int temp = x;
  x = y;
  y = temp - a / b * y;
  return d;
}
bool liEu(int a, int b, int c, int& x, int& y) {
  int d = ex_gcd(a, b, x, y);
  if (c % d != 0) return 0;
  int k = c / d;
  x *= k;
  y *= k;
  return 1;
}
# Python Version
def ex_gcd(a, b ,x, y):
  if b == 0:
      x = 1; y = 0
      return a
  d = ex_gcd(b, a % b, x, y)
  temp = x
  x = y
  y = temp - a // b * y
  return d

def liEu(a, b, c, x, y):
  d = ex_gcd(a, b, x, y)
  if c % d != 0:
      return 0
  k = c // d
  x = x * k
  y = y * k
  return 1

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK