zoukankan      html  css  js  c++  java
  • 欧几里得算法和扩展欧几里得算法

    欧几里得算法

    作用:
    计算两个数的最大公约数。
    算法:
    欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数。
    用gcd(a, b)表示a和b的最大公约数。
    gcd函数的基本性质:gcd(a,b)=gcd(b,a)=gcd(-a,b)=gcd(|a|,|b|)=gcd(a,b-a)

    C实现:

    typedef long long int64;
     
    int64 gcd(int64 a, int64 b)
    {
         return b == 0 ? a : gcd(b, a% b);
    }

    复制代码
    1 typedef long long int64;
    2 
    3 int64 gcd(int64 a, int64 b)
    4 {
    5     return b == 0 ? a : gcd(b, a% b);
    6 }
    复制代码

     

    扩展欧几里得算法

    作用:

    计算两个数a, b的最大公约数的同时,求出两个整数x, y使得a*x + b*y = gcd(a, b),且|x| + |y|取得最小值。

    算法:

    用以下过程进行模拟:

    用类似辗转相除法,求二元一次不定方程47x+30y=1的整数解。

    • 47=30*1+17
    • 30=17*1+13
    • 17=13*1+4
    • 13=4*3+1

    然后把它们改写成“余数等于”的形式

    • 17=47*1+30*(-1) //式1
    • 13=30*1+17*(-1) //式2
    • 4=17*1+13*(-1) //式3
    • 1=13*1+4*(-3)

    然后把它们“倒回去”

    • 1=13*1+4*(-3) //应用式3
    • 1=13*1+[17*1+13*(-1)]*(-3)
    • 1=13*4+17*(-3) //应用式2
    • 1=[30*1+17*(-1)]*4+17*(-3)
    • 1=30*4+17*(-7) //应用式1
    • 1=30*4+[47*1+30*(-1)]*(-7)
    • 1=30*11+47*(-7)

    得解x=-7, y=11

    C实现:

    typedef long long int64;
     
    void ext_gcd(int64 a, int64 b, int64& d, int64& x, int64& y)
    {
        if (!b) d = a, x = 1, y = 0;
        else{
            ext_gcd (b, a%b, d, y, x);
            y -= x * (a / b);
         }
    }

    复制代码
     1 typedef long long int64;
     2 
     3 void ext_gcd(int64 a, int64 b, int64& d, int64& x, int64& y)
     4 {
     5     if (!b) d = a, x = 1, y = 0;
     6     else{
     7         ext_gcd (b, a%b, d, y, x);
     8         y -= x * (a / b);
     9     }
    10 }
    复制代码

     

  • 相关阅读:
    Powershell数据处理
    Powershell About Active Directory Group Membership of a domain user
    Powershell About Active Directory Server
    Oracle Schema Objects——Tables——TableStorage
    Oracle Schema Objects——Tables——TableType
    English Grammar
    Oracle Database Documentation
    Oracle Schema Objects——Tables——Oracle Data Types
    Oracle Schema Objects——Tables——Overview of Tables
    What is Grammar?
  • 原文地址:https://www.cnblogs.com/yuyixingkong/p/4366984.html
Copyright © 2011-2022 走看看